Vinay Oops
Vinay Oops
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:
etc
Adding new data and functions is not easy. Adding new data and function is easy.
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.
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.
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.
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
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.
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:
#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:
Documentation Section:-
This section comes first and is used
to document the logic of the
program that the programmer
going to code.
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.
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.
The scope of the variable declared in this section lasts until the entire program
terminates.
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.
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:
6. LAB ASSIGNMENT:
What is difference between C and C++?
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;
5. OUTPUT:
6. LAB ASSIGNMENT:
What is use of cin function?
What are datatypes?
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";
return 0;
}
5. OUTPUT:
6. LAB ASSIGNMENT:
What is use of endl?
What are datatypes?
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;
}
}
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 ?
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;
}
6. LAB ASSIGNMENT:
What is Call by value?
What is Call by reference?
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.
Super Class: The class whose properties are inherited by a subclass is called Base Class or
Superclass.
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.
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:
Derived Classes: A Derived class is defined as the class derived from the base class.
Syntax:
//body
derived_class_name — name of the new class, which will inherit 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:
{ }
{ }
{ }
{ }
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";
}
};
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:
Types Of Inheritance:-
C++ supports five types of inheritance:
Single inheritance
Multiple inheritance
Hierarchical inheritance
Multilevel inheritance
Hybrid inheritance
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>
class Animal {
public:
void eat() {
cout<<"Eating..."<<endl;
};
public:
void bark(){
cout<<"Barking...";
};
int main(void) {
Dog d1;
d1.eat();
d1.bark();
return 0;
}
OUTPUT:-
Eating...
Barking...
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>
class Animal {
public:
void eat() {
cout<<"Eating..."<<endl;
};
public:
void bark(){
cout<<"Barking..."<<endl;
};
public:
void weep() {
cout<<"Weeping...";
};
int main(void) {
BabyDog d1;
d1.eat();
d1.bark();
d1.weep();
return 0;
OUTPUT:-
Eating...
Barking...
Weeping...
SYNTAX:-
CODE:-
#include <iostream>
class A
protected:
int a;
public:
void get_a(int n)
a = n;
};
class B
protected:
int b;
public:
void get_b(int n)
b = n;
};
public:
void display()
};
int main()
C c;
c.get_a(10);
c.get_b(20);
c.display();
return 0;
OUTPUT:-
The value of a is : 10
The value of b is : 20
Addition of a and b is : 30
Code:-
#include <iostream>
class A
protected:
int a;
public:
void get_a()
cin>>a;
};
class B : public A
protected:
int b;
public:
void get_b()
cin>>b;
};
class C
protected:
int c;
public:
void get_c()
cin>>c;
};
protected:
int d;
public:
void mul()
get_a();
get_b();
get_c();
};
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
Hierarchical inheritance is defined as the process of deriving more than one class
from a base class.
Syntax:-
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:-
#include <iostream>
public:
int a;
int b;
a= n;
b = m;
};
public:
int rect_area()
return result;
};
public:
int triangle_area()
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 << "Enter the base and height of the triangle: " << std::endl;
cin>>base>>height;
t.get_data(base,height);
float n = t.triangle_area();
return 0;
OUTPUT:-
Enter the length and breadth of a rectangle:
23
20
Area of the rectangle is : 460
Enter the base and height of the triangle:
2
5
Area of the triangle is : 5