Object Oriented Programming Lab Manual (Lab 07) : Topic: Basic Inheritance
Object Oriented Programming Lab Manual (Lab 07) : Topic: Basic Inheritance
public:
//constructor
BaseClass()
{
cout << "BaseClass constructor" << endl;
}
//function
void print()
{
cout << "BaseClass print function" << endl;
}
//destructor
~BaseClass()
{
cout << "BaseClass destructor" << endl;
}
};
Derived class public inherit with base class
class DerivedClass :public BaseClass
{
public:
//constructor
DerivedClass()
{
cout << "derived class constructor" << endl;
}
//function
void print()
{
cout << "derived class print function" << endl;
}
//destructor
~DerivedClass()
{
cout << "derived class destructor" << endl;
}
};
Create base and derived class objects
int main()
{
BaseClass bObj; // base class constructor call
bObj.print(); // base class print function call
return 0;
}
How base class parameterized constructor call from derived class constructor.
Example:
class A
{
int aValue;
public:
A(int a)
{
aValue=a;
}
}
class B
{
int bValue;
public:
B(int a,int b):A(a) // a value send to base class constructor
{
bValue=b;
}
}
Sample Problems:
Creating a Person Class
class person
{
char *name;
int age;
public:
person()
{
name = nullptr;
age = 0;
}
person(char *n, int a)
{
}
void setAge(int a)
{
age = a;
}
char* getName()
{
return name;
}
int getAge()
{
return age;
}
void print_info()
{
cout << getName() << endl;
cout << getAge() << endl;
}
~person()
{
if (name != nullptr)
delete []name;
name = nullptr;
}
};
public:
student() :person()
{
rollNo = nullptr;
cgpa = 0.0;
}
student(char*n, int a, char*rn, float cg) :person(n, a)
{
rollNo = new char[strlen(rn) + 1];
memcpy(rollNo, rn, strlen(rn) + 1);
cgpa = cg;
}
void setRollNo(char*rn)
{
if (rollNo != nullptr)
delete[]rollNo;
//override method
void print_info()
{
person::print_info();
cout << getRollNo() << endl;
cout << getCgpa() << endl;
}
~student()
{
if (rollNo != nullptr)
delete []rollNo;
rollNo = nullptr;
}
};
Create derived class object
int main()
{
student std("Afzal", 24, "BSCSF19023", 3.3f);
std.print_info();
return 0;
}
Lab Tasks
Task 1:
Employee Inheritance
Task A
Create a class Employee with following constraints in mind;
Declare two data members named firstName and lastName of string type.
Implement a parameterized constructor, which will take firstName and lastName of an
employee and initialized the respective data members.
Implement a print function that displays the complete name (firstName lastName) of an
Employee.
Task B
Test the working of your created Employee class with the help of a main program.
Task C
Create a class HourlyWorker which inherits from Employee class publically with following
constraints in mind;
Declare two data members named wage and hours of double type with private access.
Implement a parameterized constructor, which initializes all the data members of
HourlyWorker class.
Implement a getPay function that calculate and return the salary (hours * wage) of
particular HourlyWorker employee.
Implement a print function that displays the complete name and salary of a particular
employee.
Task D
Test the working of your created HourlyWorker class with the help of a main program.