Revision Exercise - 2
Exercise 1
Given an initialisation of the following structure type, describe what happens with each initialisation. Explain any problems with these initialisations.
struct Date {
int month;
int day;
int year;
};Date date = {12, 21};Date date = {12, 21, 20, 22};Date date = {12, 21, 22};
Exercise 2
Define a structure type to contain information about a given person. The information that this structure should contains are:
The annual salary for the person
The accrued vacation for the person (represented as a whole number of days)
Status, which is either hourly or salaried, represented by two
charvalues 'H' and 'S' respectively.
This structure type is to be called EmployeeRecord
Exercise 3
Part 1
Given the following class declaration:
class DayOfYear {
int day;
int month;
public:
void input();
void output();
};Implement the DayOfYear::input and DayOfYear::output functions such that the following program results in the following output:
int main() {
DayOfYear dayOfYear;
dayOfYear.input();
dayOfYear.output();
}Enter the day of the month: 13
Enter the month as a number: 5
day=13, month=5Define the member functions separately from the class, using the scope resolution operator (::).
Part 2
Create a private member function DateOfYear::check_date that will be called by DateOfYear::input to ensure that the dates being input are valid:
The day of the month cannot be less than 1, and cannot exceed 31.
The month of the year cannot be less than 1, or greater than 12.
Part 3
Create two constructors for the DayOfYear class. The first should take two int parameters corresponding to the day and month respectively. The second should have no parameters which constructs a date represented as January 1.
Create two DayOfYear variables that use each of these constructors.
Part 4
Suppose we now want to be able to compare two instances of DayOfYears, we can do so by overloading the == operator.
Create this overload function, that returns true if both the day and month member variables of a given DayOfYear is equal to another.
DayOfMonth first = DayOfMonth(3, 6);
DayOfMonth second = DayOfMonth(3, 6);
if (first == second) {
std::cout << "The dates are equal!" << std::endl;
}Implement the same for the operator <, which tests if a date comes before another.
Exercise 4
Consider the following class definition:
class Automobile {
private:
double price;
double profit;
double get_profit();
public:
void set_price(double new_price);
void set_profit(double new_profit);
double get_price();
};Suppose the program has created two instances of Automobile and has set the values of all member variables to some value:
Automobile hyundai, jaguar;
Which of the following statements are allowed to be called from main()?
hyundai.price = 1999.99jaguar.set_price(30000.10)double a_price = jaguar.get_price();double a_profit = jaguar.get_profit();double b_profit = hyundai.get_profit();
Exercise 5
Consider the following class definition:
class Foo {
public:
Foo(int x, char y);
Foo();
void bar();
private:
int x;
char y;
};Assuming that all the functions declared have been later defined, which of the following are legal?
Foo foo_one(42, 'A')Foo foo_two;Foo foo_three();foo_one = Foo(99, 'B');foo_one = Foo();foo_one = Foo;
Exercise 6
Define a simple class
Foothat inherits from a classBar. (Usepublicinheritance)If
Barwere to have anyprivatemember variables or functions, would these be accessible inFoo? If not, how can we change it so that it can be accessible withinFoo?
Exercise 7
Given the following class definitions:
class Fish {
public:
void greet() {
std::cout << "I am a Fish" << std::endl;
}
};
class Tuna {
public:
void greet() {
std::cout << "I am a Tuna!" << std::endl;
}
};Predict the output of the following program:
int main() {
Fish fish;
Tuna tuna;
fish.greet();
tuna.greet();
tuna.Fish::greet();
fish.Fish::greet();
}What would be the output if we removed the Tuna::greet function?
Last updated
Was this helpful?