Oop Lab CPP
Oop Lab CPP
Reg No:369247
Department of Computing
Class: BEE-13D
Fall 2022
Lab07: Inheritance
Instructor: Mehreen
Tahir
Objective
Understanding and applying following concepts about inheritance in C++
• Inheritance syntax in C++
• Protected Access specifier
• Constructors and Destructors in base and derived class and their order of execution
• Effect of Base Access in Inheritance (public, protected and private)
Inheritance
Inheritance is a process of creating new classes (derived class) from existing (base) class. We do it
for one of the following reasons:
Reusability
Code/Software Reliability
Improved Conceptualization and overall design of program
Example:
Suppose we have a class “Cuboid” having three properties: length, width and height.
Class Cuboid:
class Cuboid {
private:
double length;
protected:
double width;
public:
double height;
cout << " Base Class : Default Constructor " << endl;
}
~Cuboid()
{
cout << " Base Class : Destructor " << endl;
}};
Now we want to add a function (calculateVolume for example) to operate on the objects of class
Cuboid. We do it by deriving a class “myCuboid” from base class “Cuboid”.
void calculateVolume()
{
cout << " Volume of cuboid : " << length * width * height;
}
~myCuboid()
{
cout << " Derived Class : Destructor " << endl;
}
};
int main()
{
myCuboid object(2.5, 3.0, 4.2);
object.calculateVolume();
system("pause");
return 0;
}
Surface area:
Task 1:
class Cuboid {
private:
double length;
protected:
double width;
public:
double height;
double getlength();
~Cuboid();
};
child.h
#pragma once
#include"base.h"
double getwidth();
};
Base_function.cpp
#include<iostream>
#include"base.h" // ACCESS THE BASE CLASS
using namespace std;
// IT IS TO DEFINE THE FUNCTION
Cuboid::Cuboid() : length(0), width(0), height(0)
{
cout << " Base Class : Default Constructor " << endl;
}
Cuboid::Cuboid(double a, double b, double c) : length(a), width(b), height(c)
{
cout << " Base Class : Parameterized Constructor " << endl;
}
double Cuboid::getlength() {
return length;
}
Cuboid::~Cuboid()
{
cout << " Base Class : Destructor " << endl;
}
Child_function.cpp
#include<iostream>
#include"child.h"
//#include"base.h"
using namespace std;
// WE USED PRIVATE SO WE CALLED CUBOID
myCuboid::myCuboid()
{
cout << " Derived Class: Default Constructor " << endl;
}
void myCuboid::calculateVolume()
{
cout << " Volume of cuboid : " << getlength() *width * height << endl;
}
myCuboid::~myCuboid()
{
cout << " Derived Class : Destructor " << endl;
}
Source.cpp
Private:
base.h:
class Cuboid {
private:
double length;
protected:
double width;
#include<iostream>
#include"base.h"
#include"child.h"
double getlength();
double getwidth();
double getheight();
// IT GET THE LENGTH
void displayData(double , double , double );
// IT DISPLAY THE DATA
~Cuboid();
};
child.h
#include"base.h"
child_function.cpp
#include<iostream>
#include"child.h"
//#include"base.h"
using namespace std;
// WE USED PRotected SO WE CALLED CUBOID
myCuboid::myCuboid()
{
cout << " Derived Class: Default Constructor " << endl;
}
double myCuboid::getLength() {
return Cuboid::getlength();
}
double myCuboid::getWidth() {
return Cuboid::getwidth();
}
double myCuboid::getHeight() {
return Cuboid::getheight();
}
myCuboid::myCuboid(double a, double b, double c) : Cuboid::Cuboid(a, b, c)
{
cout << " Derived Class : Parameterized Constructor " << endl;
}
void myCuboid::calculateVolume()
{
cout << " Volume of cuboid : " << Cuboid::getlength() * Cuboid::getwidth() * Cuboid::getheight() << endl;
}
myCuboid::~myCuboid()
{
cout << " Derived Class : Destructor " << endl;
}
}
base_function.cpp
#include<iostream>
#include"base.h" // ACCESS THE BASE CLASS
using namespace std;
// IT IS TO DEFINE THE FUNCTION
Cuboid::Cuboid() : length(0), width(0), height(0)
{
cout << " Base Class : Default Constructor " << endl;
}
Cuboid::Cuboid(double a, double b, double c) : length(a), width(b), height(c)
{
cout << " Base Class : Parameterized Constructor " << endl;
double Cuboid::getlength() {
return length;
}
double Cuboid::getwidth() {
return width;
}
double Cuboid::getheight() {
return height;
}
void Cuboid::displayData(double a, double b, double c)
{
cout << "length = " << length << endl;
cout << "width = " << width << endl;
cout << "height = " << height << endl;
}
Cuboid::~Cuboid()
{
cout << " Base Class : Destructor " << endl;
}
Source.cpp
#include<iostream>
#include"base.h"
#include"child.h"
using namespace std;
// MAIN FUNCTION
int main()
{
myCuboid object(2.5, 3.0, 4.2); // DEFINIG CHILD CLASS TO OBJECT
object.calculateVolume();
// PRINT SURFACE AREA
cout << " surface area of cuboid : " << 2 * ((object.getLength() * object.getWidth()) + (object.getWidth() *
object.getHeight()) + (object.getLength() * object.getHeight())) << endl;
system("pause");
return 0;
}
OUTPUT:
protected:
double width;
public:
double height;
double getlength();
double getwidth();
double getheight();
// IT GET THE LENGTH
void displayData(double , double , double );
// IT DISPLAY THE DATA
~Cuboid();
};
};
child_function.cpp
#include<iostream>
#include"child.h"
//#include"base.h"
using namespace std;
// WE USED PRotected SO WE CALLED CUBOID
myCuboid::myCuboid()
{
cout << " Derived Class: Default Constructor " << endl;
}
double myCuboid::getLength() {
return Cuboid::getlength();
}
double myCuboid::getWidth() {
return Cuboid::getwidth();
}
double myCuboid::getHeight() {
return Cuboid::getheight();
}
myCuboid::myCuboid(double a, double b, double c) : Cuboid::Cuboid(a, b, c)
{
cout << " Derived Class : Parameterized Constructor " << endl;
}
void myCuboid::calculateVolume()
myCuboid::~myCuboid()
{
cout << " Derived Class : Destructor " << endl;
}
}
base_function.cpp
#include<iostream>
#include"base.h" // ACCESS THE BASE CLASS
using namespace std;
// IT IS TO DEFINE THE FUNCTION
Cuboid::Cuboid() : length(0), width(0), height(0)
{
cout << " Base Class : Default Constructor " << endl;
}
Cuboid::Cuboid(double a, double b, double c) : length(a), width(b), height(c)
{
cout << " Base Class : Parameterized Constructor " << endl;
}
double Cuboid::getlength() {
return length;
}
double Cuboid::getwidth() {
return width;
}
double Cuboid::getheight() {
return height;
}
void Cuboid::displayData(double a, double b, double c)
{
cout << "length = " << length << endl;
cout << "width = " << width << endl;
cout << "height = " << height << endl;
}
Cuboid::~Cuboid()
{
cout << " Base Class : Destructor " << endl;
}
Source.cpp
#include<iostream>
OUTPUT:
Deliverables: Complete lab manual by performing all tasks. Copy paste your code and screen
shot of console window as a solution of each task. You are required to upload the lab tasks on
LMS and the name of the task must be in this format YourFullName_reg#.