0% found this document useful (0 votes)
53 views

Oop Lab CPP

This document provides an example of inheritance in C++. It defines a base Cuboid class with length, width, and height properties and associated functions. It then defines a derived myCuboid class that inherits from Cuboid. myCuboid adds a calculateVolume() function. The document explains inheritance and its uses. It also provides tasks to test public, protected, and private inheritance by accessing base class members from the derived class.

Uploaded by

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

Oop Lab CPP

This document provides an example of inheritance in C++. It defines a base Cuboid class with length, width, and height properties and associated functions. It then defines a derived myCuboid class that inherits from Cuboid. myCuboid adds a calculateVolume() function. The document explains inheritance and its uses. It also provides tasks to test public, protected, and private inheritance by accessing base class members from the derived class.

Uploaded by

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

Abdurrahim Muhammad Ghani

Reg No:369247
Department of Computing

CS 212: Object Oriented Programming

Class: BEE-13D
Fall 2022

Lab07: Inheritance

Date: 24th October, 2022

Time: 9:00 a.m. - 12:00 p.m.

Instructor: Mehreen
Tahir

Lab Engineer: Mehwish Kiran


Introduction
In this lab Students will understand how inheritance works in OOP.

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;

Cuboid() : length(0), width(0), height(0)


{

cout << " Base Class : Default Constructor " << endl;
}

Cuboid(double a, double b, double c) : length(a), width(b), height(c)


{
cout << " Base Class : Parameterized Constructor " << endl;
}

void setData(double a, double b, double c)

CS 212: Object Oriented Page 3


Programming
{
length = a;
width = b;
height = c;
}

void displayData(double a, double b, double c)


{
cout << "length = " << length << endl;
cout << "width = " << width << endl;
cout << "height = " << height << 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”.

Derived Class myCuboid:

class myCuboid : public Cuboid {


public:
myCuboid()
{
cout << " Derived Class: Default Constructor " << endl;
}

myCuboid(double a, double b, double c) : Cuboid(a, b, c)


{
cout << " Derived Class : Parameterized Constructor " << endl;
}

void calculateVolume()
{
cout << " Volume of cuboid : " << length * width * height;
}

~myCuboid()
{
cout << " Derived Class : Destructor " << endl;
}
};

CS 212: Object Oriented Page 4


Programming
Main function:

int main()
{
myCuboid object(2.5, 3.0, 4.2);
object.calculateVolume();
system("pause");
return 0;
}

Surface area:

Task 1:

a) Understand and execute the given example.


b) Verify the three cases of Base Class Access:

CS 212: Object Oriented Page 5


Programming
Try to access the three data members of base class in derived class (in some function) for
each of three cases, and if access is not possible (in any case), try to find a solution. Note:
do not change the access of data members: length, width and height in base class.
Public:
Base.h
#pragma once

class Cuboid {
private:
double length;

protected:
double width;

public:
double height;

Cuboid(); // default constuctor


Cuboid(double , double , double ) ; // paramtized contructor

void setData(double , double , double ); //set dATA OF THE FUNCTION

double getlength();

// IT GET THE LENGTH


void displayData(double , double , double );
// IT DISPLAY THE DATA

~Cuboid();

};

child.h
#pragma once

#include"base.h"

CS 212: Object Oriented Page 6


Programming
class myCuboid : public Cuboid {// CHILD CLASS
public:
myCuboid();// DEFAULT CONSTRUCTOR

myCuboid(double a , double b , double c ) ;// PARAMTIZED CONSTRUCTOR


// PRINT SURFACE AREA

void calculateVolume();// PRINT VOLUME

double getwidth();

~myCuboid(); // DAFULT COBNSTRUCTOR

};

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

void Cuboid::setData(double a, double b, double c)


{
length = a;
width = b;
height = c;
}

double Cuboid::getlength() {
return length;
}

void Cuboid::displayData(double a, double b, double c)


{
cout << "length = " << length << endl;
cout << "width = " << width << endl;

CS 212: Object Oriented Page 7


Programming
cout << "height = " << height << endl;
}

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

myCuboid::myCuboid(double a, double b, double c) : Cuboid(a, b, c)


{
cout << " Derived Class : Parameterized Constructor " << endl;
}
double myCuboid::getwidth() {
return width;
}

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"

CS 212: Object Oriented Page 8


Programming
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.height) + (object.getlength() * object.height));
system("pause");
return 0;
}
public:
double height;

Cuboid(); // default constuctor


Cuboid(double , double , double ) ; // paramtized contructor

void setData(double , double , double ); //set dATA OF THE FUNCTION

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"

class myCuboid : protected Cuboid {// CHILD CLASS


public:
myCuboid();// DEFAULT CONSTRUCTOR

myCuboid(double a , double b , double c ) ;// PARAMTIZED CONSTRUCTOR


void getsurfacearea(); // PRINT SURFACE AREA

void calculateVolume();// PRINT VOLUME


double getLength();
double getWidth();
double getHeight();
//get data from the base class

~myCuboid(); // DAFULT COBNSTRUCTOR

CS 212: Object Oriented Page 9


Programming
};

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;

CS 212: Object Oriented Page


Programming 10
}

void Cuboid::setData(double a, double b, double c)


{
length = a;
width = b;
height = c;
}

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:

CS 212: Object Oriented Page


Programming 11
PROTECTED:
base.h:
class Cuboid {
private:
double length;

protected:
double width;

public:
double height;

Cuboid(); // default constuctor


Cuboid(double , double , double ) ; // paramtized contructor

void setData(double , double , double ); //set dATA OF THE FUNCTION

double getlength();
double getwidth();
double getheight();
// IT GET THE LENGTH
void displayData(double , double , double );
// IT DISPLAY THE DATA

~Cuboid();

};

CS 212: Object Oriented Page


Programming 12
child.h
#include"base.h"

class myCuboid : private Cuboid {// CHILD CLASS


public:
myCuboid();// DEFAULT CONSTRUCTOR

myCuboid(double a , double b , double c ) ;// PARAMTIZED CONSTRUCTOR


void getsurfacearea(); // PRINT SURFACE AREA

void calculateVolume();// PRINT VOLUME


double getLength();
double getWidth();
double getHeight();
//get data from the base class

~myCuboid(); // DAFULT COBNSTRUCTOR

};

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()

CS 212: Object Oriented Page


Programming 13
{
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;
}

void Cuboid::setData(double a, double b, double c)


{
length = a;
width = b;
height = c;
}

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>

CS 212: Object Oriented Page


Programming 14
#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:

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

CS 212: Object Oriented Page


Programming 15

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy