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

Lab Manual Object Oriented Programming

This document is a lab manual for Object Oriented Programming at Government College University Faisalabad, detailing various programming exercises and code examples. It covers topics such as arrays, functions, inheritance, and classes in C++, providing sample code for predefined and user-defined arrays, as well as demonstrating different types of inheritance. Each section includes code snippets and explanations to help students understand the concepts and implement them in their programming tasks.

Uploaded by

bila3727673
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)
4 views

Lab Manual Object Oriented Programming

This document is a lab manual for Object Oriented Programming at Government College University Faisalabad, detailing various programming exercises and code examples. It covers topics such as arrays, functions, inheritance, and classes in C++, providing sample code for predefined and user-defined arrays, as well as demonstrating different types of inheritance. Each section includes code snippets and explanations to help students understand the concepts and implement them in their programming tasks.

Uploaded by

bila3727673
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/ 116

[ GOVERNMENT COLLEGE UNIVERSTY FAISALABAD ]

Object Oriented Programming

Lab Manual

Bilal – Farhan
( 243721 )
Saim – ul – Hassan
( 243716 )
Iqra Zahid
( 243666 )
Fatima Kausar
( 243696 )
LAB – Manuals for 2nd
semester .

Contents
Array ( Pre – define )...................................................................................................................................6
Code 1: programe to show the predefined array values.......................................................6
Array ( User – defined )..............................................................................................................................7
Code 2: Program to get array values from user and display them..........................................7
Code 3: Program to get index or size of array and its values at the same thime from the user
and display them .......................................................................................................................9
Code 4: Program to display all of the even and odd numbers from the stored values in a pre
or user defined array................................................................................................................10
Code 5: Program to get name and the roll numbers of the student and stor them in arrayes
and display them on the screen in the form of a table ...........................................................12
Code 6: Program to get the size of array and its values from user and calculate its sum and
product ....................................................................................................................................13
Code 7: Program to get values from user continusly until he/she entered -1 and show the
largest number above them ....................................................................................................15
Code 8 : Program to get the array size and its values at the same time and display the
largest and smaller values stored in array to the user ...........................................................16
Functions...................................................................................................................................................19
Code 9: Program that use function to get your age and display in on screen........................19
Code 10: Program to get number of subjects their obtained and total marks and show the
percentage on the screen ........................................................................................................20
Code 11: Program to get the names and roll numbers of the different student by using
arrays and saprate the data of each student with a line on screen . each task would have to
be performed by using functions.............................................................................................22
Code 12 : Write program to take user input for an array size and values and display it on
screen also tell us the count of negative and positive values of the array and also the smallest
value of the array and perform each task by using the different function for each ................24
Functions (with parameters).....................................................................................................................29
Code 13 : Program to get the marks of 5 subjects from user by using functions and display
its total marks and percentage also by using saprate function for both..................................29

2|Page( By BIlal - Farhan )


LAB – Manuals for 2nd
semester .
Code 14:................................................................................................................................... 31
Code 15: Program to find the maximum and minimum value in an array ...............................33
Pass by Value.............................................................................................................................................36
Code 16: Programe to show the concept of pass by value ...................................................36
Pass by Reference......................................................................................................................................37
Code 17: Program that shows the working of pass by value and pass by reference ..............37
Structure....................................................................................................................................................39
Code 18 : Program that show case the working of the structures..........................................39
Code 19 : Program that show case the working of the structure’s with functions..................40
Classes and objects....................................................................................................................................43
Code 20 : Program that show case the working of the classes and objects.............................43
Code 21 : Program that show case the working of the access specifiers in the class...............44
Code 22: Program that show case the working of constructor................................................46
Code 23 : Program that show case the working of destructor................................................47
Banck Account...........................................................................................................................................49
Code 24: Programe of a BANCK ACCOUNT by using the classes, objects and member
functions ................................................................................................................................. 49
Inheritance................................................................................................................................................59
Code 25 : Programe to show the inheritance of two classes...................................................59
Single inheritance......................................................................................................................................61
Code 26 : Programe that get the name and subject of an object which are the private
members of two different classes and display them on the screenby using the functions ....61
Multiple Inheritance..................................................................................................................................64
Code 27 : Programe that shows the working of multiple inheritance......................................64
Code 28 : Program in which we have create a base class Person with attributes name and age.
Derive a class Employee from Person, and then derive a class Manager from Employee.
Display all data using the Manager object...............................................................................66
Code 29 : Create a C++ program to demonstrate multiple inheritance and function conflict
resolution using the scope resolution operator. Define two base classes, Shape and Circle,
each with a display() function. Derive a class Cylinder from both, and in its display() method,
call both base class functions explicitly. Use an object of Cylinder to show the output..........68

3|Page( By BIlal - Farhan )


LAB – Manuals for 2nd
semester .
Code 30 : Write a C++ program to demonstrate multiple inheritance using two base classes:
Engine to display engine type and Wheels to display wheel type. Derive a class Car from both
and add a method to show car information. In the main() function, create an object of Car
and call all methods to display complete car details................................................................70
Code 31 : Write a program using multiple inheritance where class Person has attributes id
and name (with input/display functions) and class Academic has attributes roll number and
marks[5] (with input/display functions). Create class Student that inherits both Person and
Academic, and has a function to calculate and display percentage of the five marks.............73
Code 32 : Program created by using multiple inheritance where class Date has attributes day,
month, year (with input/display functions) and class Time has attributes hour, minute, second
(with input/display functions). Create class DateTime that inherits both Date and Time, and
has a function to input date and time together and display them in the format “DD-MM-YYYY
HH:MM:SS.”............................................................................................................................. 77
Multi – level Inheritance............................................................................................................................84
Code 33 : Programe that shows the working of multi - level inheritance................................84
Code 34 : program to demonstrate the concept of multi-level inheritance. Create a base
class Vehicle with speed-related functions. Derive a class Car from Vehicle, which adds the
number of doors and a function to display car details. Then, derive a class Sedan from Car,
which adds trunk capacity and overrides the display function to include all relevant details.
The program should clearly show the inheritance of data and functions across the levels.....86
Code 35 : . Create a base class Person with a function to display a message. Derive a class
Student from Person that adds its own display function. Then create a class Graduate derived
from Student, with an additional function to display graduate-specific information. In the
main() function, create an object of the Graduate class and demonstrate the inheritance of
functions from all levels...........................................................................................................89
Code 36 : Write a class Creature with attribute speciesName (with input/display functions).
Create class Mammal that inherits Creature and adds attribute hasFur (with input/display
functions). Create class Dog that inherits Mammal and adds attribute breed (with
input/display functions) and a member function bark() that displays “Woof! Woof!” In main,
input and display a Dog’s species, fur status, breed, and barking............................................91
Code 37 : Write a class Device with attribute powerState (on/off) and member functions
turnOn() and turnOff() to change and display the power state. Create class Computer that
inherits Device and adds attributes processor and ram (with input/display functions). Create
class Laptop that inherits Computer and adds attributes batteryLife and weight (with
input/display functions) and a member function showInfo() that turns on the device, displays
processor, ram, batteryLife, weight, then turns off the device................................................94

4|Page( By BIlal - Farhan )


LAB – Manuals for 2nd
semester .
Code 38 : Create a class Country with country name and currency. Derive a class Province and
then a class City. Display complete location details in the City class using multilevel
inheritance............................................................................................................................... 98
Code 39 : Create a class Country with country name and currency. Derive a class Province and
then a class City. Display complete location details in the City class using multilevel
inheritance............................................................................................................................. 100
Hierarchical inheritance..........................................................................................................................103
Code 40 : program demonstrating hierarchical inheritance where a base class Animal contains
a common attribute name. Derive two child classes Dog and Cat from Animal, each with their
own unique attributes (breed for Dog and color for Cat). Implement member functions to set
and display the details of both Dog and Cat objects..............................................................103
Code 41 : Program using hierarchical inheritance with base class Course containing course
name and code. Derive classes Theory (adds credits and teacher) and Lab (adds equipment
list). Input and display data for both classes..........................................................................105
Code 42 : Creating a program using hierarchical inheritance with base class Appliance
containing brand name. Derive classes Washer (adds capacity) and Refrigerator (adds
volume). Input and display data for both classes...................................................................109
Code 43 : Creating a program using hierarchical inheritance with a base class Vehicle
containing the brand name. Derive classes Car (adds number of doors) and Bike (adds bike
type). Input and display data for both using appropriate functions.......................................111
Code 44 : Creating a program using hierarchical inheritance where a base class Person has a
common function showDetails(). Derive two classes Student and Teacher from Person, each
with their own specific functions. Create objects of both derived classes and demonstrate
access to both base and derived class functions....................................................................114

5|Page( By BIlal - Farhan )


LAB – Manuals for 2nd
semester .

Array ( Pre – define )


Code 1: programe to show the predefined array values

#include<iostream>
Pre- defined Arryas:
using namespace std;
The array that is defined
int main() and also initialized by the
programmer Which can
{ int i,a[5]={12,13,14,15,16}; never be change by the
user during the execution
for(i=0;i<5;i++) of the program .

cout<<"a["<<i<<"]="<<a[i]<<endl;

return 0;

Output :

6|Page( By BIlal - Farhan )


LAB – Manuals for 2nd
semester .

Array ( User – defined )


Code 2: Program to get array values from user and display
them.

#include<iostream>

using namespace std;

int main()

int i,a[5];

for(i=0;i<5;i++)

{ cout<<"Enter the index ["<<i<<"] values :";

cin>>a[i]; }

for(i=0;i<5;i++)

cout<<"a["<<i<<"]="<<a[i]<<endl;

return 0;

User- defined Arryas:


The array that is not defined and can be initialize by the user at the time of execution of the program
and in this case the user can insert the values of the the arrays by his own thoughts .

7|Page( By BIlal - Farhan )


LAB – Manuals for 2nd
semester .

Output:

Code 3: Program to get index or size of array and its values


at the same thime from the user and display them .

#include<iostream> Note :

using namespace std; “ In this written block of


code the user is adding
int main() the number of array size
fro the program and also
{ adding or given the input
of the array values to the
int i,b,a[b]; program for the
execution ”
cout<<"enter the size of index : ";

8|Page( By BIlal - Farhan )


LAB – Manuals for 2nd
semester .

cin>>b;

for(i=0;i<b;i++)

cout<<"Enter the array ["<<i<<"] values :";

cin>>a[i];

for(i=0;i<b;i++)

cout<<"[a"<<i<<"]="<<a[i]<<endl;

return 0;

Output:

9|Page( By BIlal - Farhan )


LAB – Manuals for 2nd
semester .

Code 4: Program to display all of the even and odd


numbers from the stored values in a pre or user defined
array.

#include<iostream>

using namespace std;

int main()

int i,a[10]={1,2,3,4,5,6,7,8,9,10};

for(i=0;i<10;i++)

{ cout<<"a["<<i<<"]="<<a[i]<<endl;

cout<<"Even numbers are :"<<endl;

for(i=0;i<10;i++)

if(a[i]%2 == 0)

cout<<a[i]<<",";

cout<<"\nodd numbers are :"<<endl;

for(i=0;i<10;i++)

10 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

if(a[i]%2 != 0)

cout<<""<<a[i]<<","; }

Output:

Code 5: Program to get name and the roll numbers of the


student and stor them in arrayes and display them on the
screen in the form of a table .

#include<iostream>

#include<string>

using namespace std;

int main()

11 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

string name[150];

int rn[5],i;

for (i=1;i<=5;i++)

cout<<" Enter the name of student "<<i<<" = ";

cin>>name[i];

cout<<" Enter the Roll no. of "<<name[i]<<" = ";

cin>>rn[i];

cout<<"\n\tName\tRoll no."<<endl;

for (i=1;i<=5;i++)

cout<<"\t"<<name[i]<<"\t"<<rn[i]<<endl;

12 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Output :

Code 6: Program to get the size of array and its values from
user and calculate its sum and product .

#include<iostream>

#include<windows.h>

using namespace std;

int main()

int i,a[5],sum;

for(i=0;i<5;i++)

{
13 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

cout<<"Enter the index ["<<i<<"] values :";

cin>>a[i];

sum*=a[i];

system("cls");

cout<<"Product of all the elements of a[5] is = "<<sum;

return 0;

Output:

Code 7: Program to get values from user continusly until


he/she entered -1 and show the largest number above them .

14 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

#include<iostream>

#include<windows.h>

using namespace std;

int main()

int a,g=0;

while(1)

cout<<"plz enter a positive number (-1 to end ) : ";

cin>>a;

if(a==-1)

break;

if(a>g)

g=a;

cout<<"the greater number is : "<<g;

return 0;

Output:

15 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Code 8 : Program to get the array size and its values at the
same time and display the largest and smaller values stored
in array to the user .

#include<iostream>

using namespace std;

int main()

int n, grt , mini ;

cout<<"enter the size or index of array = ";

cin>>n;

int a[n];

for(int i=0;i<n;i++)

16 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

cout<<"enter the value for a["<<i<<"] = ";

cin>>a[i];

grt=a[0];

mini=a[0];

for(int i=0;i<n;i++)

if(a[i]<mini)

mini=a[i];

if(a[i]>grt)

grt=a[i];

cout<<"maximum value = "<<grt<<endl;

cout<<"minimum value = "<<mini<<endl;

Output:

17 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

18 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Functions

Code 9: Program that use function to get your age and


display in on screen.

#include<iostream>

using namespace std;

int a;

int input()

cout<<"enter your age : ";

cin>>a;

int output()

cout<<"your age is : "<<a; Function calling :

} A function is a block of code the is written


by the user at the starting of the program
int main() by the programmer. It allow the programs
to use a single block of code various time
{ in a single program by help of function
calling .
input();

output(); }
19 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Output:

Code 10: Program to get number of subjects their obtained


and total marks and show the percentage on the screen .

#include<iostream>

using namespace std;

int output(int a)

{cout<<"your avrage percentage is = "<<a<<endl;

int main()

{ double i,b=0,c,ts,tm,d;

cout<<"How many subjects did you inroll = ";

cin>>ts;

cout<<"Enter the total marks = ";

cin>>tm;

20 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

for(i=1;i<=ts;i++)

cout<<"Enter the marks of subject No."<<i<<" = ";

cin>>c;

b=b+c;

d=(b/tm)*100;

output(d);

Output:

21 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Code 11: Program to get the names and roll numbers of the
different student by using arrays and saprate the data of
each student with a line on screen . each task would have to
be performed by using functions.

#include<iostream>

#include<string.h>

using namespace std;

int name();

int line();

int main()

for(int i=1;i<=3;i++)

cout<<"\n"<<i<<".";

name();

line();

int name()

22 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

string a;

cout<<"Enter you name : ";

cin>>a;

cout<<"Your name is : "<<a<<endl;

int line()

cout<<"____________________________________________________
_____"<<endl;

Output:

23 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Code 12 : Write program to take user input for an array


size and values and display it on screen also tell us the count
of negative and positive values of the array and also the
smallest value of the array and perform each task by using
the different function for each .

#include<iostream>

using namespace std;

int b,a[1];

int input();

int output();

int countpositive();

int countnegative();

int smallest();

int main()

input();

output();

countpositive();

countnegative();

24 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

smallest();

int input()

cout<<"enter the size of array = ";

cin>>b; This is a function named


input() to get the array size
and the values of the array
for(int i=0;i<b;i++) from the user .

cout<<"Enter the value to the array a["<<i<<"] = ";

cin>>a[i];

int output() This is a function named output


to display the valued of array
{ saved at the spasific index
for(int i=0;i<b;i++)

cout<<"value of a["<<i<<"] = "<<a[i]<<endl;

25 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

int countpositive()

int p=0;

for(int i=0;i<b;i++) This is a function named count


positive that is used to display the
{ total count of the positive
numbers entered in an array .
if(a[i]>0)

p++;

cout<<"the number of all positive values entered in array =


"<<p<<endl;

int countnegative()

int n=0;
This is a function named count
for(int i=0;i<b;i++) negative that is used to display the
total count of the negative numbers
{
entered in an array .
if(a[i]<0)

n++;

26 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

cout<<"the number of all positive values entered in array =


"<<n<<endl;

int smallest()

{
This is a function named smallest
int s=a[0]; to show the smallest number
from all of the entered values of
for(int i=0;i<b;i++) the array on the screen .
{

if(a[i]<s)

s=a[i];

cout<<"Smallest value = "<<s<<endl;

27 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Output:

28 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Functions (with parameters)


Code 13 : Program to get the marks of 5 subjects from user
by using functions and display its total marks and
percentage also by using saprate function for both.

#include <iostream>

using namespace std;

const int numberOfSubjects = 5;

int subjects[numberOfSubjects];

int totalMarks = 0;

void marks() {

cout << "Enter marks for 5 subjects:\n";

for (int i = 0; i < numberOfSubjects; i++) {

cout << "Subject " << (i + 1) << ": ";

cin >> subjects[i];

if (subjects[i] < 0 || subjects[i] > 100) {

cout << "Invalid marks. Please enter a value between 0 and 100.\
n";

i--;

} else {

totalMarks += subjects[i];
29 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

int main() {

marks();

int maxMarks = numberOfSubjects * 100;

float percentage = (totalMarks / (float)maxMarks) * 100;

cout << "\nTotal Marks: " << totalMarks << "/" << maxMarks << endl;

cout << "Percentage: " << percentage << "%" << endl;

return 0;

Output:

30 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Code 14: Program to calculate the percentage of the students.


#include<iostream>

#include<string.h>

using namespace std;

int percentage ( int total , int a[],int s )

int sum=0;

float per;

for (int i =0;i<s;i++)

sum=sum+a[i];

per=((float)sum/total)*100;

cout<<per;

int main()

string sub[100];

int tm,s;

31 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

cout<<"enter the number of subjects = ";

cin>>s;

int b[s];

cout<<"enter the total marks = ";

cin>>tm;

for(int i=0;i<s;i++)

cout<<"Enter the name of subject "<<i+1<<" = ";

cin>>sub[i];

for(int i=0;i<s;i++)

cout<<"plz enter the obtained numbers of "<<sub[i+1]<<" = ";

cin>>b[i];

percentage( tm, b );

return 1;

32 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Output:

Code 15: Program to find the maximum and minimum value


in an array .

#include<iostream>

using namespace std;

int mini(int a[],int s)

int mini=a[0];

for(int i=0;i<s;i++)

if(a[i]<mini)

mini=a[i];

33 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

cout<<"Minimum value entered in array = "<<mini<<endl;

int max(int a[1],int s)

a[0]=a[s];

int max=a[0];

for(int i=0;i<s;i++)

if(a[i]>max)

max=a[i];

cout<<"Maximum value entered in array = "<<max<<endl;

int main()

int size;

cout<<"Enter the size of an array = ";

cin>>size;

int a[size];

34 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

for(int i=0;i<size;i++)

cout<<"Enter the value of a["<<i<<"] = ";

cin>>a[i];

max(a,size);

mini(a,size)

Output:

35 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Pass by Value
Code 16: Programe to show the concept of pass by value .
#include<iostream>

using namespace std;

void passByValue(int num)

num = num + 10; // Modifying the value

cout << "Inside passByValue function, value: " << num << endl;

int main()

int value1 = 20;

cout << "Original value1 (before passByValue): " << value1 << endl;

passByValue(value1); // Passing by value

cout << "Value1 after passByValue (unchanged): " << value1 << endl;

36 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Output:

Pass by Reference
Code 17: Program that shows the working of pass by value
and pass by reference .

#include<iostream>

using namespace std;

int a(int &c,int e)

c=c+10;

cout<<"the value is : "<<c<<endl;

int main()

37 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

int d=20,b=20;

cout<<"value1 is : "<<d<<endl;

a(d,b);

cout<<"value 1 after pass by value = "<<d<<endl;

cout<<"value2 is : "<<b<<endl;

cout<<"value 1 after pass by refrence value = "<<b<<endl;

Output:

38 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Structure
Code 18 : Program that show case the working of the
structures.
#include<iostream>

#include<string>

using namespace std;

struct datamem{ A structure is a user define data


type that combines the different
int age; variables of different data types
that could be access able only by
string name; using the instant of the structure
int classs; that would be defined by the
structure name .
};

int main()

datamem student;

cout<<"enter your name : ";

cin>>student.name;

cout<<"enter your age : ";

cin>>student.age;

cout<<"enter your class : ";

39 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

cin>>student.classs;

cout<<"Name : "<<student.name<<endl;

cout<<"Age : "<<student.age<<endl;

cout<<"Class : "<<student.classs<<endl;

Output:

Code 19 : Program that show case the working of the


structure’s with functions.
#include<iostream>

#include<string>

using namespace std;

struct datamem{

40 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

int age;

string name;

int classs;

};

void input(int &age,int &classs,string &name)

{ datamem student;

cout<<"enter your name : ";

cin>>student.name;

cout<<"enter your age : ";

cin>>student.age;

cout<<"enter your class : ";

cin>>student.classs;

age = student.age;

classs = student.classs;

name = student.name;

int main()

int a,cl;

string n;

41 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

input(a,cl,n);

cout<<"Name : "<<n<<endl;

cout<<"Age : "<<a<<endl;

cout<<"Class : "<<cl<<endl;

Output:

42 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Classes and objects


Code 20 : Program that show case the working of the classes
and objects.
#include<iostream>

#include<string>

using namespace std;


A class is the blue prints that are
class student{ used to define the different
objects. A class contain the
public: different variables that are called
string name; the data members and the
functions can also be defined
int age; with in the class that are called
the member functions
int clas;
An object is an instant of class
}; that follows or could work under
the constraints given or
int main ()
mentioned in the class.
{ student s1;

s1.name = "bilal";

s1.age = 19;

s1.clas = 14;

cout<<"Name : "<<s1.name<<endl;

cout<<"Age : "<<s1.age<<endl;

cout<<"Class : "<<s1.clas<<endl; }

43 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Output:

Code 21 : Program that show case the working of the access


specifiers in the class.
#include<iostream>

#include<string>

using namespace std;


In this program we are not able to
class student{ access the data member CGPA
because it is private .
private:
To access this data member we
float cgpa; have used the 2 different
functions. first function getcgpa()
public: is used to get the value of cgpa
string name; and void display to display it .

The both functions are public so


int age;
that we can access them easily
int clas; with out any error or restriction .

void getcgpa(){
44 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

cgpa=3.41;

void display(){

cout<<"Cgpa : "<<cgpa<<endl;

}};

int main ()

student s1;

s1.name = "bilal";

s1.age = 19;

s1.clas = 14;

s1.getcgpa();

cout<<"Name : "<<s1.name<<endl;

cout<<"Age : "<<s1.age<<endl;

cout<<"Class : "<<s1.clas<<endl;

s1.display();

45 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Output:

Code 22: Program that show case the working of


constructor.

#include <iostream>

using namespace std;

class MyClass {

public:

MyClass() {

cout << "Constructor is called!" << endl;

}};
A constructor is a a function that is
int main() {
named as same as the class name
MyClass obj; and it has no return type.

return 0; It is usually used to initialize the


private data members .
46 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Output:

Code 23 : Program that show case the working of


destructor.
#include <iostream>

using namespace std;


A destructor a bit like constructer
class MyClass { And it clears the memory
public: automatically when any task is
performed in programe
// Constructor

MyClass() {

cout << "Constructor is called!" << endl;

// Destructor

~MyClass() {

cout << "Destructor is called!" << endl;


47 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

};

int main() {

MyClass obj;

return 0;

Output:

48 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Banck Account
Code 24: Programe of a BANCK ACCOUNT by using the
classes, objects and member functions .
#include<iostream>

#include<string>

#include<windows.h>

using namespace std;

class bankaccount

private:

string accountholdername;

int accountnumber;

double balance;

public:

banckaccount(string name,int accountno,double firstbalance)

accountholdername = name;

accountnumber = accountno;

balance = firstbalance;

};

49 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

void displayAccountInfo()

cout << "Account Holder: " << accountholdername << endl;

cout << "Account Number: " << accountnumber << endl;

cout << "Current Balance: $" << balance << endl;

void deposite(double amount)

if(amount<1)

cout<<"kindly enter the correct ammount"<<endl;

balance += amount;

cout<<"Depositing $"<<amount<<"...."<<endl;

cout<<"Current balance = "<<balance<<endl;

void withdraw(double amount)

if(amount>=balance)

50 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

cout<<"\n\n\n\t\t\t\t\tYou only have "<<balance;

else if(amount<1)

cout<<"kindly enter the correct ammount"<<endl;

else

balance = balance - amount;

cout<<"withdrawing $"<<amount<<"...."<<endl;

cout<<"your curent amount is "<<balance;

};

int main()

{ bankaccount p1;

string name;

int accountn = 0;

double firstbalance , amount1 ;

51 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

cout<<"Enter your name = ";

cin>>name;

cout<<"Set an account number = "<<accountn;

cin>>accountn;

cout<<"enter your first balance = ";

cin>>firstbalance;

p1.banckaccount(name,accountn,firstbalance);

system("cls");

int pin,y=1;

cout<<"kindly enter your account number to access your account ";

cin>>pin;

system("cls");

int e;

if( pin == accountn )

b:

system("cls");

string a;

cout<<"To see your account info plz enter \" 1 \" "<<endl;

52 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

cout<<"To deposit the ammount plz enter \" 2 \" "<<endl;

cout<<"To withdraw the amount plz enter \" 3 \" "<<endl;

cin>>e;

system("cls");

if(e == 1)

p1.displayAccountInfo();

cout<<"\n\n\n\npress any key to go to menu ";

cin>>a;

goto b;

else if (e == 2)

cout<<" Enter the depositing amount = ";

cin>>amount1;

p1.deposite(amount1);

cout<<"\n\n\n\npress any key to go to menu ";

cin>>a;

goto b;

53 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

else if (e == 3)

cout<<" Enter your withdrawing amount = ";

cin>>amount1;

p1.withdraw(amount1);

cout<<"\n\n\n\npress any key to go to menu ";

cin>>a;

goto b;

else

{ for(int i=0;i<2;i++)

cout<<"You have entered incorrect account number \n Plz try again =


";

cin>>pin;

system("cls");

if( pin == accountn )

{ goto b;

54 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

}}}

double x;

cout<<"you have input the account number incorrect 3 times \n\n To


access the account plz enter the exect balance ";

cin>>x;

if(x == firstbalance)

goto b;

} else {

system("cls");

cout<<" \n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t\tSorry ! \n\n\t\


t\t\t\tyou are not able to access the account .....\n\n\n\n\n\n\n\n\n\n\
n\n\n";

return 0;

55 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Out puts :

Firstly the code will get us our general information and then
proceed further.

After entering our info ,it will ask us about the account number
that we have set already and If we didn’t enter the correct
number then it will give us the opportunity to enter the correct
account number 3 times by showing the following massage …..

If we lose our 3 attempts then it will ask us about our exact


ammount for security purposes by showing the msg ….

56 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

If we fail to enter the exact balance then we will not be able to


access the account and code will display the msg………

But if any point we entered the exact required thing to access


the account so that then we will moved to the home page of
account …

By using the instructions of the lay out we will be able to


proceed and to use the account …………..

Home

57 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Depositing

Withdrawing

58 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Inheritance

Inheritance is one of the key features of Object-oriented


programming in C++. It allows us to create a new class (derived
class) from an existing class (base class). The derived class
inherits the features from the base class and can have
additional features of its own.
There are five types of inheritance that are :
1. Single inheritance
2. Multiple inheritance
3. Multi – level inheritance
4. Hierarchical inheritance
5. Hybrid inheritance

Code 25 : Programe to show the inheritance of two classes.

#include <iostream>

#include <string>

using namespace std;

class student {

59 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

public:

void display(){;

cout<<"student 1 "<<endl;

} };

class sub : public student {

public:

void displaysub(){

cout<<"Subject "<<endl;

} };

int main()

sub s;

s.display();

s.displaysub();

Output:

60 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Single inheritance
Code 26 : Programe that get the name and subject of an
object which are the private members of two different
classes and display them on the screenby using the functions
.
#include <iostream>

#include <string>

using namespace std;

class student {

private:

string name;

public:

void getname(string n)

name = n;

void display(){

cout<<"student 1 : "<<name<<endl;

} };

61 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

class sub : public student {

private:

string sub;

public:

void getsub(string s)

sub = s;

void displaysub(){

cout<<"Subject : "<<sub<<endl;

} };

int main()

sub s1;

s1.getname("Bilal Farhan");

s1.getsub("Data Analytics");

s1.display();

s1.displaysub();

62 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Output:

63 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Multiple Inheritance
Code 27 : Programe that shows the working of multiple
inheritance.
#include<iostream>

using namespace std;

class bilal{

public:

void display1(){

cout<<"i am bilal"<<endl;

} };

class usman{

public:

void display2(){

cout<<"I am usman "<<endl;

} };

class hassan{

public:

void display3(){

cout<<"i am hassan "<<endl;

} };

64 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

class Sharfa{

public:

void display4(){

cout<<"i am sharfa"<<endl;

} };

class oop : public bilal,public usman,public hassan,public Sharfa{

public:

void big(){

display1();

display2();

display3();

display4();

cout<<"and we are here for oop"<<endl;

} };

int main()

{ oop d;

d.big();

return 0; }

65 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Output:

Code 28 : Program in which we have create a base class


Person with attributes name and age. Derive a class
Employee from Person, and then derive a class Manager
from Employee. Display all data using the Manager object.

#include<iostream>

#include<string>

using namespace std;

class person{

public:

string name;

int age;

66 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

};

class employee : public person {

public:

void getdata(string n,int a){

name = n;

age = a;

} };

class manager : public employee {

public:

void display(){

cout<<"Name : "<<name<<endl;

cout<<"Age : "<<age<<endl;

} };

int main()

manager m;

m.getdata("Bilal",19);

m.display();

return 0;

67 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Output:

Code 29 : Create a C++ program to demonstrate multiple


inheritance and function conflict resolution using the scope
resolution operator. Define two base classes, Shape and
Circle, each with a display() function. Derive a class Cylinder
from both, and in its display() method, call both base class
functions explicitly. Use an object of Cylinder to show the
output.
#include<iostream>

#include<string>

using namespace std;

class shape{

public:

void display(){

cout<<"Shaps :"<<endl;
68 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

};

class circal{

public:

void display(){

cout<<"1.Circal "<<endl;

};

class cylender : public shape,public circal{

public:

void display(){

shape::display();

circal::display();

cout<<"2.Cylender "<<endl;

};

int main(){

cylender c;

c.cylender::display();

69 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Output:

Code 30 : Write a C++ program to demonstrate multiple


inheritance using two base classes: Engine to display engine
type and Wheels to display wheel type. Derive a class Car
from both and add a method to show car information. In the
main() function, create an object of Car and call all methods
to display complete car details.

#include<iostream>

#include<string>

using namespace std;

class engine {

protected:

string enginetype;

public:

70 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

void input1(string et) {

enginetype = et;

void output1() {

cout << "Engine : " << enginetype << endl;

};

class wheel {

protected:

string wheeltype;

public:

void input2(string wt) {

wheeltype = wt;

void output2() {

cout << "Wheels : " << wheeltype << endl;

};

class car : public engine, public wheel {

public:

71 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

void getcarinfo() {

input1("tyota");

input2("Big pythone style");

void discarinfo(){

output1();

output2();

};

int main() {

car c;

c.getcarinfo();

c.discarinfo();

return 0;

Output:

72 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Code 31 : Write a program using multiple inheritance where


class Person has attributes id and name (with input/display
functions) and class Academic has attributes roll number
and marks[5] (with input/display functions). Create class
Student that inherits both Person and Academic, and has a
function to calculate and display percentage of the five
marks.

#include<iostream>

#include<string>

using namespace std;

class person{

protected:

int id;

string name;

public:

void input(int ID, string n){

id = ID;

name = n;

void output(){

cout<<"Name : "<<name<<endl;

73 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

cout<<"Id : "<<id<<endl;

};

class acadimic{

protected:

int rollnum,totalm;

float m[4];

public:

void input(int rn,int tm){

rollnum = rn;

totalm = tm;

for ( int i = 0 ; i <= 4 ; i++)

cout<<"Enter the marks for sub "<<i+1<<" : ";

cin>>m[i];

void output()

cout<<" Roll number : "<<rollnum<<endl;

74 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

for ( int i = 0 ; i <= 4 ; i++)

cout<<"Marks of Subject "<<i+1<<" : "<<m[i]<<endl;

}}

};

class student : public person, public acadimic{

public:

void displayper()

{ float per;

int sum = 0;

for ( int i = 0 ; i <= 4 ; i++)

sum+=m[i];

per = (sum*100)/totalm;

cout<<"percentage : "<<per<<endl;

};

int main()

75 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

student s1;

s1.acadimic::input(721,500);

s1.person::input(2345,"Bilal");

s1.person::output();

s1.acadimic::output();

s1.displayper();

Output:

76 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Code 32 : Program created by using multiple inheritance


where class Date has attributes day, month, year (with
input/display functions) and class Time has attributes hour,
minute, second (with input/display functions). Create class
DateTime that inherits both Date and Time, and has a
function to input date and time together and display them in
the format “DD-MM-YYYY HH:MM:SS.”

Method 1 :

#include <iostream>

#include <iomanip>

#include <string>

using namespace std;

class date {

protected:

int day, month, year;

void input(int dd, int mm, int yyyy) {

day = dd;

month = mm;

year = yyyy;

}
77 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

void output() {

cout << setfill('0') << setw(2) << day << "-"<< setw(2) << month <<
"-"<< setw(4) << year << " ";

};

class time {

protected:

int hour, min, sec;

void input(int hh, int mm, int ss) {

hour = hh;

min = mm;

sec = ss;

void output() {

cout << setfill('0') << setw(2) << hour << ":"<< setw(2) << min <<
":"<< setw(2) << sec << endl;

};

class datetime : public date, public time {

public:

78 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

void inputtogather() {

date::input(26, 05, 2025);

time::input(06, 35, 23);

void displaytogather() {

date::output();

time::output();

};

int main() {

datetime dt;

dt.inputtogather();

dt.displaytogather();

return 0;

Output:

79 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Method 2 :
#include<iostream>

using namespace std;

class Date {

private:

int day, month, year;

public:

void input() {

cout << "Enter Day: ";

cin >> day;

cout << "Enter Month: ";

cin >> month;

cout << "Enter Year: ";

cin >> year;

void display() {

if(day < 10) cout << "0";

cout << day;

cout << "-";

80 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

if(month < 10) cout << "0";

cout << month;

cout << "-";

cout << year;

};

class Time {

private:

int hour, minute, second;

public:

void input() {

cout << "Enter Hour: ";

cin >> hour;

cout << "Enter Minute: ";

cin >> minute;

cout << "Enter Second: ";

cin >> second;

void display() {

81 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

cout << " ";

if(hour < 10) cout << "0";

cout << hour;

cout << ":";

if(minute < 10) cout << "0";

cout << minute;

cout << ":";

if(second < 10) cout << "0";

cout << second;

};

class DateTime : public Date, public Time {

public:

void inputAll() {

Date::input();

Time::input();

void displayAll() {

cout << "\n--- Date and Time ---\n";

82 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Date::display();

Time::display();

cout << endl;

};

int main() {

DateTime dt;

dt.inputAll();

dt.displayAll();

return 0;

Output:

83 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Multi – level Inheritance


Code 33 : Programe that shows the working of multi - level
inheritance.
#include<iostream>

using namespace std;

class students{

public:

void display(){

cout<<"i am from parent class "<<endl;

} };

class department : public students{


84 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

public:

void display2(){

cout<<"i can act like both parent and child"<<endl;

} };

class rollnum : public department{

private:

void display3(){

cout<<"i am a child class "<<endl;

public:

void display4(){

display3();

} };

int main (){

rollnum d1;

d1.display();

d1.display2();

d1.display4();

return 0; }

85 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Output:

Code 34 : program to demonstrate the concept of multi-


level inheritance. Create a base class Vehicle with speed-
related functions. Derive a class Car from Vehicle, which
adds the number of doors and a function to display car
details. Then, derive a class Sedan from Car, which adds
trunk capacity and overrides the display function to include
all relevant details. The program should clearly show the
inheritance of data and functions across the levels.

#include<iostream>

using namespace std;

class vehicle{

public:

int speed;

void displayspeed(){
86 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

cout<<"Speed:"<<speed<<"km/h"<<endl;

};

class Car:public vehicle{

public:

int doors;

void cardetails(){

cout<<"Doors:"<<doors<<endl;

};

class Sedan:public Car{

public:

int trunk_capacity;

void displaysedan(){

cout<<"Trunk Capacity:"<<trunk_capacity<<endl;

void display(){

displayspeed();

cardetails();

87 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

displaysedan();

};

int main(){

Sedan S;

S.speed=180;

S.doors=4;

S.trunk_capacity=500;

S.display();

Output:

88 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Code 35 : . Create a base class Person with a function to


display a message. Derive a class Student from Person that
adds its own display function. Then create a class Graduate
derived from Student, with an additional function to display
graduate-specific information. In the main() function, create
an object of the Graduate class and demonstrate the
inheritance of functions from all levels.

#include <iostream>

using namespace std;

class person{

public:

void personinfo(){

cout<< "My name is BIlal"<<endl;

};

class student : public person {

public:

void studentinfo(){

cout<<"I am a student of data analytics"<<endl;

};

89 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

class graduate:public student{

public:

void graduationinfo(){

cout<<"My graduation will be completed in 2028"<<endl;

};

int main(){

graduate g;

g.personinfo();

g.studentinfo();

g.graduationinfo();

return 0;

Output:

90 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Code 36 : Write a class Creature with attribute speciesName


(with input/display functions). Create class Mammal that
inherits Creature and adds attribute hasFur (with
input/display functions). Create class Dog that inherits
Mammal and adds attribute breed (with input/display
functions) and a member function bark() that displays
“Woof! Woof!” In main, input and display a Dog’s species, fur
status, breed, and barking.
#include<iostream>

#include<string>

using namespace std;

class creature{

private:

string speciesname;

public:

void input(string name){

speciesname = name;

void display(){

cout<<"Species name : "<<speciesname<<endl;

};

91 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

class mammal : public creature{

public:

bool hasfur;

void input(bool y)

hasfur = y;

void display()

if(hasfur=1) cout<<"It has fur"<<endl;

else cout<<"It has no fur"<<endl;

};

class dog : public mammal {

public:

string breed;

void input(string b){

breed = b ;

void display(){

92 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

cout<<"bread : "<<breed<<endl;

void bark(){

cout<<"Woof! Woof!"<<endl;

};

int main()

dog d;

d.creature::input("Dog");

d.mammal::input(1);

d.dog::input("Husky");

d.creature::display();

d.mammal::display();

d.dog::display();

d.bark();

93 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Output:

Code 37 : Write a class Device with attribute powerState


(on/off) and member functions turnOn() and turnOff() to
change and display the power state. Create class Computer
that inherits Device and adds attributes processor and ram
(with input/display functions). Create class Laptop that
inherits Computer and adds attributes batteryLife and
weight (with input/display functions) and a member
function showInfo() that turns on the device, displays
processor, ram, batteryLife, weight, then turns off the device.

#include<iostream>

#include<string>

using namespace std;

class device{

public:

bool powerstate;
94 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

void turnon(){

powerstate = 1;

cout<<"Device is on "<<endl;

void turnoff(){

powerstate = 0;

cout<<"Device is off "<<endl;

};

class computer : public device {

public:

int ram;

string processor;

void input(int r,string p){

ram = r;

processor = p;

void display(){

cout<<"Ram : "<<ram<<" GB "<<endl;

95 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

cout<<"Processor : "<<processor<<endl;

};

class laptop : public computer{

public:

int batterylife;

double weight;

void input(int bl,double w){

batterylife = bl;

weight = w;

void display(){

cout<<"Battery Life : "<<batterylife<<" % "<<endl;

cout<<"Weight : "<<weight<<" Kg "<<endl;

void showinfo(){

computer::display();

laptop::display();

};

96 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

int main ()

laptop l;

l.turnon();

l.computer::input(16,"12-ps-2354");

l.laptop::input(90,12.5);

l.showinfo();

Output:

97 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Code 38 : Create a class Country with country name and


currency. Derive a class Province and then a class City.
Display complete location details in the City class using
multilevel inheritance.

#include<iostream>

#include<string>

using namespace std;

class country{

protected:

string countryname = "Pakistan",crrency = "Rupees";

void display(){

cout<<"Country name : "<<countryname<<endl;

cout<<"Crrency : "<<crrency<<endl;

};

class province : public country {

protected:

string name = "Panjab";

98 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

void display(){

cout<<"Province : "<<name<<endl;

};

class city : public province {

protected:

string cname = "Faisalabad";

public:

void display(){

cout<<"City name : "<<cname<<endl;

void addressinfo(){

country::display();

province::display();

city::display();

} };

int main ()

city c;

c.addressinfo(); }

99 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Output:

Code 39 : Create a class Country with country name and


currency. Derive a class Province and then a class City.
Display complete location details in the City class using
multilevel inheritance.

#include<iostream>

#include<string>

using namespace std;

class country{

protected:

string countryname = "Pakistan",crrency = "Rupees";

void display(){

cout<<"Country name : "<<countryname<<endl;

cout<<"Crrency : "<<crrency<<endl;

};
100 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

class province : public country {

protected:

string name = "Panjab";

void display(){

cout<<"Province : "<<name<<endl;

};

class city : public province {

protected:

string cname = "Faisalabad";

public:

void display(){

cout<<"City name : "<<cname<<endl;

void addressinfo(){

country::display();

province::display();

city::display();

};

101 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

int main ()

city c;

c.addressinfo();

Output:

102 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Hierarchical inheritance

Code 40 : program demonstrating hierarchical inheritance


where a base class Animal contains a common attribute
name. Derive two child classes Dog and Cat from Animal,
each with their own unique attributes (breed for Dog and
color for Cat). Implement member functions to set and
display the details of both Dog and Cat objects.

#include <iostream>

#include<string>

using namespace std;

class animal{

public:

void displayc(){

cout<<"Info of Cat"<<endl;

void displayd(){

cout<<"Info of Dog"<<endl;

};

class cat : public animal {

103 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

public:

string color;

void getcolor(){

color = " Grey";

void catinfo(){

cout<<"Color of Cat :"<<color<<endl;

};

class dog : public animal {

public:

string breed;

void getbreed(){

breed = "Huskey";

void doginfo(){

cout<<"Breed of Dog : "<<breed<<endl;

};

int main(){

104 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

cat c;

dog d;

c.displayc();

c.getcolor();

c.catinfo();

d.displayd();

d.getbreed();

d.doginfo();

Output:

Code 41 : Program using hierarchical inheritance with base


class Course containing course name and code. Derive
classes Theory (adds credits and teacher) and Lab (adds
equipment list). Input and display data for both classes.

#include <iostream>

#include<string>

105 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

using namespace std;

class course{

public:

string coursename,coursecode;

void getcourse(){

coursename = " OOP";

coursecode = " OOP - 1123";

void displaycourse(){

cout<<"Course name : "<<coursename<<endl;

cout<<"Course code : "<<coursecode<<endl;

};

class theory : public course {

public:

string teachername;

int credit;

void gettheory(){

teachername = " Rabia Shahid";

credit = 4;

106 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

void displayteory(){

cout<<"Teacher name : "<<teachername<<endl;

cout<<"Credit hours : "<<credit<<endl;

};

class lab : public course {

public:

string labiq;

void getlab(){

labiq = " Laptop";

void displaylab(){

cout<<"Lab equipments : "<<labiq<<endl;

};

int main(){

theory t;

lab l;

t.getcourse();

107 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

t.displaycourse();

t.gettheory();

t.displayteory();

l.getlab();

l.displaylab();

Output:

108 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

Code 42 : Creating a program using hierarchical inheritance


with base class Appliance containing brand name. Derive
classes Washer (adds capacity) and Refrigerator (adds
volume). Input and display data for both classes.

#include <iostream>

#include<string>

using namespace std;

class aplince{

public:

string aplincename;

void getapplince(){

aplincename = " Dawlence";

void displayaplince(){

cout<<"Applince name : "<<aplincename<<endl;

};

class Washer : public aplince {

public:

int capacity;

109 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

void getwasher(){

capacity = 4;

void displaywasher(){

cout<<"washer Capacity : "<<capacity<<endl;

};

class refrigerator : public aplince {

public:

int volume;

void getvolume(){

volume = 10;

void displayvolume(){

cout<<"Refrigerator volume : "<<volume<<endl;

};

int main(){

refrigerator r;

Washer w;

110 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

w.getapplince();

w.displayaplince();

w.getwasher();

w.displaywasher();

r.getvolume();

r.displayvolume();

Output:

Code 43 : Creating a program using hierarchical inheritance


with a base class Vehicle containing the brand name. Derive
classes Car (adds number of doors) and Bike (adds bike
type). Input and display data for both using appropriate
functions.

#include <iostream>

using namespace std;

class vhicle{

public:
111 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

string brand;

void getbrand(){

brand = "Suzuki";

void displayvahicle(){

cout<<"Brand : "<<brand<<endl;

};

class car : public vhicle {

public:

int doors;

void getcar(){

doors = 4;

void displaycar(){

cout<<"Car info"<<endl;

cout<<"Doors : "<<doors<<endl;

};

class bike : public vhicle {

112 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

public:

string biketype;

void getbike(){

biketype = "heavy bike";

void displaybike(){

cout<<"Bike info"<<endl;

cout<<"Bike type : "<<biketype<<endl;

};

int main(){

car c;

bike b;

c.getcar();

c.getbrand();

c.displaycar();

c.displayvahicle();

b.getbike();

b.getbrand();

b.displaybike();

113 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

b.displayvahicle(); }

Output:

Code 44 : Creating a program using hierarchical inheritance


where a base class Person has a common function
showDetails(). Derive two classes Student and Teacher from
Person, each with their own specific functions. Create
objects of both derived classes and demonstrate access to
both base and derived class functions.

#include <iostream>

using namespace std;

class person{

public:

void displayperson(){

cout<<"My name is Bilal Farhan"<<endl;

114 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

};

class student : public person {

public:

void displaystudent(){

cout<<"I am a student at GCUF"<<endl;

};

class teacher : public person {

public:

void displayteacher(){

cout<<"I also give the home tutions"<<endl;

};

int main(){

student s;

teacher t;

s.displayperson();

s.displaystudent();

t.displayperson();

115 | P a g e ( B y B I l a l - F a r h a n )
LAB – Manuals for 2nd
semester .

t.displayteacher();

Output:

116 | P a g e ( B y B I l a l - F a r h a n )

You might also like