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

OOPS Object-Oriented Programming

The document is a certificate issued to Mitesh Mahesh Adelkar for successfully completing all practical work in the subject of Object-Oriented Programming Language as part of an Information Technology course. It lists Mitesh's name, roll number, and the name of the college and confirms that the practical examination was conducted by the University of Mumbai. The certificate is signed by the subject in charge and head of department.

Uploaded by

Rae Test
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

OOPS Object-Oriented Programming

The document is a certificate issued to Mitesh Mahesh Adelkar for successfully completing all practical work in the subject of Object-Oriented Programming Language as part of an Information Technology course. It lists Mitesh's name, roll number, and the name of the college and confirms that the practical examination was conducted by the University of Mumbai. The certificate is signed by the subject in charge and head of department.

Uploaded by

Rae Test
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Name: Mitesh Mahesh Adelkar Roll No: 01

Enroll No:

Satish Pradhan Dnyanasadhana College


Thane
Certificate
Mitesh Mahesh Adelkar
This is to certify that Mr./Miss.: ______________________________________

of F. Y. B. Sc. Information Technology (Semester-II) Class has successfully


completed all the practical work in subject Object-Oriented Programming
Language, under the guidance of Asst. Prof. Sayali Karmode (subject in
charge) during Year 2020-21 in partial fulfillment of Information Technology
Practical Examination conducted by University of Mumbai.

______________________
Subject in charge Head of Department

12-04-2021
Date__________________
Name: Mitesh Mahesh Adelkar Roll No: 01

INDEX:

SR.NO. CONTENT DATE

1. PRACTICAL NO. 1

A] Write a program to display Hello World. 18-1-2021

B] Write a program to add two numbers. 18-1-2021

C] Write a program to add two numbers by taking input from user. 19-1-2021

D] Write a program to display different data types using cout in C++ 19-1-2021

2. PRACTICAL NO. 2

A] Implementation of class and object to display students name, roll


no. and marks.

30-1-2021
B] Write a program to call a function using object name.

3. PRACTICAL NO. 3

A] Write a program to pass an object as an argument to the function. 4-2-2021

B] Write a program to return object from a function. 4-2-2021

C] Implementation of friend class. 9-2-2021

D] Implementation of friend function. 9-2-2021


Name: Mitesh Mahesh Adelkar Roll No: 01

4. PRACTICAL NO. 4

A] Implementation scope resolution operator. 13-2-2021

B] Implementation of Inline function. 13-2-2021

5. PRACTICAL NO. 5

A] Implementation of default constructer. 22-2-2021

B] Implementation of parameterized constructer. 22-2-2021

C] Implementation of copy constructer. 27-2-2021

6. PRACTICAL NO. 6

A] Implementation of function overloading. 2-3-2021

B] Implementation of function overriding. 2-3-2021

7. PRACTICAL NO. 7

A] Implementation of virtual function.

B] Implementation of pure virtual function. 8-3-2021


Name: Mitesh Mahesh Adelkar Roll No: 01

8. PRACTICAL NO. 8

A] Implementation of this pointer. 16-3-2021

B] Implementation of static function. 16-3-2021

9. PRACTICAL NO. 9

A] Implementation single inheritance. 20-3-2021

B] Implementation multiple inheritance. 20-3-2021

C] Implementation multilevel inheritance. 22-3-2021

D] Implementation hierarchical inheritance. 22-3-2021

E] Write a program to remove ambiguity in multiple inheritance. 23-3-2021

10. PRACTICAL NO. 10

A] Write a program to implement function template. 31-3-2021


Name: Mitesh Mahesh Adelkar Roll No: 01

Practical no 1: (A) Write a program to display the message Hello, World!

Aim: To write a program to display the message Hello, World! using the cout function and
print it on the output screen.

Software requirements: Coding C++

Program:

#include<iostream>
using namespace std;

int main()
{

cout<<"Hello,World!"<<endl;
return 0;
}

Output:

Conclusion: Thus we have successfully written and executed the program of hello world.
This program will display the message Hello, World! on the output screen.
Name: Mitesh Mahesh Adelkar Roll No: 01

Practical no 1: (B) Write a program to add two numbers

Aim: To write a program to perform addition of two numbers in C++ and display the result
on the output screen.

Software requirements: Coding C++

Program:

#include<iostream>
using namespace std;
int main()
{
int a = 45;
int b = 50;
cout<< "Sum is:" <<a+b<<endl;
return 0;
}

Output:

Conclusion: Thus we have successfully written and executed the program of addition of two
numbers. In this program two variables a and b are assigned values and addition will be
performed and the result will be displayed on the output screen using the cout function.
Name: Mitesh Mahesh Adelkar Roll No: 01

Practical no 1: (C) Write a program to add two numbers by taking input from
the user.

Aim: To write a program to perform addition of two numbers by taking the input from the
user and display the result on the output screen.

Software requirements: Coding C++

Program:

#include<iostream>
using namespace std;
int main()
{
int a , b, c;
cout<< "Enter two numbers:";
cin>> a >> b;
c = a + b;
cout<< "Sum is:" << c <<endl;
return 0;
}

Output:

Conclusion: Thus we have successfully written and executed the program to perform addition
of two numbers by taking the input from the user. The two values will be taken as an input
and addition operation will be performed on them and then the result will be displayed on the
output screen.
Name: Mitesh Mahesh Adelkar Roll No: 01

Practical no 1: (D) Write a program to display different data types using cout.

Aim: To write a program to display different data types such as integer, character, float etc.
on the output screen using the cout function.

Software requirements: Coding C++

Program:

#include<iostream>
using namespace std;
int main()
{
int i = 500;
char c = 'A';
float f = 40.50;

cout<< "Integer value: " <<i<<endl;


cout<< "Char Value: " << c <<endl;
cout<< "Float value: " << f <<endl;
return 0;
}

Output:

Conclusion: Thus we have successfully written and executed the program to display different
types of data types in C++. In this program we have created different variables with different
data types and have assigned the values accordingly, then we have displayed the data stored
in the variables using the cout function and the program is executed.
Name: Mitesh Mahesh Adelkar Roll No: 01

Practical no 2: (A) Implementation of class and objects to display students


name, roll no and marks.

Aim: To write a program to display the student’s name, roll no and marks on the output
screen by using class and objects (class name = students).

Software requirements: Coding C++

Program:

#include<iostream>
#include<string>
using namespace std;

class students
{
public:
string name;
int rollno;
int marks;
};

int main()
{
students a,b,c,d,e;

a.name="mitesh";
a.rollno= 01;
a.marks=50;

b.name="kitesh";
b.rollno= 02;
b.marks= 45;
Name: Mitesh Mahesh Adelkar Roll No: 01

c.name="Ritesh";
c.rollno= 03;
c.marks= 67;

d.name="Smitesh";
d.rollno= 04;
d.marks= 34;

e.name="Pritesh";
e.rollno= 05;
e.marks= 55;

cout<<"name: "<<a.name<<endl;
cout<<"roll no: "<<a.rollno<<endl;
cout<<"marks obtained: "<<a.marks<<endl;

cout<<"name: "<<b.name<<endl;
cout<<"roll no: "<<b.rollno<<endl;
cout<<"marks obtained: "<<b.marks<<endl;

cout<<"name: "<<c.name<<endl;
cout<<"roll no: "<<c.rollno<<endl;
cout<<"marks obtained: "<<c.marks<<endl;

cout<<"name: "<<d.name<<endl;
cout<<"roll no: "<<d.rollno<<endl;
cout<<"marks obtained: "<<d.marks<<endl;
Name: Mitesh Mahesh Adelkar Roll No: 01

cout<<"name: "<<e.name<<endl;
cout<<"roll no: "<<e.rollno<<endl;
cout<<"marks obtained: "<<e.marks<<endl;

return 0;
}

Output:

Conclusion: Thus we have successfully written and executed the program to implement class
and objects to display students data such as name, roll no and marks. We have created a class
named students which contains 3 public data members name, rollno and marks and have
accessed those data members by creating the objects a, b, c, d and e. and have displayed the
data using the cout statements on the output screen.
Name: Mitesh Mahesh Adelkar Roll No: 01

Practical no 2: (B) Write a program to call a function using object name.

Aim: To write a program to call the member function of a class by using the object name and
using the dot operator (.)

Software requirements: Coding C++

Program:

#include<iostream>
using namespace std;

class hello
{
public:
void display()
{
cout<<"Hello World!"<<endl;
}
};
int main()
{
hello Obj1;
Obj1.display ();
return 0;
}
Output:

Conclusion: Thus we have successfully written and executed the program to call the member
function of the class using the object. In this program we have called the member function
Name: Mitesh Mahesh Adelkar Roll No: 01

display() by using the object Obj1 and dot operator( . ). When the function is called inside the
main() function then the codes written inside the function body are executed.

Practical no 3: (A) Write a program to pass object as an argument to the


function.

Aim: To write a program to pass objects as an argument to the function

Software requirements: Coding C++

Program:

#include <iostream>
using namespace std;
class A
{
public:
int n=100;
char ch='A';
void display(A a)
{
cout<<a.n<<endl;
cout<<a.ch<<endl;
}
};
int main()
{
A obj;
obj.display(obj);
return 0;
}
Name: Mitesh Mahesh Adelkar Roll No: 01

Output:

Conclusion: Thus we have successfully written and executed the program to pass objects as
an argument to the function. In this program we have created a member function display(A a)
in which ‘A’ is the class name and ‘a’ is the object name, so to call the function we need to
pass the object of class A as an argument, and when the function will be called the codes
inside the display() function will be executed. And thus the program gets successfully
executed.

Practical no 3: (B) Write a program to return object from a function

Aim: To write a program to return an object from a function.

Software requirements: Coding C++

Program:

#include<iostream>
#include<string>
using namespace std;

class Student
{
public:
int Id;
int Age;
string Name;
Name: Mitesh Mahesh Adelkar Roll No: 01

Student input(int n, int a, string s)


{
Student obj;
obj.Id = n;
obj.Age = a;
obj.Name = s;
return obj;
}
void disp(Student obj)
{
cout<<"Name: "<<obj.Name<<endl;
cout<<"Id: "<<obj.Id<<endl;
cout<<"Age: "<<obj.Age<<endl;
}
};

int main()
{
Student s1;

s1 = s1.input(01, 18, "Kitesh");


s1.disp(s1);
return 0;
}

Output:
Name: Mitesh Mahesh Adelkar Roll No: 01

Conclusion: Thus we have successfully written and executed the program to return an object
from a function. In this program we have created a function input(int n, int a, string s) which
takes the input from the user when we call the function, when we call the function we need to
pass parameters accordingly. And then the input() function stores the values passed in the
objects and returns them to s1. Next when we call the disp() function by passing object name
s1 as a parameter then the values which were given as inputs will be displayed on the output
screen and the program gets executed.

Practical no 3: (C) Implementation of friend class

Aim: To write a program to create a friend class and access the private data members of the
base class and display them using a function of friend class.

Software requirements: Coding C++

Program:

#include <iostream>
using namespace std;

class XYZ
{
private:
char ch='A';
int num = 11;

public:
friend class ABC;
};

class ABC
Name: Mitesh Mahesh Adelkar Roll No: 01

{
public:
void disp(XYZ obj)
{
cout<<obj.ch<<endl;
cout<<obj.num<<endl;
}
};

int main()
{
ABC obj;
XYZ obj2;

obj.disp(obj2);
return 0;
}

Output:

Conclusion: Thus we have successfully written and executed the program to write a program
to create a friend class. A friend class can access private and protected members of a class for
which it is declared as a friend. It can be useful to allow a particular class to access private
members of other class. Here in this example class ABC is a friend class of class XYZ and
thus it can access all the private and protected members of the XYZ class and display them
using the objects and thus the program gets executed.
Name: Mitesh Mahesh Adelkar Roll No: 01

Practical no 3: (D) Implementation of Friend function

Aim: To write a program to create a friend function and access the private data members of a
class for which it is declared as a friend and display them on the output screen.

Software requirements: Coding C++

Program:

#include <iostream>
using namespace std;

class XYZ
{
private:
int num=100;
char ch='A';

public:
friend void disp(XYZ obj);
};

void disp(XYZ obj)


{
cout<<obj.num<<endl;
cout<<obj.ch<<endl;
}

int main()
{
XYZ obj;
disp(obj);
return 0;
}
Name: Mitesh Mahesh Adelkar Roll No: 01

Output:

Conclusion: Thus we have successfully written and executed the program to create a friend
function. In this program we have created a class XYZ and have declared the friend function
disp(). A friend function is a function which is declared outside the class scope but it has the
right to access all the private and protected members of the class, friend functions are not
member functions but it works like one. Thus we have successfully implementer the friend
function and have accessed the private data members and have displayed them on the output
screen.

Practical no 4: (A) Implementation of scope resolution operator

Aim: To write a program to implement the scope resolution operator to call the global
variable with the same name as local variable, and display their values using cout function.

Software requirements: Coding C++

Program:

#include<iostream>
using namespace std;
int num = 30;

int main()
{
int num = 10;
Name: Mitesh Mahesh Adelkar Roll No: 01

cout<< "Value of global num is " << ::num;


cout<< "\nValue of local num is " <<num;

return 0;
}

Output:

Conclusion: Thus we have successfully written and executed the program to implement the
scope resolution operator. In this program the name of the global as well as the name of the
local variable is same (num) so to avoid the errors in the program we need to use the scope
resolution operator (::) to access the global variable. And thus the values of the global
variables and the local variable are successfully displayed on the output screen.

Practical no 4: (B) Implementation of Inline functions

Aim: To write a program to implement inline functions

Software requirements: Coding C++

Program:

#include <iostream>
using namespace std;

inline void displayNum(int num)


{
cout<<num<<endl;
Name: Mitesh Mahesh Adelkar Roll No: 01

int main()
{
// first function call
displayNum(5);

// second function call


displayNum(8);

// third function call


displayNum(666);

return 0;
}

Output:

Conclusion: Thus we have successfully written and executed the program to implement inline
functions. In this program we have created an inline function named displayNum() that takes
a single integer as a parameter. We then called the function 3 times in the main() function
with different arguments. Each time displayNum() is called, the compiler copies the code of
the function to that call location. Inline functions are defined by using the inline keyword and
when they are called the compiler copies the function to the location of the function call in
compile-time and may make the program execution faster.
Name: Mitesh Mahesh Adelkar Roll No: 01

Practical no 5: (A) Implementation of default constructor

Aim: To write a program to create a default constructor for a class and to create an object to
call the constructor.

Software requirements: Coding C++

Program:

#include <iostream>
using namespace std;
class Wall
{
private:
double length;
public:
Wall()
{
length = 5.5;
cout<< "Creating a wall." <<endl;
cout<< "Length = " << length <<endl;
}
};
int main()
{
Wall wall1;
return 0;
}

Output:
Name: Mitesh Mahesh Adelkar Roll No: 01

Conclusion: Thus we have successfully written and executed the program to implement the
default constructor. In this program when the wall1 object is created, the Wall() constructor is
called. This sets the length variable of the object to 5.5. The constructor is automatically
called whenever an object is created and as there are no parameters in wall() constructor it is
called as default constructor.

Practical no 5: (B) Implementation of parameterized constructor

Aim: To write a program to create a parameterized constructor for the class wall to initialize
its data members and then calculate area of the wall and display the result on the screen.

Software requirements: Coding C++

Program:

#include <iostream>
using namespace std;

class wall
{
private:
double length;
double height;
public:

wall(double a, double b)
{
length = a;
height = b;
}

double calculateArea()
Name: Mitesh Mahesh Adelkar Roll No: 01

{
return length * height;
}
};

int main()
{
wall obj1(10, 20);
wall obj2(8.5, 6.3);

cout<< "Area of Wall 1: " << obj1.calculateArea() <<endl;


cout<< "Area of Wall 2: " << obj2.calculateArea() <<endl;

return 0;
}

Output:

Conclusion: Thus we have successfully written and executed the program to implement the
parameterized constructor. Here, we have created a parameterized constructor Wall() that has
two parameters: double a and double b. The values contained in these parameters are used to
initialize the member variables length and height. When we create an object of the Wall class,
we pass the values for the member variables as arguments. With the member variables thus
initialized, we can now calculate the area of the wall with the calculateArea() function. It is
called as parameterized constructor because it consists of parameters.
Name: Mitesh Mahesh Adelkar Roll No: 01

Practical no 5: (C) Implementation of copy constructor

Aim: To write a program to create and implement copy constructor.

Software requirements: Coding C++

Program:

#include <iostream>
using namespace std;

class wall
{
private:
double length;
double height;
public:

wall(double a, double b)
{
length = a;
height = b;
}
/*Copy constructor*/
wall(wall &obj)
{
length = obj.length;
height = obj.height;
}

double calculateArea()
{
return length * height;
}
Name: Mitesh Mahesh Adelkar Roll No: 01

};

int main()
{
wall obj1(20, 10);
cout<< "Area of Wall 1: " << obj1.calculateArea() <<endl;

wall obj2 = obj1;


cout<< "Area of Wall 2: " << obj2.calculateArea() <<endl;

return 0;
}

Output:

Conclusion: Thus we have successfully written and executed the program to implement the
copy constructor. In this program, we have used a copy constructor to copy the contents of
one object of the Wall class to another. We assign the values of the variables of the first
object to the corresponding variables of the second object. This is how the contents of the
object are copied, in main() function we create two objects wall1 and wall2 and then copy the
contents of the first object to the second with the code: wall obj2 = obj1; and thus the
program gets executed.
Name: Mitesh Mahesh Adelkar Roll No: 01

Practical no 6: (A) Implementation of function overloading

Aim: To write a program to implement function overloading

Software requirements: Coding C++

Program:

#include <iostream>
using namespace std;

int sum(int a, int b)


{
return a + b;
}

double sum(double a, double b)


{
return a + b;
}

int sum(int a ,int b, int c)


{
return a + b + c;
}

int main()
{
cout<<"Sum 1 = "<< sum(57, 62)<<endl;
cout<<"Sum 2 = "<< sum(53.75, 67.61)<<endl;
cout<<"Sum 3 = "<< sum(56, 65, 77)<<endl;

return 0;
}
Name: Mitesh Mahesh Adelkar Roll No: 01

Output:

Conclusion: Thus we have successfully written and executed the program to implement
function overloading. In this program we have created 3 different sum() functions with
different parameters (number/type of parameters). And, based on the arguments passed
during a function call, a particular sum() is called. It's a compile-time polymorphism because
the compiler knows which function to execute before the program is compiled.

Practical no 6: (B) Implementation of function overriding

Aim: To write a program to perform function overriding by declaring one function as a


virtual function

Software requirements: Coding C++

Program:

#include <iostream>
using namespace std;

class Base
{
public:
virtual void display()
{
cout<< "Base Class Function" <<endl;
Name: Mitesh Mahesh Adelkar Roll No: 01

}
};

class Derived : public Base


{
public:
void display()
{
cout<< "Derived Class Function" <<endl;
}
};
int main()
{
Derived obj;
obj.display();
return 0;
}

Output:

Conclusion: Thus we have successfully written and executed the program to perform function
overriding. In this program we have used a display() function in the Base class and the same
function in the Derived class. When we call display() using the Derived object obj, it
overrides the display() function of Base by executing the derived() function of
the Derived class. To perform overriding we need to declare the base class function as virtual.
It's a runtime polymorphism because the function call is not resolved by the compiler, but it is
resolved in the runtime instead.
Name: Mitesh Mahesh Adelkar Roll No: 01

Practical no 7: (A) Implementation of Virtual functions

Aim: To write a program to implement virtual function, to override the base class function by
using the object of the derived class.

Software requirements: Coding C++

Program:

#include <iostream>
using namespace std;

class Base
{
public:
virtual void disp()
{
cout<<"Base Class Function"<<endl;
}
};

class Derived : public Base


{
public:
void disp()
{
cout<<"Derived Class Function"<<endl;
}
};

int main()
{
Derived derived1;
Name: Mitesh Mahesh Adelkar Roll No: 01

/*pointer of Base type that points to derived1*/


Base* base1 = &derived1;

/*calls member function of Derived class*/


base1->disp();

return 0;

Output:

Conclusion: Thus we have successfully written and executed the program to implement
virtual functions. In this program we have used a virtual function disp() in the Base class to
ensure that it is overridden by the disp() function in the Derived class. In the main() function
we have created a pointer to the derived class object and then have called the disp() function
by using the base class object. Using virtual functions in the base class ensures that the
function can be overridden.
Name: Mitesh Mahesh Adelkar Roll No: 01

Practical no 7: (B) Implementation of pure virtual functions

Aim: To write a program using pure virtual function.

Software requirements: Coding C++

Program:

#include<iostream>
using namespace std;

class Shape
{
protected:
float dimension;
public:
void getdata()
{
cin>>dimension;
}
/*Pure Virtual Function Creation*/
virtual float area()=0;
};
/*Derived Classes*/
class Square : public Shape
{
public:
float area()
{
return dimension*dimension;
}
};

class Circle : public Shape


Name: Mitesh Mahesh Adelkar Roll No: 01

{
public:
float area()
{
return 3.14*dimension*dimension;
}
};

int main()
{
Square square;
Circle circle;

cout<<"Enter the length of the square:";


square.getdata();
cout<<"Area of square:"<<square.area()<<endl;

cout<<"\nEnter radius of the circle:";


circle.getdata();
cout<<"Area of circle:"<<circle.area()<<endl;

return 0;
}

Output:
Name: Mitesh Mahesh Adelkar Roll No: 01

Conclusion: Thus we have successfully written and executed the program to implement pure
virtual functions. In this program we have created a pure virtual function area(). Pure virtual
functions are the functions which do not have any use in the base class but the function must
be implemented in all of its derived classes. In this program we will take the dimension from
the user through the getdata() function and then calculate the areas of the shapes by using the
redefined area() functions by using the class names (square and circle) and dot operator. It is
necessary to implement the pure virtual function in all of its derived classes.

Practical no 8: (A) Implementation of This pointer

Aim: To write a program to implement this pointer in C++

Software requirements: Coding C++

Program:

#include<iostream>
using namespace std;

class mymax
{
private:
int num;
char ch;
public:
void set(int num, char ch)
{
this->num = num;
this->ch = ch;
}
void disp()
{
Name: Mitesh Mahesh Adelkar Roll No: 01

cout<<num<<endl;
cout<<ch<<endl;
}
};
int main()
{
mymax obj1;

obj1.set(3200,'H');
obj1.disp();
return 0;
}

Output:

Conclusion: Thus we have successfully written and executed the program of this pointer.
Here you can see that we have two data members num and ch. In member function set() we
have two local variables having same name as data members name. In such case if you want
to assign the local variable value to the data members then you won’t be able to do until
unless you use this pointer, because the compiler won’t know that you are referring to
object’s data members unless you use this pointer. We generally use this pointer when the
names of two variables are same.
Name: Mitesh Mahesh Adelkar Roll No: 01

Practical no 8: (B) Implementation of Static function

Aim: To write a program to implement static function by calling it using class name.

Software requirements: Coding C++

Program:

#include<iostream>
using namespace std;
class fun
{
public:
static void display()
{
cout<<"This is static function"<<endl;
}
};
int main()
{
/*Calling member function using class name
and scope resolution operator*/

fun::display();
}

Output:
Name: Mitesh Mahesh Adelkar Roll No: 01

Conclusion: Thus we have successfully written and executed the program of static function.
A static member function can be called even if no objects of the class exist and the static
functions are accessed using only the class name and the scope resolution operator (::)
Thus we have used the statement fun::display(); and hence the static function gets called and
program gets executed.

Practical no 9: (A) Implementation of Single Inheritance

Aim: To write a program to show single inheritance.

Software requirements: Coding C++

Program:

#include <iostream>
using namespace std;

class Base
{
public:

void baseDisp()
{
cout<< "This is a base class function" <<endl;
}
};

class Derived:public Base


{
public:
void derivedDisp()
{
Name: Mitesh Mahesh Adelkar Roll No: 01

cout<< "This is derived class function" <<endl;


}
};

int main()
{
Derived obj1;

obj1.baseDisp();
obj1.derivedDisp();

return 0;
}

Output:

Conclusion: Thus we have successfully written and executed the program of single
inheritance. In this program we have created one base class and one derived class, in single
inheritance there is only one derived class inherited from only one base class. The derived
class can access all the data members and member functions of the base class and thus we can
call the functions of base class using the objects of derived class.
Name: Mitesh Mahesh Adelkar Roll No: 01

Practical no 9: (B) Implementation of Multiple Inheritance

Aim: To write a program to show multiple inheritance.

Software requirements: Coding C++

Program:

#include <iostream>
using namespace std;

class first
{
public:

first()
{
cout<<"This is statement 1."<<endl;
}
};

class second
{
public:

second()
{
cout<<"This is statement 2."<<endl;
}
};

class third : public first, public second


{
public:
Name: Mitesh Mahesh Adelkar Roll No: 01

third()
{
cout<<"This is statement 3."<<endl;
}
};

int main()
{
third obj1;

return 0;
}

Output:

Conclusion: Thus we have successfully written and executed the program of multiple
inheritance. A class can be derived from more than one parent or base class.
In this example, class third is derived from base classes first and second. So the class three
can inherit all the properties of both base classes. Thus when an object for class third is
created it calls the constructor of every base class and the output is generated.
Name: Mitesh Mahesh Adelkar Roll No: 01

Practical no 9: (C) Implementation of Multi-level Inheritance

Aim: To write a program to show Multi-level inheritance.

Software requirements: Coding C++

Program:

#include <iostream>
using namespace std;

class one
{
public:
void display()
{
cout<<"This is a function in class one"<<endl;
}
};

class two : public one


{
//codes
};

class three : public two


{
//codes
};

int main()
{
three obj1;
Name: Mitesh Mahesh Adelkar Roll No: 01

obj1.display();

return 0;
}

Output:

Conclusion: Thus we have successfully written and executed the program of Multi-level
inheritance. In this program, class three is derived from class two (which is derived from base
class one). The obj1 object of class three is defined in the main() function. When the display
function is called using the object of class three it gets executed because multiple inheritance
To say simply, class two is derived from class one, and class three is derived from class two,
so class three can access both class two and class one data members and member functions.
And thus the display() function gets successfully called using obj of class three.

Practical no 9: (D) Implementation of Hierarchical Inheritance

Aim: To write a program to show hierarchical inheritance.

Software requirements: Coding C++

Program:

#include <iostream>
using namespace std;
Name: Mitesh Mahesh Adelkar Roll No: 01

class A
{
public:
int x, y;

void getdata()
{
cout<< "\n\nEnter value of x and y:";
cin>> x >> y;
}
};

class B : public A
{
public:
void product()
{
cout<< "\nProduct= " << x * y;
}
};

class C : public A
{
public:
void sum()
{
cout<< "\nSum= " << x + y;
}
};

int main()
{
B obj1;
Name: Mitesh Mahesh Adelkar Roll No: 01

C obj2;

obj1.getdata();
obj1.product();

obj2.getdata();
obj2.sum();

return 0;
}

Output:

Conclusion: Thus we have successfully written and executed the program of Hierarchical
inheritance. In this example, there is only one base class A from which two classes B and C
are derived. Both derived class have their own members as well as base class members. The
product is calculated in the derived class B, whereas, the sum is calculated in the derived
class C but both use the values of x and y from the base class. In the main function we use the
function getdata() to take input from the user and then call the other function to perform the
calculations using the objects created and thus the program gets executed.
Name: Mitesh Mahesh Adelkar Roll No: 01

Practical no 9: (E) Write a program to remove ambiguity in multiple inheritance

Aim: To write a program to show how to remove the ambiguity in multiple inheritance when
the name of base functions are same.

Software requirements: Coding C++

Program:

#include<iostream>
using namespace std;

class first
{
public:
void test()
{
cout<<"Class First function"<<endl;
}
};

class second
{
public:
void test()
{
cout<<"Class Second function"<<endl;
}
};

class third : public first, public second


{
public:
int a, b;
Name: Mitesh Mahesh Adelkar Roll No: 01

};

int main()
{
third obj;

obj.first :: test();
obj.second :: test();

return 0;
}

Output:

Conclusion: Thus we have successfully written and executed the program to remove the
ambiguity in multiple inheritance. In this example, the two base classes first and second have
function of same name test() which are inherited to the derived class c. When the object of
the class c is created and call the function test () then the compiler is confused which base’s
class function is called by the compiler. So we use the scope resolution operator to remove
the ambiguity obj.first :: test(); Using this we can remove the ambiguity in multiple
inheritance
Name: Mitesh Mahesh Adelkar Roll No: 01

Practical no10: ( ) Write a program to implement function template

Aim: To write a program to show the working of the function template.

Software requirements: Coding C++

Program:

#include<iostream>
using namespace std;

template<typename T>
T myMax(T x, T y)
{
return (x>y)? x:y;
}

int main()
{
cout<<myMax(30,46)<<endl;
cout<<myMax(30.45,30.47)<<endl;
cout<<myMax('A','a')<<" has higher ASCII value"<<endl;

return 0;
}

Output:
Name: Mitesh Mahesh Adelkar Roll No: 01

Conclusion: Thus we have successfully written and executed the program of function
template. In this program we have created a template myMax which returns the higher value
based on two inputs. Based on the type of data given as inputs the templates will be framed
accordingly. For example, when we pass the values (30, 46) while calling the function then
the T in the formal parameters will be changed to int because the int data types are used as
parameters. We can create a single function to work with different data types.

You might also like