OOPS Fast Fast
OOPS Fast Fast
Public:
• Data members and member functions can be accessed by other classes and functions.
• Accessible from anywhere in the program using the direct member access operator (.).
// access modifier
#include<iostream>
// class definition
class Circle
public:
double radius;
double compute_area()
return 3.14*radius*radius;
};
// main function
int main()
Circle obj;
obj.radius = 5.5;
return 0;
Output:
Radius is: 5.5
Area is: 94.985
Private:
• Class members declared as private can only be accessed by member functions inside
the class.
// access modifier
#include<iostream>
using namespace std;
class Circle
private:
double radius;
public:
double compute_area()
return 3.14*radius*radius;
};
// main function
int main()
Circle obj;
return 0;
Output:
In function 'int main()':
11:16: error: 'double Circle::radius' is private
double radius;
^
31:9: error: within this context
obj.radius = 1.5;
// access modifier
#include<iostream>
class Circle
private:
double radius;
public:
void compute_area(double r)
radius = r;
};
// main function
int main()
Circle obj;
obj.compute_area(1.5);
return 0;
}
Output:
Radius is: 1.5
Area is: 7.065
Protected:
• Similar to access modifier, protected access can be accessed by any subclass of that
class.
• Private data members of a class can be accessed indirectly using public member
functions.
#include <bits/stdc++.h>
// base class
class Parent
protected:
int id_protected;
};
{
public:
id_protected = id;
void displayId()
};
// main function
int main() {
Child obj1;
obj1.setId(81);
obj1.displayId();
return 0;
Output:
id_protected is: 81
#include <iostream>
class GFG {
private:
int private_variable;
protected:
int protected_variable;
public:
GFG()
{
private_variable = 10;
protected_variable = 99;
friend class F;
};
// class GFG.
class F {
public:
void display(GFG& t)
<< t.protected_variable;
};
// Driver code
int main()
{
GFG g;
F fri;
fri.display(g);
return 0;
Output
• They are non-member functions that can access and manipulate the
private and protected members of the class.
// as a friend function
#include <iostream>
class anotherClass {
public:
};
// base class for which friend is declared
class base {
private:
int private_variable;
protected:
int protected_variable;
public:
base()
private_variable = 10;
protected_variable = 99;
};
<< endl;
}
// driver code
int main()
base object1;
anotherClass object2;
object2.memberFunction(object1);
return 0;
Output
Private Variable: 10
Protected Variable: 99
#include <iostream>
// Forward declaration
class ABC;
class XYZ {
int x;
public:
void set_data(int a)
x = a;
};
class ABC {
int y;
public:
void set_data(int a)
y = a;
};
else
// Driver code
int main()
ABC _abc;
XYZ _xyz;
_xyz.set_data(20);
_abc.set_data(35);
// calling friend function
max(_xyz, _abc);
return 0;
Output35
Advantages:
Disadvantages:
The constructor can be defined inside the class declaration or outside the class
declaration
<class-name> (list-of-parameters)
{
// constructor definition
}
<class-name>: :<class-name>(list-of-parameters)
{
// constructor definition
}
#include <iostream>
int rno;
char name[50];
double fee;
public:
// constructor
student()
void display()
cout << endl << rno << "\t" << name << "\t" << fee;
};
int main()
s.display();
return 0;
Output
Enter the RollNo:121
Enter the Name:Geeks
Enter the Fee:5000
121 Geeks 5000
#include <iostream>
class student {
int rno;
char name[50];
double fee;
public:
student();
void display();
};
void student::display()
cout << endl << rno << "\t" << name << "\t" << fee;
// driver code
int main()
student s;
s.display();
return 0;
Output
Enter the RollNo:11
Enter the Name:Aman
Enter the Fee:10111
11 Aman 10111
C++ Constructor Characteristics
#include <iostream>
class Base {
public:
};
class A {
public:
// uninitialized
int size;
};
class B : public A {
};
class C : public A {
public:
C()
};
class D {
public:
D()
private:
A a;
};
// Driver Code
int main()
Base base;
B b;
C c;
D d;
return 0;
Output
A Constructor
A Constructor
C Constructor
A Constructor
D Constructor
#include <iostream>
#include <string.h>
class student {
int rno;
char name[50];
double fee;
public:
rno = t.rno;
strcpy(name, t.name);
fee = t.fee;
void display();
};
rno = no;
strcpy(name, n);
fee = f;
void student::display()
cout << endl << rno << "\t" << name << "\t" << fee;
int main()
s.display();
manjeet.display();
return 0;
• The above program compiles and runs fine, indicating that creating
private destructors is not a compiler error.
• The class instance method can also be used to create classes with
private destructors.
• The C++ Standard allows the compiler to optimize the copy away in
certain cases, such as Return Value Optimization (RVO).
• Only one copy of the member is created for the entire class and
shared by all objects.
• Visible only within the class, but its lifetime is the entire
program.
• Accessing any static member without any object can be done using the
scope resolution operator directly with the class name.
• Can access static data members and static member functions inside or
outside of the class.
• Have a scope inside the class and cannot access the current object
pointer.
• Can access static data members and static member functions inside or
outside of the class.
• Can determine how many objects of the class have been created.
• The type of this pointer is 'X*' for a class X and 'const X *' for a
member function of X declared as const.
• The scope resolution operator can only be used for static data
members or class members.
Real-Life Example
Features of Encapsulation
• The example shows that the data of the sections like sales, finance,
or accounts are hidden from any other section.
Types of Abstraction
• Data abstraction: Shows only required information about the data and
hides unnecessary data.
• Control abstraction: Shows only required information about the
implementation and hides unnecessary information.
Abstraction in C++
Examples of Abstraction
• The program also includes a class Vehicle with private and public
members.
Types of Polymorphism
Explanation
• An example is the addition operator (+) for string class, which adds
two operands.
• The example program uses the operator '+' to perform the addition of
two numbers, not just adding them.
• The defining of the Parent class and the derived class are crucial
in defining the overridden function and the derived class.
• They allow the compiler to perform late binding, matching the object
with the right-called function and executing it during runtime.
• Example: Class bike can inherit from two-wheel vehicles class, which
can be a subclass of vehicles.
• Overloaded constructors have the same name but differ by the number
and type of arguments.
• Member function declarations with the same name and the name
parameter-type-list cannot be overloaded if any of them is a static
member function declaration.
• For example, two functions: int sum() and float sum(), will generate
a compile-time error as function overloading is not possible.
• Example: Adding two objects of type "class A" using the "+"
operator.
• The user redefines the meaning of the "+" operator to add two class
objects.
• Operator functions are the same as normal functions, but the name of
an operator function is always the operator keyword followed by the
operator symbol.
• Binary Arithmetic: +, -, *, /, %
• Unary Arithmetic: +, -, ++, —
• De-referencing:(->)
• De-allocation:New, delete
• Subscript:[ ]
• Function call:()
• Logical:&, | |,!
Overloading Limitations
1. Sizeof Operator:
2. Typeid Operator:
• Provides a CPP program with the ability to recover the derived type
of the object referred to by a pointer or reference.
Note: d2 = -d1 will not work as operator-() does not return any value.
• Functions that take only one argument can be passed to using global
variables, but good coding practices advise against using them.
• The code snippet shows how functors can be used in conjunction with
STLs.
• Example:
Example:
• The program implements constructor in multiple inheritance.
• The output includes the constructors of the base class A2, A1, and
the derived class S.