Sei sulla pagina 1di 2

Practice Exercises

1. Write the definition of a class that has the following properties:


a. The name of the class is SecretType
b. The class SecretType has four member variables: name of type string, age and weight of
type int, and height of type double.
c. The class SecretType has the following member functions.
i. Print – outputs the data stored in the member variables with the appropriate
titles
ii. setName—function to set the name
iii. setAge—function to set the age
iv. setWeight—function to set the weight
v. setHeight—function to set the height
vi. getName—value-returning function to return the name
vii. getAge- value-returning function to return the age
viii. getWeight—value-returning function to return the weight
ix. getHeight—value-returning function to return the height
x. default constructor—with default parameters: name=empty string, and age,
weight and height are 0.
d. Write the definition of the member functions of the class SecretType as described in
part C
2. Composition: (has-a relationship)
Create a class Date that stores date values. Use integer values to represent the private data
members of the class – month, day and year. Month values should accept 1 to 12 only,
pertaining to the months of the year. Day values should accept 1-31 depending on the month.
Provide a constructor that enables the user to initialize the object when it is declared. The
constructor must check the validity of the values given by the user. Write a utility function,
checkDay() to check whether the day is proper for the month and the year.

Provide a public member function that prints the date in month/day/year format.

Create a class Employee that contain private data members firstName, lastName, birthDate and
hireDate. Members birthDate and hireDate are objects of class Date. Provide a constructor that
enables the user to initialize the object when it is declared. The constructor must use member
initializer list to pass initializer values to constructors of member object birthDate and hireDate.

Provide a public member function that prints the employee information.

3. Inheritance: (kind-of or is-a relationship)


a. Consider the following code:
class One{
public:
void print();
protected:
void setData(int u, int v);
private:
int x;
int y;
};

class Two: public One{


public:
void setData(int a, int b, int c);
void print();
private:
int z;
};

a.1. Writethe definition of the function setData of the class two.


a.2. Write the definition of the function print of the class two.

Potrebbero piacerti anche