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 {
|
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;
};
class Time {
public:
Time();
Time(int h, int m, int s);
void add_seconds(long s);
long seconds_from(Time t) const;
int get_seconds() const;
int get_minutes() const;
int get_hours() const;
private:
int hours; /* conjecture */
int minutes; /* conjecture */
int secounds; /* conjecture */
};
Does this mean liftoff is at 25:30:00? The constuctors and add_seconds function guarantee that all times are always valid, thanks to the encapsulation mechanism.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;
}
if (next.is_better_than(best))
Syntax 6.2 : Member Function Definition return_type Class_name::function_name(parameter1, ..., parametern) [const]opt
|
Product::Product()
{ price = 0;
score = 0;
}
Syntax 6.3 : Constructor Definition Class_name::Class_name(parameter1, ..., parametern)
|
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;
}
The constructor must initialize the time fields using the Time() constructor.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;
};
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);
}
void raise_salary(Employee& e, double percent)
{ e.salary = e.salary * (1 + percent / 100 ); /* Error */
}
void raise_salary(Employee& e, double percent)
{ e.set_salary(e.get_salary() * (1 + percent / 100 )); /* OK */
}
class Time {
public:
Time();
Time(int h, int m, int s);
void add_seconds(long s);
long seconds_from(Time t) const;
int get_seconds() const;
int get_minutes() const;
int get_hours() const;
private:
long time_in_secs;
};
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;
}
versus the member functionvoid raise_salary(Employee& e, double percent)
{ double new_salary = e.get_salary() * (1 + percent / 100);
e.set_salary(new_salary);
}
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;