Unit-2 and Unit-3
Unit-2 and Unit-3
What is OOP‟s?
Features
POP & OOP Comparison
Class & Object
Inheritance & Its Types
Constructor & Destructors
Functions – Inline ,Friend & Virtual
Virtual Class
Introduction
3
1. Object
2. Classes
3. Data Abstraction & Encapsulation
4. Inheritance
5. Polymorphism
6. Dynamic Binding
7. Message Passing
CLASS & OBJECT 8
Syntax
[access specifier]
Class<Keyword> < class name >
{ Access specifier : variable declarations;
Access specifier :
method declarations & implementations };
NOTE:
In C++ Private data should be accessed only by member functions of the same class.
Member functions : The functions which are declared inside the class are called as
member functions.
Default access specifier in C++ is private.
private members are not directly accessible from out side the class.
10
11
Object 12
NOTE:
In a class to access data members first memory have to be allocated.
No memory is allocated when a class is created. Memory is allocated only when an
object is created, i.e., when an instance of a class is created. Object is physical copy
and class is logical copy.
Syntax :
for creating an object :
class name followed by variable name;
Demo1 obj1;
13
Polymorphism
Polymorphism comes from the Greek words “poly” and “morphism”.
“poly” means many and “morphism” means form
i.e.. many forms. Polymorphism means the ability to take more than one form.
Advantage of this is you can make an object behave differently in different
situations, so that no need to create
Polymorphism can be achieved with the help of Overloading and Overriding
concepts and it is classified into compile time polymorphism and Runtime
polymorphism.
Function with same name but different arguments .
Functioning is different.
Example : add2i (int a, int b) add3i (int a, int b, int c)
add2f (float a, float b) add3f (float a, float b, float c)
16
Message Passing
The technique of message communication between objects
makes the interface with external systems easier.
17
1. C follows procedural
programming concepts.
2. Data is less Secured in
C.
C follows the top down
approach – prorgam
devides
SCOPE RESOLUTION OPERATOR (::) 18
This preprocessor directive tells the compiler that the header file needs to
be processed prior to the compilation. For example, the <iostream>
header file in C++ contains the definition of input-output functions.
✖ Standard library header files
✖ User-defined header files
✖ The name of the header file is enclosed in double quotes, and this
syntax is usually preferred when you are defining a user-defined
header file.
20
Continue ….
INHERITANCE TYPES
1. Single Inheritance
2. Multiple Inheritance
3. Multi-level Inheritance
4. Hierarchal Inheritance
5. Hybrid Inheritance
28
Single Inheritance
✖ Single or simple Inheritance - In this Class A1
type of inheritance, a sub class derives
properties from a single super class.
Class B1
Class Z1
Class A 29
✖ Hierarchical Inheritance - In
this type of inheritance,
multiple sub classes derive
properties from a single super
class.
30
Hybrid Inheritance
Class A
Constructor
Syntax: Example:
Class ClassName Class Number
{ { Public: Number ()
Public: Constructor {
Name () Cout<<“Demo for
{} Contructor”;
}; }
};
38
.
outside class definition using
Constructors can be defined class name and scope resolution
either inside the class :: operator
definition
Syntax class Demo2
{ public: int i;
class Demo1 Demo2(); // constructor declared
{ public: int x; }; Demo2::Demo2() //
constructor definition { i = 100; }
Demo1() // constructor
{ // object initialization } };
define a constructor & initialize in class data member variables with constants. 39
{
{
//default constructor called
public: when object is created
int l; Length obj1;
//default constructor cout<<"Line size is"<<"
Length() "<<obj1.l<<" "<<"cm";
{
return 0;
l=100; }
}
};
DEFAULT CONSTRUCTOR EXAMPLE 42
{ C1.display();
v3=obj.v3; return 0;
} }
47
48
Destructors
class ConstDest
o To release (or) delete this memory we use
destructor. (When ever constructor is executed { ConstDest() // Constructor
object is defined and memory is allocated.) { Cout<<“here constructor create
Destructors are typically used to de-allocate memory”;
memory – delete the memory created by the }
constructor.
~ConstDest() //Destructor
{
o The destructor is called whenever an
object's lifetime ends, which includes Cout<<“here destructor releases
program termination. (objects with memory”;
static storage duration) }
};
49
Reminder to create Destructor
1. Destructor name should be similar to class name and preceded by 'tilde'
operator(~).
2.Destructor should be declared in public section.
3. Destructor doesn't have any arguments or don‟t return any value (or void),
so overloading is not possible.
4. Destructor never returns a value, it makes a implicit call new and delete
operator.
5. Destructor never called with object followed by dot(.) operator.
6. Destructor never participate or use in inheritance.
1. The constructor initializes the class 1. If the object is no longer required, then
and allots the memory to an object. destructors demolish the objects.
2. If the object is no longer required, 2. When the program gets terminated, the
then destructors demolish the objects. destructor is called automatically.
3. It receives arguments & can be 3. It does not receive any argument & cant
overloaded. overloaded.
4. They are often called in successive 4. They are often called in reverse order of
order. constructor. (explicit define)
class Demo1
{
public:
Demo1()
{cout << "when object created - constructor call" << endl; }
~ Demo1()
{ cout << "when object destroy - destructor call"<< endl; }
};
int main()
{
Demo1 *obj1= new Demo1();
delete obj;
return 0;
}
Function in C++ 52
#include <iostream>
using namespace std;
inline int
ShowNum(int n)
{
cout<<"Number is "<< n<<endl;
}
int main() {
ShowNum(10);
ShowNum(15);
return 0;
}
VIRTUAL CLASS
55
All the private members cannot be accessed from outside the class.
i.e., a non member function cannot have an access to the private
data of a class.
In C++ a non member function can access private members by
making the function friendly to a class.
A friend function is not a member of any class, but it is able to
access the private members of those where it is presented as a
friend.
when a data is declared as private inside a class, then it is not
accessible from outside the class. A friend function is used for
accessing the non-public members of a class.
Continue…
60
public:
char name[20];
int id;
int age; };
int main() {
class Userinfo u1={"Ravi",111,35};
Userinfo *ptr;
ptr=&u1;
cout<<"User Name= "<<ptr->name<<endl;
cout<<"User id= "<<ptr->id<<endl;
cout<<" User Age= "<<ptr->age<<endl;
return 0; }
POINTER TO OBJECT 69
class demo {
int main() {
int v1,v2;
demo dobj;
public:
dobj.read(100,200);
void read(int v1, int v2) {
dobj.show();
this->v1=v1;
return 0;
this->v2=v2;
}
}
void show() {
cout<<"V1= "<<v1<<endl;
cout<<"V2= "<<v2<<endl;
}
};
72
//this pointer
#include<iostream>
int main() {
using namespace std;
TPdemo tp;
class TPdemo {
tp.disp(200);
int v1;
return 0;
public: TPdemo() {
}
v1=10000;
}
void disp(int v1) {
cout<<"The value of argument variable 1="<<v1;
cout<<“The value of data member v1="<<this->v1;
}
};
73
Type Conversion
Function Overriding
76
ABSTRACT CLASS
• In C++, an abstract class is a class that cannot be instantiated on its
own but is meant to be used as a base class for other classes.
• When a class is not using for creating an object such class is known
as abstract class.
• An abstract class gives a structure, using this other classes are form
as.
• By defining an abstract class, you can provide a common interface and shared functionality
that subclasses must implement or extend. This promotes code reusability and reduces
redundancy in your codebase.
• Abstract classes can have pure virtual functions, which are functions without a defined
implementation in the abstract class itself.
• Polymorphism allows you to treat objects of different classes that inherit from the same
abstract class in a uniform way. This makes it easier to work with a variety of related objects
without needing to know their specific types.
• Design for Extensibility: You can create an abstract class with a defined set of methods and
attributes and later extend it by creating new subclasses. These subclasses can add more
functionality while still adhering to the contract defined by the abstract class.
• Preventing Object Instantiation: Abstract classes cannot be instantiated on their own, which
means you cannot create objects of an abstract class. This helps ensure that the class is used
as a base for other classes and not as a standalone object.
78
#include<iostream> void disp()
using namespace std; {
class super1 { cout<<“access base class -super1- disp
public: () in derived class sub1";
virtual void disp()=0; }
int v1; };
}; int main() {
class sub2 : public super1 { super1 *s1obj;
public: sub2 obj2;
s1obj = &obj2;
s1obj->disp();
return 0;
}
79
FUNCTION OVERRIDING
Function overriding is a fundamental concept in object-oriented
programming that empowers developers to create more flexible
and specialized classes. It enables a derived class to provide its own
implementation of a function inherited from a base class.
Function overriding is a feature that allows a derived class to
provide a new implementation for a function that is already defined
in its base class. This is essential for building hierarchical
relationships between classes, where derived classes inherit
characteristics and behavior from their base classes but can also
customize or extend those behaviors as needed.
Continue… 80
Class Base {
Public: Virtual Void Display () {
cout<<“here display fun. Of base class “;
}
};
Class Derived: Public Base
{
Public: void Display()
{
cout<<“ display of derived class”;
}
}
81
Syntax:
class Base {
public:
virtual returnType functionName(parameters) {
// Base class implementation
}
};
class Derived : public Base {
public:
returnType functionName(parameters) override {
// Derived class implementation
}
};
Thanks