0% found this document useful (0 votes)
18 views34 pages

Vinay Oops

Uploaded by

raj
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)
18 views34 pages

Vinay Oops

Uploaded by

raj
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/ 34

BTCS202N Object Oriented Programming

Experiment : 1
1. TITLE: Object Oriented Programming.

2. OUTCOMES:
 Describe OOPs concepts.
 Use functions and pointers in your C++ program.
 Understand tokens, expressions, and control structure.
 Explain arrays and strings and create programs using them.
 Describe and use constructors and destructors.
 Understand and employ file management.
 Demonstrate how to control errors with exception handling.
3. OBJECTIVE:
 Introduction to Object Oriented Programming.
 Difference between Object Oriented Programming and Procedural Oriented
Programming.
 Concept of Object Oriented Programming.
 Advantages of Object Oriented Programming over Procedural Oriented
Programming.

4 . THEORY:

Introduction to Object Oriented Programming: As the name suggests,


Object-Oriented Programming or OOPs refers to languages that use objects in
programming. Object-oriented programming aims to implement real-world entities
like inheritance, hiding, polymorphism, etc in programming. The main aim of OOP
is to bind together the data and the functions that operate on them so that no other
part of the code can access this data except that function.

Difference between Object Oriented Programming & Procedural


Oriented Programming:

Procedural Oriented Programming Object Oriented Programming

In object-oriented programming, the


In procedural programming, the program is
program is divided into small parts
divided into small parts called functions.
called objects.

Procedural programming follows a top- Object-oriented programming follows


down approach. a bottom-up approach.

There is no access specifier in procedural Object-oriented programming has access


programming. specifiers like private, public, protected,

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

Procedural Oriented Programming Object Oriented Programming

etc

Adding new data and functions is not easy. Adding new data and function is easy.

Procedural programming does not have


Object-oriented programming provides
any proper way of hiding data so it is less
data hiding so it is more secure.
secure.

In procedural programming, overloading is Overloading is possible in object-oriented


not possible. programming.

In object-oriented programming, the


In procedural programming, there is no
concept of data hiding and inheritance is
concept of data hiding and inheritance.
used.

In procedural programming, the function is In object-oriented programming, data is


more important than the data. more important than function.

Procedural programming is based on Object-oriented programming is based on


the unreal world. the real world.

Procedural programming is used for Object-oriented programming is used for


designing medium-sized programs. designing large and complex programs.

Procedural programming uses the concept Object-oriented programming uses the


of procedure abstraction. concept of data abstraction.

Code reusability absent in procedural Code reusability present in object-


programming, oriented programming.

Examples: C, FORTRAN, Pascal, Basic,


Examples: C++, Java, Python, C#, etc.
etc.

Object-Oriented Programming Concepts:


 Class
 Objects
 Data Abstraction
 Encapsulation

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

 Inheritance
 Polymorphism
 Dynamic Binding
 Message Passing

1. Class:
A class is a user-defined data type. It consists of data members and member
functions, which can be accessed and used by creating an instance of that class. It
represents the set of properties or methods that are common to all objects of one
type. A class is like a blueprint for an object.  
For Example: Consider the Class of Cars. There may be many cars with different
names and brands but all of them will share some common properties like all of
them will have 4 wheels, Speed Limit, Mileage range, etc. So here, Car is the class,
and wheels, speed limits, mileage are their properties.

2. Object: 
It is a basic unit of Object-Oriented Programming and represents the real-life
entities. An Object is an instance of a Class. When a class is defined, no memory is
allocated but when it is instantiated (i.e. an object is created) memory is allocated.
An object has an identity, state, and behavior. Each object contains data and code to
manipulate the data. Objects can interact without having to know details of each
other’s data or code, it is sufficient to know the type of message accepted and type
of response returned by the objects. 
For example:“Dog” is a real-life Object, which has some characteristics like Color,
Breed, Bark, Sleep, and Eats.

Object
3. Data Abstraction: 
Data abstraction is one of the most essential and important features of object-
oriented programming. Data abstraction refers to providing only essential
information about the data to the outside world, hiding the background details or
implementation. Consider a real-life example of a man driving a car. The man only
knows that pressing the accelerators will increase the speed of the car or applying
brakes will stop the car, but he does not know about how on pressing the accelerator
the speed is increasing, he does not know about the inner mechanism of the car or
the implementation of the accelerator, brakes, etc in the car. This is what abstraction
is.

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

4. Encapsulation: 
Encapsulation is defined as the wrapping up of data under a single unit. It is the
mechanism that binds together code and the data it manipulates. In Encapsulation,
the variables or data of a class are hidden from any other class and can be accessed
only through any member function of their class in which they are declared. As in
encapsulation, the data in a class is hidden from other classes, so it is also known
as data-hiding.

Consider a real-life example of encapsulation, in a company, there are different


sections like the accounts section, finance section, sales section, etc. The finance
section handles all the financial transactions and keeps records of all the data related
to finance. Similarly, the sales section handles all the sales-related activities and
keeps records of all the sales. Now there may arise a situation when for some reason
an official from the finance section needs all the data about sales in a particular
month. In this case, he is not allowed to directly access the data of the sales section.
He will first have to contact some other officer in the sales section and then request
him to give the particular data. This is what encapsulation is. Here the data of the
sales section and the employees that can manipulate them are wrapped under a
single name “sales section”.

5. Inheritance: 
Inheritance is an important pillar of OOP(Object-Oriented Programming). The
capability of a class to derive properties and characteristics from another class is
called Inheritance. When we write a class, we inherit properties from other classes.
So when we create a class, we do not need to write all the properties and functions
again and again, as these can be inherited from another class that possesses it.
Inheritance allows the user to reuse the code whenever possible and reduce its
redundancy.

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

6. Polymorphism: 
The word polymorphism means having many forms. In simple words, we can define
polymorphism as the ability of a message to be displayed in more than one form.
For example, A person at the same time can have different characteristics. Like a
man at the same time is a father, a husband, an employee. So the same person posses
different behavior in different situations. This is called polymorphism.

7. Dynamic Binding:
In dynamic binding, the code to be executed in response to the function call is
decided at runtime. Dynamic binding means that the code associated with a given
procedure call is not known until the time of the call at run time.  Dynamic Method
Binding One of the main advantages of inheritance is that some derived class D has
all the members of its base class B. Once D is not hiding any of the public members
of B, then an object of D can represent B in any context where a B could be used.
This feature is known as subtype polymorphism.
8. Message Passing:
It is a form of communication used in object-oriented programming as well as
parallel programming. Objects communicate with one another by sending and
receiving information to each other. A message for an object is a request for
execution of a procedure and therefore will invoke a function in the receiving object

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

that generates the desired results. Message passing involves specifying the name of
the object, the name of the function, and the information to be sent.

Advantage Over Procedural Oriented Programming:-

 In Procedural Oriented Programming the code reusability feature is not present


while, In Object Oriented Programming using Inheritance we can make code
reusable.
 In Object Oriented Programming we use Polymorphism to allows the object to
behave differently in different conditions while, In Procedural Oriented
Programming we cannot use this.
 Object Oriented Programming makes development and maintenance easier.
 Object Oriented Programming provides data hiding.
 Object Oriented Programming provide ability to simulate real-world event much
more effectively.
 Object Oriented Programming is faster and easier to execute
 Object Oriented Programming provides a clear structure for the programs
 Object Oriented Programming helps to keep the code DRY "Don't Repeat
Yourself".
 Object Oriented Programming makes it possible to create full reusable
applications with less code and shorter development time.
 Object Oriented Programming gives flexibility through polymorphism.
 Object Oriented Programming gives better productivity.

5. IMPLEMENTATION:
The concept of OOP in C++ can be implemented through a tool found in the
language called ‘Class’ , therefore we should have knowledge of classes. A class is
a group of same type of object or simply we can say that classes are the collection of
object. Object-oriented programming aims to implement real-world entities like
inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to
bind together the data and the functions that operate on them so that no other part of
the code can access this data except that function.

6. LAB ASSIGNMENT:

4. Write a program which asks for


length and width of a rectangle
and calculates the area and
5. perimeter.

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

6. Write a program which asks for


length and width of a rectangle
and calculates the area and
7. perimeter.
Write a program to enter your name and enrollment number in C++.

#include<iostream.h>
void main()
{
cout<<”My name is Saharsh Bhartiya”;
cout<<”\n My Enrollment Number is 22100BTCSE11745”;
}
7. QUIZ AND VIVA QUESTION
 What is oops?
 Why use oops?
 What are main features of oops?
 What is difference between object and class?
 What are limitations of oops?

Experiment : 2
1. TITLE:- Basic Structure of C++
2. OUTCOMES:- Understanding Basic Structure of C++
3. OBJECTIVE:- Study of Basic Structure of C++
4. THEORY:- The C++ program is written using a specific template structure. The
structure of the program written in C++ language is as follows:

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

Documentation Section:-
 This section comes first and is used
to document the logic of the
program that the programmer
going to code.

 It can be also used to write for


purpose of the program.

 Whatever written in the


documentation section is the
comment and is not compiled
by the compiler.

Header Files:
Generally, a program includes various programming elements like built-in functions,
classes, keywords, constants, operators, etc. that are already defined in the standard C++
library.
In order to use such pre-defined elements in a program, an appropriate header must be
included in the program.
Standard headers are specified in a program through the preprocessor directive #include.
In Figure, the iostream header is used. When the compiler processes the instruction
#include<iostream>, it includes the contents of the stream in the program. This enables
the programmer to use standard input, output, and error facilities that are provided only
through the standard streams defined in <iostream>. These standard streams process data
as a stream of characters, that is, data is read and displayed in a continuous flow. The
standard streams defined in <iostream> are listed here.
#include<iostream>
Namespaces:
 A namespace permits grouping of various entities like classes, objects, functions, and
various C++ tokens, etc. under a single name.

 Any user can create separate namespaces of its own and can use them in any other
program.

 In the below snippets, namespace std contains declarations for cout, cin, endl, etc.
statements.

 using namespace std;

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

Namespaces can be accessed in multiple ways:


 using namespace std;

 using std :: cout;

Definition Section:
 It is used to declare some constants and assign them some value.

 In this section, anyone can define your own datatype using primitive data types.

 In #define is a compiler directive which tells the compiler whenever the message is
found to replace it with “Factorial\n”.

 typedef int INTEGER; this statement tells the compiler that whenever you will
encounter INTEGER replace it by int and as you have declared INTEGER as datatype
you cannot use it as an identifier.

Global Declaration Section:


 Here, the variables and the class definitions which are going to be used in the program
are declared to make them global.

 The scope of the variable declared in this section lasts until the entire program
terminates.

 These variables are accessible within the user-defined functions also.

Function Declaration Section:


 It contains all the functions which our main functions need.

 Usually, this section contains the User-defined functions.

 This part of the program can be written after the main function but for this, write the
function prototype in this section for the function which for you are going to write
code after the main function.

Main Function:

 The main function tells the compiler where to start the execution of the program. The
execution of the program starts with the main function.
 All the statements that are to be executed are written in the main function.
 The compiler executes all the instructions which are written in the curly braces {}
which encloses the body of the main function.
 Once all instructions from the main function are executed, control comes out of the
main function and the program terminates and no further execution occur.

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

5. QUIZ AND VIVA:-


 What is use of Document Section?
 What is use of Header Files?
 What is use of Namespace?
 What is use of Definition Section?
 What is use of Global Declaration Section?
 What is use of Function Declaration Section?
 What is use of Main Function?

EXPERIMENT : 3
1. TITLE: Hello World.
2. OUTCOMES: Hello World will be printed on screen.
3. OBJECTIVE: Write a program to print statement in C++ ‘‘Hello World”.

4. IMPLEMENTATION:

#include<iostream.h>
void main()
{
cout<<‘‘Hello World”;
}

5. OUTPUT:

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

6. LAB ASSIGNMENT:
 What is difference between C and C++?

7. QUIZ AND VIVA QUESTIONS:


 What are keywords?
 What is use of cout function?

EXPERIMENT : 4
1. TITLE: Addition of two number.
2. OUTCOMES: Sum of the two number will be printed on our screen.
3. OBJECTIVE: Write a C++ program to print addition of two number .

4. IMPLEMENTATION:

#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"Enter num1:";
cin>>a;
cout<<"Enter num2:";
cin>>b;

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

cout<<"The sum of two number num1 & num2 is:";


cout<<a+b;
return 0;
}

5. OUTPUT:

6. LAB ASSIGNMENT:
 What is use of cin function?
 What are datatypes?

7. QUIZ AND VIVA QUESTION:


 What are Operators?
 What are Variables?

EXPERIMENT : 5
1. TITLE: Print marks of subject Maths, Physics, Chemistry.
2. OUTCOMES: Maths=90, Physics=74, Chemistry=76 will be printed
on our screen.
3. OBJECTIVE: Write a program to display the following output using a
single cout :Maths=90, Physics=74, Chemistry=76.

4. IMPLEMENTATION:

#include<iostream>
using namespace std;
int main()
{
cout<<"Maths = 90 "<<endl<<"Physics = 74 "<<endl<<"Chemistry = 76";

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

return 0;
}

5. OUTPUT:

6. LAB ASSIGNMENT:
 What is use of endl?
 What are datatypes?

7. QUIZ AND VIVA QUESTION:


 How to print new line in C++?

EXPERIMENT : 6
1. TITLE: Display larger value.
2. OUTCOMES: Larger value will be printed on our screen.
3. OBJECTIVE: Write a program to read two numbers from keyboard
and display the larger value on the screen.

4. IMPLEMENTATION:
#include<iostream>
using namespace std;
void large(int a ,int b){
if(a>b){
cout<<a<<" is Greater than "<<b;
}
else{
cout<<b<<" is Greater than "<<a;

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

}
}
int main()
{
int x,y;
cout<<"Enter two numbers for compare"<<endl;
cin>>x>>y;
large(x,y);
return 0;
}
5. OUTPUT:

6. LAB ASSIGNMENT:
 What is use of if , else?
 What are parameters ?

7. QUIZ AND VIVA QUESTION:


 What are functions?
 What is function calling?

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

EXPERIMENT : 7
1. TITLE: Call by value & Call by reference .
2. OUTCOMES: Swapping will be done using Call by value & Call by
reference.
3. OBJECTIVE: Write a function using reference variables as arguments
to swap the values of a pair of integers.
4. IMPLEMENTATION:
For Call by value
#include<iostream>
using namespace std;
void change(int data);
int main()
{
int data = 3;
change(data);
cout << "Value of the data is: " << data<< endl;
return 0;
}
void change(int data)
{
data = 5;
}

For call by reference


#include<iostream>
using namespace std;
void swap(int *x, int *y)
{
int swap;
swap=*x;
*x=*y;
*y=swap;
}
int main()
{
int x=500, y=100;
swap(&x, &y); // passing value to function
cout<<"Value of x is: "<<x<<endl;
cout<<"Value of y is: "<<y<<endl;
return 0;
}
5. OUTPUT:
For call by value

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

For call by reference

6. LAB ASSIGNMENT:
 What is Call by value?
 What is Call by reference?

7. QUIZ AND VIVA QUESTION:


 What is actual parameters?
 What is formal parameters?

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

EXPERIMENT : 11

TITLE: Write a program to implement the concept of inheritance and its types
OUTCOMES:
Inheritance and its program and types of it are understood using the programs that’ll
displayed on screen
Types of inheritance:-
 Single level inheritance and its program
 Multiple level inheritance and its program
 Hierarchical inheritance and its program
 Multi-level inheritance and its program
 Hybrid inheritance and its program

OBJECTIVE: Write in detail about inheritance and its types with program and output
IMPLEMENTATION:

CPP INHERITANCE

In C++, inheritance is a process in which one object acquires all the properties and
behaviours of its parent object automatically. In such way, you can reuse, extend or modify
the attributes and behaviours which are defined in other class.

In C++, the class which inherits the members of another class is called derived class and the
class whose members are inherited is called base class. The derived class is the specialized
class for the base class.

The capability of a class to derive properties and characteristics from another class is called
Inheritance. Inheritance is one of the most important features of Object-Oriented
Programming.

Inheritance is a feature or a process in which, new classes are created from the existing
classes. The new class created is called “derived class” or “child class” and the existing class
is known as the “base class” or “parent class”. The derived class now is said to be inherited
from the base class.

When we say derived class inherits the base class, it means, the derived class inherits all the
properties of the base class, without changing the properties of base class and may add new
features to its own. These new features in the derived class will not affect the base class. The
derived class is the specialized class for the base class.

Sub Class: The class that inherits properties from another class is called Subclass or Derived
Class.

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

Super Class: The class whose properties are inherited by a subclass is called Base Class or
Superclass.

ADVANTAGES OF CPP INHERITANCE

Code reusability: Now you can reuse the members of your parent class. So, there is no need
to define the member again. So less code is required in the class.

Why and when to use inheritance?

Consider a group of vehicles. You need to create classes for Bus, Car, and Truck. The
methods fuelAmount(), capacity(), applyBrakes() will be the same for all three classes. If
we create these classes avoiding inheritance then we have to write all of these functions in
each of the three classes as shown below figure:

You can clearly see that the above process results in duplication of the same code 3 times.
This increases the chances of error and data redundancy. To avoid this type of situation,
inheritance is used. If we create a class Vehicle and write these three functions in it and
inherit the rest of the classes from the vehicle class, then we can simply avoid the
duplication of data and increase re-usability. Look at the below diagram in which the three
classes are inherited from vehicle class:

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

Derived Classes: A Derived class is defined as the class derived from the base class.

Syntax:

class <derived_class_name> : <access-specifier> <base_class_name>

//body

Where class — keyword to create a new class

derived_class_name — name of the new class, which will inherit the base class

access-specifier — either of private, public or protected. If neither is specified, PRIVATE


is taken as default

base-class-name — name of the base class

Note: A derived class doesn’t inherit access to private data members. However, it does
inherit a full parent object, which contains any private members which that class declares.

Example:

1. class ABC : private XYZ //private derivation

{ }

2. class ABC : public XYZ //public derivation

{ }

3. class ABC : protected XYZ //protected derivation

{ }

4. class ABC: XYZ //private derivation by default

{ }

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

CODE:-
#include<iostream>
using namespace std;
class Person
{
int id;
char name[100];

public:
void set_p()
{
cout<<"Enter the Id:";
cin>>id;
fflush(stdin);
cout<<"Enter the Name:";
cin.get(name,100);
}

void display_p()
{
cout<<endl<<id<<"\t"<<name<<"\t";
}
};

class Student: private Person


{
char course[50];
int fee;

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

public:
void set_s()
{
set_p();
cout<<"Enter the Course Name:";
fflush(stdin);
cin.getline(course,50);
cout<<"Enter the Course Fee:";
cin>>fee;
}

void display_s()
{
display_p();
cout<<course<<"\t"<<fee<<endl;
}
};

main()
{
Student s;
s.set_s();
s.display_s();
return 0;
}

Output:

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

Enter the Id: 101

Enter the Name: Dev

Enter the Course Name: GCS

Enter the Course Fee:70000


101 Dev GCS 70000

Types Of Inheritance:-
C++ supports five types of inheritance:

 Single inheritance

 Multiple inheritance

 Hierarchical inheritance

 Multilevel inheritance
 Hybrid inheritance

C++ Single Inheritance:-

Single inheritance is defined as the inheritance in which a derived class is inherited


from the only one base class.
Where 'A' is the base class, and 'B' is the derived class.

When one class inherits another class, it is known as single level inheritance. Let's
see the example of single level inheritance which inherits the fields only.
CODE:-

#include <iostream>

using namespace std;

class Animal {

public:

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

void eat() {

cout<<"Eating..."<<endl;

};

class Dog: public Animal

public:

void bark(){

cout<<"Barking...";

};

int main(void) {

Dog d1;

d1.eat();

d1.bark();

return 0;
}
OUTPUT:-
Eating...
Barking...

C++ Multi-level Inheritance:-


Multilevel inheritance is a process of deriving a class from another
derived class.

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

When one class inherits another class which is further inherited by another class, it
is known as multi level inheritance in C++. Inheritance is transitive so the last
derived class acquires all the members of all its base classes. Let's see the example
of multi level inheritance in C++.

CODE:-

#include <iostream>

using namespace std;

class Animal {

public:

void eat() {

cout<<"Eating..."<<endl;

};

class Dog: public Animal

public:

void bark(){

cout<<"Barking..."<<endl;

};

class BabyDog: public Dog

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

public:

void weep() {

cout<<"Weeping...";

};

int main(void) {

BabyDog d1;

d1.eat();

d1.bark();

d1.weep();

return 0;

OUTPUT:-
Eating...
Barking...
Weeping...

C++ Multiple Inheritance:-

Multiple inheritance is the process of deriving a new class that inherits


the attributes from two or more classes.

SYNTAX:-

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

class D : visibility B-1, visibility B-2, ?

// Body of the class;

CODE:-

#include <iostream>

using namespace std;

class A

protected:

int a;

public:

void get_a(int n)

a = n;

};

class B

protected:

int b;

public:

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

void get_b(int n)

b = n;

};

class C : public A,public B

public:

void display()

std::cout << "The value of a is : " <<a<< std::endl;

std::cout << "The value of b is : " <<b<< std::endl;

cout<<"Addition of a and b is : "<<a+b;

};

int main()

C c;

c.get_a(10);

c.get_b(20);

c.display();

return 0;

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

OUTPUT:-
The value of a is : 10
The value of b is : 20
Addition of a and b is : 30

C++ Hybrid Inheritance:-

Hybrid inheritance is a combination of more than one type of inheritance.

Code:-

#include <iostream>

using namespace std;

class A

protected:

int a;

public:

void get_a()

std::cout << "Enter the value of 'a' : " << std::endl;

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

cin>>a;

};

class B : public A

protected:

int b;

public:

void get_b()

std::cout << "Enter the value of 'b' : " << std::endl;

cin>>b;

};

class C

protected:

int c;

public:

void get_c()

std::cout << "Enter the value of c is : " << std::endl;

cin>>c;

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

};

class D : public B, public C

protected:

int d;

public:

void mul()

get_a();

get_b();

get_c();

std::cout << "Multiplication of a,b,c is : " <<a*b*c<< std::endl;

};

int main()

D d;

d.mul();

return 0;

OUTPUT:-
Enter the value of 'a' :10
Enter the value of 'b' : 20
Enter the value of c is : 30
Multiplication of a,b,c is : 6000

C++ Hierarchical Inheritance:-

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

Hierarchical inheritance is defined as the process of deriving more than one class
from a base class.

Syntax:-

class A

// body of the class A.

class B : public A

// body of class B.

class C : public A

// body of class C.

class D : public A

// body of class D.

CODE:-

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

#include <iostream>

using namespace std;

class Shape // Declaration of base class.

public:

int a;

int b;

void get_data(int n,int m)

a= n;

b = m;

};

class Rectangle : public Shape // inheriting Shape class

public:

int rect_area()

int result = a*b;

return result;

};

class Triangle : public Shape // inheriting Shape class

public:

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

int triangle_area()

float result = 0.5*a*b;

return result;

};

int main()

Rectangle r;

Triangle t;

int length,breadth,base,height;

std::cout << "Enter the length and breadth of a rectangle: " << std::endl;

cin>>length>>breadth;

r.get_data(length,breadth);

int m = r.rect_area();

std::cout << "Area of the rectangle is : " <<m<< std::endl;

std::cout << "Enter the base and height of the triangle: " << std::endl;

cin>>base>>height;

t.get_data(base,height);

float n = t.triangle_area();

std::cout <<"Area of the triangle is : " << n<<std::endl;

return 0;

OUTPUT:-
Enter the length and breadth of a rectangle:
23

22100BTCSE11853 VINAY JAIN


BTCS202N Object Oriented Programming

20
Area of the rectangle is : 460
Enter the base and height of the triangle:
2
5
Area of the triangle is : 5

22100BTCSE11853 VINAY JAIN

You might also like