Object Oriented Programming
Object Oriented Programming
● Class is a user-defined data type which defines its properties and its
functions. Class is the only logical representation of the data. For
example, Human being is a class. The body parts of a human being are its
properties, and the actions performed by the body parts are known as
functions. The class does not occupy any memory space till the time an
object is instantiated.
APNI KAKSHA
● Inheritance
Inheritance is a process in which one object acquires all the properties and
behaviors of its parent object automatically. In such a way, you can reuse,
extend or modify the attributes and behaviors which are defined in other
classes.
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.
C++ Syntax :
Types of Inheritance :
1. Single inheritance : When one class inherits another class, it is known
as single level inheritance
2. Multiple inheritance : Multiple inheritance is the process of deriving
a new class that inherits the attributes from two or more classes.
3. Hierarchical inheritance : Hierarchical inheritance is defined as the
process of deriving more than one class from a base class.
4. Multilevel inheritance : Multilevel inheritance is a process of deriving a
class from another derived class.
5. Hybrid inheritance : Hybrid inheritance is a combination of
simple, multiple inheritance and hierarchical inheritance.
● Encapsulation
APNI KAKSHA
● Abstraction
● Polymorphism
APNI KAKSHA
Method Overloading : Method overloading is a technique which allows you
to have more than one function with the same function name but with
different functionality. Method overloading can be possible on the
following basis:
Example :
#include<bits/stdc++.h>
using namespace std;
class Add {
public:
int add(int a,int b){
return (a + b);
}
int add(int a,int b,int c){
return (a + b + c);
}
};
int main(){
Add obj;
int res1,res2;
res1 = obj.add(2,3);
res2 = obj.add(2,3,4);
cout << res1 << " " << res2 << endl;
return 0;
}
/*
Output : 5 9
add() is an overloaded function with a different number of parameters. */
APNI KAKSHA
● Runtime Polymorphism : Runtime polymorphism is also known as dynamic
polymorphism. Function overriding is an example of runtime
polymorphism. Function overriding means when the child class contains
the method which is already present in the parent class. Hence, the child
class overrides the method of the parent class. In case of function
overriding, parent and child classes both contain the same function with a
different definition. The call to the function is determined at runtime is
known as runtime polymorphism.
#include <bits/stdc++.h>
using namespace std;
class Base_class{
public:
virtual void show(){
cout << "Apni Kaksha base" << endl;
}
};
int main(){
Base_class* b;
Derived_class d;
b = &d;
b->show(); // prints the content of show() declared in derived
class return 0;
APNI KAKSHA
}
#include <bits/stdc++.h>
using namespace std;
class go {
public:
int x;
go(int a){ // parameterized constructor.
APNI KAKSHA
x=a;
}
go(go &i){ // copy constructor
x = i.x;
}
};
int main(){
go a1(20); // Calling the parameterized constructor.
go a2(a1); // Calling the copy constructor.
cout << a2.x << endl;
return 0;
}
// Output : 20
Example :
#include<bits/stdc++.h>
using namespace std;
class A{
public:
// constructor and destructor are called automatically,
once the object is instantiated
A(){
cout << "Constructor in use" << endl;
}
~A(){
cout << "Destructor in use" << endl;
}
};
int main(){
APNI KAKSHA
A a;
A b;
return 0;
}
/*
Output: Constructor in use
Constructor in use
Destructor in use
Destructor in use
*/
● ‘this’ Pointer : this is a keyword that refers to the current instance of the
class. There can be 3 main uses of ‘this’ keyword:
1. It can be used to pass the current object as a parameter to
another method
C++ Syntax :
struct node{
int data;
node *next;
node(int x){
this->data = x;
this->next = NULL;
}
}
● Friend Function : Friend function acts as a friend of the class. It can access
the private and protected members of the class. The friend function is not
APNI KAKSHA
a member of the class, but it must be listed in the class definition. The
non-member function cannot access the private data of the class.
Sometimes, it is necessary for the non-member function to access the data.
The friend function is a non-member function and has the ability to
access the private data of the class.
Note :
1. A friend function cannot access the private members directly, it has
to use an object name and dot operator with each member name.
2. Friend function uses objects as arguments.
Example IMP :
#include <bits/stdc++.h>
using namespace std;
class A{
int a = 2;
int b = 4;
public:
// friend function
friend int mul(A k){
return (k.a * k.b);
}
};
int main(){
A obj;
int res = mul(obj);
cout << res << endl;
return 0;
}
// Output : 8
APNI KAKSHA
any entity reference. It is another way to reuse the class. It is a form of
association that represents the HAS-A relationship.
2. When we use the same function name in both base and derived
class, the function in base class is declared with a keyword
virtual.
Key Points :
1. Virtual functions cannot be static.
2. A class may have a virtual destructor but it cannot have a virtual
constructor.
C++ Example :
#include <bits/stdc++.h>
APNI KAKSHA
using namespace std;
class base {
public:
// virtual function (re-defined in the derived class)
virtual void print(){
cout << "print base class" << endl;
}
void show(){
cout << "show base class" << endl;
}
};
void show(){
cout << "show derived class" << endl;
}
};
int main(){
base* bptr;
derived d;
bptr = &d;
/*
output :
APNI KAKSHA
print derived class // (impact of virtual function)
show base class
*/
C++ Syntax :
C++ Example :
#include<bits/stdc++.h>
using namespace std;
class Base{
public:
virtual void show() = 0;
};
class Derived : public Base {
public:
void show() {
cout << "You can see me !" << endl;
}
};
int main(){
Base *bptr;
APNI KAKSHA
Derived d;
bptr = &d;
bptr->show();
return 0;
}
Example :
#include<bits/stdc++.h>
using namespace std;
// abstract class
class Shape{
public:
virtual void draw()=0;
};
class Rectangle : Shape{
public:
void draw(){
cout << "Rectangle" << endl;
}
};
class Square : Shape{
public:
void draw(){
cout << "Square" << endl;
}
};
int main(){
Rectangle rec;
Square sq;
APNI KAKSHA
rec.draw();
sq.draw();
return 0;
}
/*
Output :
Rectangle
Square
*/
● Namespaces in C++ :
1. The namespace is a logical division of the code which is designed to
stop the naming conflict.
2. The namespace defines the scope where the identifiers such as
variables, class, functions are declared.
3. The main purpose of using namespace in C++ is to remove the
ambiguity. Ambiguity occurs when a different task occurs with the
same name.
4. For example: if there are two functions with the same name such as
add(). In order to prevent this ambiguity, the namespace is used.
Functions are declared in different namespaces.
5. C++ consists of a standard namespace, i.e., std which contains
inbuilt classes and functions. So, by using the statement "using
namespace std;" includes the namespace "std" in our program.
C++ Example :
#include <bits/stdc++.h>
using namespace std;
// user-defined namespace
namespace Add {
int a = 5, b = 5;
int add() {
return (a + b);
}
}
APNI KAKSHA
int main() {
int res = Add :: add(); // accessing the function inside namespace
cout << res;
}
// output : 10
● Access Specifiers IMP : The access specifiers are used to define how functions
and variables can be accessed outside the class. There are three types of
access specifiers:
Key Notes
● Virtual inheritance facilitates you to create only one copy of each object
even if the object appears more than one in the hierarchy.
APNI KAKSHA
Operator overloading: Operator overloading is defined as the standard
operator can be redefined so that it has a different meaning when applied to
the instances of a class.
APNI KAKSHA