0% found this document useful (0 votes)
10 views7 pages

OOPs Concepts in C++

The document explains Object Oriented Programming (OOP) concepts in C++, including classes, objects, abstraction, encapsulation, inheritance, and polymorphism. It provides examples of how to create and manipulate classes and objects, demonstrating their properties and behaviors. The document emphasizes the advantages of OOP in simplifying complex problem-solving through the use of objects that interact with each other.

Uploaded by

MarieFernandes
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)
10 views7 pages

OOPs Concepts in C++

The document explains Object Oriented Programming (OOP) concepts in C++, including classes, objects, abstraction, encapsulation, inheritance, and polymorphism. It provides examples of how to create and manipulate classes and objects, demonstrating their properties and behaviors. The document emphasizes the advantages of OOP in simplifying complex problem-solving through the use of objects that interact with each other.

Uploaded by

MarieFernandes
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/ 7

OOPs Concepts in C++

Object oriented programming is a way of solving complex problems by breaking them


into smaller problems using objects. Before Object Oriented Programming
(commonly referred as OOP), programs were written in procedural language, they
were nothing but a long list of instructions. On the other hand, the OOP is all about
creating objects that can interact with each other, this makes it easier to develop
programs in OOP as we can understand the relationship between them.

Object Oriented Programming (OOP)


In Object oriented programming we write programs using classes and objects utilizing
features of OOPs such as abstraction, encapsulation, inheritance and polymorphism
1. Class and Objects
A class is like a blueprint of data member (variables) and member functions and
object is an instance of class. For example, lets say we have a class Car which has
data members (variables) such as speed, weight, price and functions such as
gearChange(), slowDown(), brake() etc. Now lets say I create a object of this class
named FordFigo which uses these data members and functions and give them its own
values. Similarly we can create as many objects as we want using the blueprint (class).
//Class name is Car
class Car
{
//Data members
char name[20];
int speed;
int weight;

public:
//Functions
void brake()
{
}
void slowDown()
{
}
};
int main()
{
//ford is an object
Car ford;
}
That is, Object is any entity that has state (data member/variables) and behavior
(member functions) is known as an object. For example: chair, pen, table,
keyboard, bike etc. It can be physical and logical. Whereas a class is a Collection
of objects is called class. It is a logical entity.
Object is a runtime entity, it is created at runtime. Object is an instance of a
class. All the members of the class can be accessed through object.

2. Abstraction
Abstraction or Data Abstraction is a process of hiding irrelevant details from user. For
example, When you send an sms you just type the message, select the contact and
click send, the phone shows you that the message has been sent, what actually
happens in background when you click send is hidden from you as it is not relevant to
you.

3. Encapsulation
Encapsulation is a process of combining data and function into a single unit like
capsule. This is to avoid the access of private data members from outside the class. To
achieve encapsulation, we make all data members of class private and create public
functions; using them we can get the values from these data members or set the value
to these data members.

4. Inheritance
Inheritance is a feature using which an object of child class acquires the properties of
parent class.
#include <iostream>
using namespace std;
class ParentClass
{
//data member
public:
int var1 =100;
};
class ChildClass : public ParentClass
{
public:
int var2 = 500;
};
int main(void) {
ChildClass obj;
}
Now this object obj can use the properties (such as variable var1) of ParentClass.

5. Polymorphism
Function overloading and Operator overloading are examples of polymorphism.
Polymorphism is a feature using which an object behaves differently in different
situation.
In function overloading we can have more than one function with same name but
different numbers, type or sequence of arguments.
Polymorphism Example
#include <iostream>
using namespace std;
class Sum
{
public:
int add(int num1,int num2)
{
return num1 + num2;
}
int add(int num1, int num2, int num3){
return num1 + num2 + num3;
}
};
int main(void)
{
//Object of class Sum
Sum obj;
//This will call the second add function
cout<<obj.add(10, 20, 30)<<endl;
//This will call the first add function
cout<<obj.add(11, 22);
return 0;
}
Output:
60
33
C++ Class and Object
Since C++ is an object-oriented language, program is designed using objects and
classes in C++.

C++ Object
In C++, Object is a real world entity, for example, chair, car, pen, mobile, laptop etc.

That is, object is an entity that has state and behavior. Here, state means data and
behavior means functionality.

Object is a runtime entity, it is created at runtime.

Object is an instance of a class. All the members of the class can be accessed
through object.

Let's see an example to create object of student class using s1 as the reference
variable.

Student s1; //creating an object of Student


In this example, Student is the type and s1 is the reference variable that refers to the
instance of Student class.

C++ Class
In C++, class is a group of similar objects. It is a template from which objects are
created. It can have fields (data members/variables), methods (member
functions), constructors etc.

Let's see an example of C++ class that has three fields only.

class Student
{
public:
int id; //field or data member
float salary; //field or data member
String name;//field or data member
}
C++ Object and Class Example
Let's see an example of class that has two fields: id and name. It creates instance of
the class, initializes the object and prints the object value.
#include <iostream>
using namespace std;
class Student {
public:
int id;//data member (also instance variable)
string name;//data member(also instance variable)
};
int main() {
Student s1; //creating an object of Student
s1.id = 201;
s1.name = "Sonoo Jaiswal";
cout<<s1.id<<endl;
cout<<s1.name<<endl;
return 0;
}
Output:

201
Sonoo Jaiswal

C++ Class Example: Initialize and Display data through method


Let's see another example of C++ class where we are initializing and displaying object
through method.

#include <iostream>
using namespace std;
class Student {
public:
int id;//data member (also instance variable)
string name;//data member(also instance variable)
void insert(int i, string n)
{
id = i;
name = n;
}
void display()
{
cout<<id<<" "<<name<<endl;
}
};
int main(void) {
Student s1; //creating an object of Student
Student s2; //creating an object of Student
s1.insert(201, "Sonoo");
s2.insert(202, "Nakul");
s1.display();
s2.display();
return 0;
}
Output:

201 Sonoo
202 Nakul
C++ Class Example: Store and Display Employee Information
Let's see another example of C++ class where we are storing and displaying employee
information using method.

#include <iostream>
using namespace std;
class Employee {
public:
int id;//data member (also instance variable)
string name;//data member(also instance variable)
float salary;
void insert(int i, string n, float s)
{
id = i;
name = n;
salary = s;
}
void display()
{
cout<<id<<" "<<name<<" "<<salary<<endl;
}
};
int main(void) {
Employee e1; //creating an object of Employee
Employee e2; //creating an object of Employee
e1.insert(201, "Sonoo",990000);
e2.insert(202, "Nakul", 29000);
e1.display();
e2.display();
return 0;
}
Output:

201 Sonoo 990000


202 Nakul 29000

You might also like