0% found this document useful (0 votes)
47 views6 pages

Object Oriented Programming Lab Manual (Lab 07) : Topic: Basic Inheritance

The document is a lab manual on basic inheritance in object-oriented programming. It discusses creating a base class and derived class that inherits from the base class. It provides sample code of a base class and derived class that demonstrates how the derived class inherits properties and methods from the base class. It also discusses how to call the base class constructor from the derived class constructor. The lab tasks section provides an example problem of creating an Employee class and HourlyWorker class that inherits from Employee to model class inheritance.

Uploaded by

Haris Hassan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views6 pages

Object Oriented Programming Lab Manual (Lab 07) : Topic: Basic Inheritance

The document is a lab manual on basic inheritance in object-oriented programming. It discusses creating a base class and derived class that inherits from the base class. It provides sample code of a base class and derived class that demonstrates how the derived class inherits properties and methods from the base class. It also discusses how to call the base class constructor from the derived class constructor. The lab tasks section provides an example problem of creating an Employee class and HourlyWorker class that inherits from Employee to model class inheritance.

Uploaded by

Haris Hassan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Object Oriented Programming

Lab Manual (Lab 07)

Topic: Basic Inheritance


Course Instructor: Mam Asma Naseer
Lab Instructor: M.Waheed Waqar

Session: Fall 2020

School of Systems and Technology


UMT Lahore Pakistan
Objectives:
Implementation of inheritance. How derived class inherit with base class. How base class constructor call
by derived class.
Sample Problems:
Creating a Base Class
class BaseClass
{

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

DerivedClass dObj; // first base class constructor call then derived


dObj.print(); /* derived class print function call . if derived class not
implement print function then base class print function automatically 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)
{

name = new char[strlen(n) + 1];


memcpy(name, n, strlen(n) + 1);
age = a;
}
void setName(char*n)
{
if (name != nullptr)
delete[] name;

name = new char[strlen(n) + 1];


memcpy(name, n, strlen(n) + 1);

}
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;
}

};

Student class public inherit with Person class


class student :public person
{
char *rollNo;
float cgpa;

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;

rollNo = new char[strlen(rn) + 1];


memcpy(rollNo, rn, strlen(rn) + 1);
}
void setCgpa(float cg)
{
cgpa = cg;
}
char* getRollNo()
{
return rollNo;
}
float getCgpa()
{
return cgpa;
}

//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.

You might also like