unit 2 oop
unit 2 oop
• Introduction to OOP
• Procedural Vs. Object Oriented Programming
• Principles of OOP
• Benefits and applications of OOP
Introduction to OOP
▪ OOP is a design philosophy. It stands for Object Oriented
Programming.
▪ C++ was founded in (1983)
Bjarne Stroustrup
Introduction to OOP
▪ Object-Oriented Programming (OOP) uses a different set of
programming languages than old procedural programming
languages like (C, Pascal, etc.).
▪ Everything in OOP is grouped as self sustainable "objects".
What is Object?
Result Account
Bank Account
Logical objects…
Attributes and operations
Events
Properties (Describe)
On_Start
Manufacturer
On_Parked
Model
On_Brake
Color
Year
Methods (Actions)
Price
Start
Drive
Park
Classes…
Abstraction
Encapsulation
Inheritance
Polymorphism
Abstraction
▪ Abstraction refers to the act of representing essential features
without including the background details or explanations.
▪ Abstraction provides you a generalized view of your classes or
object by providing relevant information.
▪ Abstraction is the process of hiding the working style of an object,
and showing the information of an object in understandable
manner.
Abstraction Example
FM Radio FM Radio
MP3 MP3
Camera Camera
Video Recording
Reading E-mails
Abstraction Example
▪ Example:
If somebody in your collage tell you to fill application form, you
will fill your details like name, address, data of birth, which
semester, percentage you have got etc.
▪ If some doctor gives you an application to fill the details, you
will fill the details like name, address, date of birth, blood group,
height and weight.
▪ See in the above example what is the common thing?
Age, name, address so you can create the class which consist of
common thing that is called abstract class.
That class is not complete and it can inherit by other class.
Encapsulation
▪ The wrapping up of data and functions into a single unit is known
as encapsulation
▪ The insulation of the data from direct access by the program is
called data hiding or information hiding.
▪ It is the process of enclosing one or more details from outside
world through access right.
Encapsulation
A B
C
Inheritance
▪ Inheritance is the process by which objects of one class acquire
the properties of objects of another class.
Vehicle
▪ Here Vehicle class can have properties like Chassis no. , Engine,
Colour etc.
▪ All these properties inherited by sub classes of vehicle class.
Polymorphism
▪ Polymorphism means ability to take more than one form.
▪ For example the operation addition.
▪ For two numbers the operation will generate a sum.
▪ If the operands are strings, then the operation would produce a
third string by concatenation.
C++ Object
in C++, Object is a real world entity, for example, chair, car, pen,
mobile, laptop etc.
In other words, object is an entity that has state and behavior. Here,
state means data and behavior means functionality.
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++ Class
In C++, class is a group of similar objects. It is a template
from which objects are created. It can have fields,
methods, constructors etc.
Let's see an example of C++ class that has three fields only.
class MyClass
{ // The class
public: // Access specifier
int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};
C++ Object and Class Example
#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;
}
C++ Object and Class Example
#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;
}
C++ Class Example: Initialize and Display data 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;
}
C++ Class Example: Store and Display Employee Information
#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;
} };
C++ Class Example: Store and Display Employee Information
int main(void) {
Employee e1; //creating an object of Employee
Employee e2; //creating an object of Employee
e1.insert(201, "Sahil",990000);
e2.insert(202, "Nakul", 29000);
e1.display();
e2.display();
return 0;
}
C++ Constructor
A constructor in C++ is a special method that is
automatically called when an object of a class is
created.
Default constructor
Parameterized constructor
C++ Constructor
#include <iostream>
using namespace std;
class Employee
{
public:
Employee()
{
cout<<"Default Constructor Invoked"<<endl;
}
};
int main(void)
{
Employee e1; //creating an object of Employee
Employee e2;
return 0;
}
C++ Parameterized constructor
Constructor
C++ Copy Constructor
The copy constructor in C++ is used to copy data of one object to another.
#include <iostream>
// copy constructor with a Wall object as
using namespace std;
parameter
// copies data of the obj parameter
// declare a class Wall(Wall &obj) {
class Wall { length = obj.length;
private: height = obj.height;
double length; }
double height;
double calculateArea() {
public: return length * height;
}
};
Wall(double len, double hgt) {
length = len;
height = hgt;
}
C++ Copy Constructor
The copy constructor in C++ is used to copy data of one object to
another.
int main() {
return 0;
}
C++ Destructor
A destructor works opposite to constructor; it destructs the objects of
classes. It can be defined only once in a class. Like constructors, it is
invoked automatically.
class_name::function_name (parameter);
Example
#include <iostream>
using namespace std;
class Note
{
// declare a static data member
static int num;
public:
// create static member function
static int func ()
{
return num;
}
};
// initialize the static data member using the class name and the scope resolution operator
int Note :: num = 5;
int main ()
{
// access static member function using the class name and the scope resolution
cout << " The value of the num is: " << Note:: func () << endl;
return 0;
}
Example- Let's create another program to access the static member
function using the class' object in the C++ programming language.
#include <iostream>
using namespace std;
class Note
{
static int num;
public:
// create static member function
static int func ()
{
cout << " The value of the num is: " << num << endl;
}
};
Example- Let's create another program to access the static
member function using the class' object in the C++
programming language.
int main ()
{
// create an object of the class Note
Note n;
// access static member function using the object
n.func();
return 0;
}
C++ Strings
#include <iostream>
using namespace std;
int main( ) {
string s1 = "Hello";
char ch[] = { 'C', '+', '+'};
string s2 = string(ch);
cout<<s1<<endl;
cout<<s2<<endl;
}
C++ String copy Example
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char key[25], buffer[25];
cout << "Enter the key string: ";
cin.getline(key, 25);
strcpy(buffer, key);
cout << "Key = "<< key << endl;
cout << "Buffer = "<< buffer<<endl;
return 0;
}
C++ String length Example
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char ary[] = "Welcome to C++ Programming";
cout << "Length of String = " << strlen(ary)<<endl;
return 0;
}
Operations on Strings
1)Input Functions
2)Capacity Functions
3) Iterator Functions
4) Manipulating Functions:
C++ Operator Overloading
C++ operator overloading is one of the most powerful
features of C++ that allows a user to change the way the
operator works.
//body of function
}
Operator overloading in C++ can be achieved
in following ways
• Operator overloading using member function
• Operator overloading using non-member
function
• Operator overloading using friend function
which we cannot?
public: count1.display();
Count() : value(5) {} return 0;
}
// Overload ++ when used as prefix
void operator ++ () {
++value;
}
void display() {
cout << "Count: " << value <<
endl;
}};
Binary Operator Overloading
#include <iostream>
using namespace std;
class Complex {
private:
float real;
float imag;
public:
// Constructor to initialize real and imag to 0
Complex() : real(0), imag(0) {}
void input() {
cout << "Enter real and imaginary parts respectively: ";
cin >> real;
cin >> imag;
}
int main() {
Complex complex1, complex2, result;
return 0;
}
Output
}
Class Myclass
{
private:
int member1;
}
int main()
{
Myclass obj;
obj.member1=5;
}
C++ Friend function
class class_name
{
friend data_type function_name(argument/s);
// syntax of
friend function.
};
In the above declaration, the friend function is preceded by the keyword friend.
The function can be defined anywhere in the program like a normal C++
function. The function definition does not use either the keyword friend or
scope resolution operator.
C++ function Example
#include <iostream>
using namespace std;
class Box
{
private:
int length;
public:
Box(): length(0) { }
Introduction
• Friend function using operator overloading offers better flexibility to the
class.
• These functions are not a members of the class and they do not have 'this'
pointer.
• When you overload a unary operator you have to pass one argument.
• When you overload a binary operator you have to pass two arguments.
• Friend function can access private members of a class directly.
Syntax:
friend return-type operator operator-symbol (Variable 1, Varibale2)
{
//Statements;
}
Program demonstrating Unary operator overloading using
Friend function
#include<iostream>
using namespace std;
class UnaryFriend
{
int a=10;
int b=20;
int c=30;
public:
void getvalues()
{
cout<<"Values of A, B & C\n";
cout<<a<<"\n"<<b<<"\n"<<c<<"\n"<<endl;
}
void show()
{
cout<<a<<"\n"<<b<<"\n"<<c<<"\n"<<endl;
}
void friend operator-(UnaryFriend &x); //Pass by reference
};
Program demonstrating Unary operator overloading using
Friend function
int main()
{
UnaryFriend x1;
x1.getvalues();
cout<<"Before Overloading\n";
x1.show();
cout<<"After Overloading \n";
-x1;
x1.show();
return 0;
}
Output:
Values of A, B & C
https://mjginfologs.com/type-conversion-in-
cpp/#:~:text=Class%20type%20to%20Basic%20Type,-
The%20constructor%20functions&text=C%2B%2B%20
allows%20us%20to%20define,)%20%7B%20%2F%2FP
rogram%20statement.%20%7D