
ACMA P600 995 77 Alaris Nx868 798 57
if (next_score / next_price > best_score / best_price)
{
best_name = next_name;
best_score = next_score;
best_price = next_price;
}
class Product
{
public:
Product();
void read();
bool is_better_than(Product b) const;
void print() const;
private:
implementation details
};
|
Syntax 6.1 : Class Definitions class Class_name
{
public:
constructor declarations
member function declarations
private:
data fields
};
|
Product best; /* default construction */
class Product
{
public:
Product();
void read();
bool is_better_than(Product b) const;
void print() const;
private:
string name;
double price;
int score;
};
Does this mean liftoff is at 25:30:00?Time liftoff(19, 30, 0); /* liftoff is delayed by six hours */ /* won't compile, but suppose it did */ liftoff.hours = liftoff.hours + 6;
void Product::read()
{
cout << "Please enter the model name: ";
getline(cin, name);
cout << "Please enter the price: ";
cin >> price;
cout << "Please enter the score: ";
cin >> score;
string remainder; /* read the remainder of the line */
getline(cin, remainder);
}
next.read();
void Product::print() const
{
cout << name
<< " Price: " << price
<< " Score: " << score << "\n";
}
best.print();
bool Product::is_better_than(Product b) const
{
if (b.price == 0) return false;
if (price == 0) return true;
return score / price > b.score / b.price;
}
|
Syntax 6.2 : Member Function Definition return_type Class_name::function_name(parameter1, ..., parametern) [const]opt
{
statements
}
|
Product::Product()
{
price = 0;
score = 0;
}
|
Syntax 6.3 : Constructor Definition Class_name::Class_name(parameter1, ..., parametern)
{
statements
}
|
Employee Fred; /* default construction */
Employee Harry("Harry Hacker", 40000); /* alternate construction */
class Employee
{
public:
Employee()
Employee(string employee_name, double initial_salary);
void set_salary(double new_salary);
string get_name() const;
double get_salary() const;
private:
string name;
double salary;
};
Employee::Employee(string employee_name, double initial_salary)
{
name = employee_name;
salary = initial_salary;
}
class Employee
{
public:
Employee(string employee_name, double initial_salary,
int arrive_hour, int leave_hour);
. . .
private:
string name;
double salary;
Time arrive;
Time leave;
};
The constructor must initialize the time fields using the
Time() constructor.
Employee::Employee(string employee_name, double initial_salary,
int arrive_hour, int leave_hour)
{
name = employee_name;
salary = initial_salary;
arrive = Time(arrive_hour, 0, 0);
leave = Time(leave_hour, 0, 0);
}
|
Syntax 6.4 : Constructors with Field Initializer List Class_name::Class_name(parameters):field1(expression), ..., fieldn(expression)
{
statements
}
|
void raise_salary(Employee& e, double percent)
{
e.salary = e.salary * (1 + percent / 100 ); /* Error */
}
void raise_salary(Emplyee & e, double percent)
{
double new_salary = e.get_salary() * (1 + percent / 100);
e.set_salary(new_salary);
}
Time::Time(int hour, int min, int sec)
{
time_in_secs = 60 * 60 * hour + 60 * min + sec;
}
int Time::get_minutes() const
{
return (time_in_secs / 60) % 60;
}
int Time::seconds_from(Time t) const
{
return time_in_secs - t.time_in_secs;
}
void raise_salary(Employee& e, double percent)
{
double new_salary = e.get_salary()
* (1 + percent / 100);
e.set_salary(new_salary);
}
versus the memeber function
void Employee::raise_salary(double percent)
{
salary = salary * (1 + percent / 100);
}
raise_salary(harry, 7);
harry.raise_salary(7);
void Employee::print() const
{
cout << "Name: " << get_name()
<< "Salary: " << get_salary()
<< "\n";
}
| Explicit Parameter | Implicit Parameter | |
| Value Parameter (not changed) |
Default Example: void print(Employee) |
Use const Example: void Employee::print()const |
| Reference Parameter (can be changed) |
Use & |
Default |
#ifndef PRODUCT_H #define PRODUCT_H . . . #endif
#include "Product.h"
extern GraphicWindow cwin;
The source file contains the definition.
GraphicWindow cwin;