Object Oriented Programming C++ Notes
Object Oriented Programming C++ Notes
Structures
• Can come before main function and after main function
• User defined data type
• Collection of multiple variables of different data types
Declaration of structure
Memory Blocks
};
int main (){
For Example: vehical car; //define structure variable
//Acessing number of structure
#include <iostream>
cout << "Enter Car Brand:" << endl;
using namespace std;
cin >> car.brand;
struct vehicle //declaration structure
cout << "Enter Car Model:" << endl;
{
cin >> car.model;
string brand;
cout << "Enter Years of issue: " << endl;
string model;
cin>> car.years;
int years;
}
Union (Concept)
• Like structure but we make variable in structure and memory allocated in multiple cells
• Difference variable allocated on basis of biggest/highest data member
• Memory management
• can't apply oops concept
• Can't apply construct destruct concepts
Example
Enum (Concept)
Enum (Enumeration)
~ Lecture By sir
Syntax
Example
Output:
2
By changing index
Oops Concept
Inheritance
Basically Approach not a language
Two types of programing • Parent Child Same Characteristics
• Procedural
• Object oriented Polymorphism
Programming based on object • State Object in different aspects
Pillar of oops are four
• Inheritance Encapsulation
• Polymorphism • Data Building
• Abstraction
• Encapsulation Abstraction
• Data Hiding
Class:
Class and object basic components Object
• Class collection of objects
• Object is a real world Entity Like person,
• Representing objects
Things etc.
• Memory will allocated as we create object But Do not created
• Objects have specific properties(states)
when class is created
and functions(behaviors)
Methods and Data members and functions are studied in function
• When object is created memory is allocated
Private public Protected
Lecture by Sir
Function in class
Function can must be declare with in class but can be
defined within or without the class
Example
Example
Constructor Example
(Concept)
• Name will be same as class
• Return type is not used/Defined
• Main point: As object is created the body of constructor is
executed automatically no need to call function
Types:
• Default
• Parametrized
• Copy
Lecture by Sir:
• Special Member Function
• Whose name is same as class name
• Constructor call automatically when object is created
• Constructor Do not have return type
• Can't be declared as virtual Function
Syntax
Default Constructor
The Constructor which have no arguments or no parameters
JAVA Can overload main function
C++ can't overload main function and return type Constructor Overloading
Default data type of function can be "void" or "int" The process of Declaring multiple
constructor in a program with same but
Recursion different in
Recursion is a programming technique in which the • No of parameters
function calls itself • Type of Parameters
• Sequence of parameters
Recursive function
The function that calls itself Example:
Used instead of loop, used in stack(last In last out)
Less Efficient
Requires more memory
Example
Paramerterized constructor
Jis me parameter pass kerjai
Copy constructor Syntax:
Through copy constructor we initialize the variable of Static data_type variable_name;
object with value of variable of other object
Conditions: Example:
Same as class name Static int a;
Data-type should be same
Const
Object pass as argument in function
Syntax ▪ Fix value
▪ Will not upgrade
▪ Scope will different
▪ Can't be modified
Syntax:
Const data_type variable_name;
Static Data Members
Two types of members Example:
• Static Const int a;
▪ Assign value when we
initialize Static Member function
▪ Scope object will same
Declare with keywords static
▪ Concept of const will not
Can be access with/without creating of objects of class
applied
Virtual function can't use as static
▪ Will use in management
Static member functions are mostly used to store
▪ We can access then
information that are shared to all the objects.
without creating object
They can't be accessed by pointer based objects
▪ Memory management
▪ Static keyword
• Normal
▪ Assign value
▪ Scope of object is
different
• Const
▪ Fix value
▪ Will not upgrade
▪ Scope will different
▪ Can't be modified
Lecture by Sir
Static Data members
• Use static keywords
• Scope of static data member
remain same for all objects of
class
• Static data members can use for
memory management
• Static Data Member can access
without creating objects
• Can Initialize before main().
Friend Function (Concept)
Friend function is a function that access private /
protected class members
Private and protected are used in data hiding
We can't use it as a normal function and use special
function for it
We do not need use to dot operator to access it
Class object is passed as an argument
Disadvantage: if using multiple friend function data
can be breach
• Advantages
Accessible member of class without
inheritance
Act like bridge between classes
• Disadvantages
Violates the law of data-hiding
Finals Example:
Inheritance (Concept)
Use properties of one class to other
Relationship of inheritance
• Single level: one child class one parent
• Multi-level: one parent multiple child
• Multiple level: two parents and one child
class
• Hybrid: multiple parent class multiple
children class
Lecture by sir:
The process of accessing the data member, member
function of one class in another class
Parent class (Base Class)
Child Class(Derived Class)
Levels of Inheritance
Single level inheritance (One to One)
Multi-level inheritance (One to Many)
Multiple level inheritance (Many to One)
Hybrid level inheritance (Many to Many)
Private:
Member can't be accessed from outside the class i.e. members are private to that class only
Protected:
Member can't be accessed from outside the class but they can be accessed in inherited class or derived classes
Syntax:
Class A
{
Public:
//member functions
Protected:
//member function
Private:
//member function
};
Class B: public A
Class B private A
Class B: protected A
Protected Inheritance
Accessibility in protected inheritance
Accessibility Private member protected member Public member
Private Inheritance
Accessibility in private inheritance
Accessibility Private member protected member Public member
Lecture by sir:
Pointer:
Is variable that store the memory address
Is a variable that store the address of other variable
Example:
#include <iostream>
using namespace std;
int main()
{
int x = 30;
int *ptr;
ptr = &x;
Polymorphism (concept)
Polymorphism means many forms
Aik he per son teacher b hota hay son b hota hay to ye
aik he person ki many forms hoti hain
Same like object has many forms according to
conditions
Object behaves differently according to condition
Lecture by sir
Polymorphism
Many forms
The ability of function/object behave different
according to condition
Operator Overloading void show()
{
Built in data type operators for data types (string, int) are [*,+,==] etc.
cout << "Volume of n = " << n;
When we apply operators in class they are different
}
Operator based Function need to be declared like two objects increment
We need to make a function to do that
void operator++()
Operator based function must not be static
{
Unary operator
n = n + 1;
• That have one Operand
}
Binary, Comparison Operators
};
Lecture by Sir
int main()
The way to changing operator to for user define data type (class/struct) {
Number obj;
Rules for operator Overloading obj.show();
• For Normal Function binary operator have one argument and for ++obj;
Unary Operator No Argument obj.show();
• For Friend Function Binary operator have two arguments and unary }
operator have one argument
Operator Overloading with return type
• Operator function must be global/friend function
• These operators can’t be overload: #include <iostream>
. (dot Operator) using namespace std;
, (Comma Operator) class Number
:: (Resolution Operator) {
? (Question Operator) int n;
:
Unary Operator Overloading public:
That Have one operand Number()
{
Syntax: n = 5;
}
void show()
return_type operator symbol()
{
{
cout << "Volume of n = " << n;
//statement;
}
}
Number operator--()
Code: {
Number temp;
#include <iostream> n--;
using namespace std; temp.n = n;
class Number return temp;
{ }
int n; };
int main()
public: {
Number() Number obj1, obj2;
{ obj1.show();
n = 5; obj2.show();
}
obj2 =--obj1;
obj1.show();
Without return type obj2.show();
With return type }
Pointer to Object (concept)
int main()
Object is like a variable
{
We can Also access objects from pointer test obj;
test *pobj;
Lecture by sir pobj = &obj;
Code: (*pobj).in();
(*pobj).out();
Method # 1: }
#include <iostream>
using namespace std; Method # 3:
#include <iostream>
class test using namespace std;
{
int n; class test
{
public: int n;
void in()
{ public:
cout << "Enter Value of n: "; void in()
cin >> n; {
} cout << "Enter Value of n: ";
cin >> n;
void out() }
{
cout << "The value of n: " << n; void out()
} {
}; cout << "The value of n: " << n;
int main() }
{ };
test obj; int main()
test *pobj; {
pobj = &obj; test *pobj;
pobj = new test;
pobj->in(); pobj->in();
pobj->out(); pobj->out();
} }
public:
void in()
{
cout << "Enter Value of n: ";
cin >> n;
}
void out()
{
cout << "The value of n: " << n;
}
};
Overriding Virtual Function
Same name Same type of parameter then • Existing in effect but not in reality
program execute • Use to access the runtime polymorphism
Use in inheritance • Define in base class but override in child class
Function definition of overriding changed • Enable The user to access the different function with same
in Derived class function call
But overloading can be use everywhere • Do not use virtual function as static
• Don't use as constructor but use as destructor
Lecture by Sir
We declare the function in Base and Example:
derived Class with same name and #include <iostream>
parameter (Signature/Argument) using namespace std;
int main()
{
animal *ptr;
cow c;
ptr = &c;
ptr -> print();
}
Pure Virtual Function
• Through pure virtual function we don't access class directly
• Pure virtual function can declare but not define
• Have no body
• Use to create a generic object
Example (on vs code)
• Early binding
• Late binding
Early binding happens in compile time, while late binding during runtime.
• Concreate class
• Abstract class
Abstraction Inheritance:
• Is-a->relationship
Is defined as delivering only essential information
Association(Many to Many):
Only provide user that he knows
Virtual function is used in abstraction • Has-a->relationship
• Relationship between two classes that
Lecture by sir: established through their objects
Example:
Composition • Building- Room
• Engine-Car