Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views4 pages

Oops

Object-Oriented Programming (OOP) is a programming paradigm that focuses on objects, which encapsulate states and behaviors through classes. Key concepts include encapsulation, inheritance, polymorphism, and abstraction, with access modifiers (public, private, protected) controlling visibility. Constructors and destructors manage object lifecycle, while static members and functions provide class-level properties without needing an instance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Oops

Object-Oriented Programming (OOP) is a programming paradigm that focuses on objects, which encapsulate states and behaviors through classes. Key concepts include encapsulation, inheritance, polymorphism, and abstraction, with access modifiers (public, private, protected) controlling visibility. Constructors and destructors manage object lifecycle, while static members and functions provide class-level properties without needing an instance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Object-Oriented Programming

This is a paradigm/technique of programming which revolves around the object, it tries to bridge
the gap between computer programming and real world objects.

Object is an entity(instance of class) ——> States/Properties


——> Behaviours

Classes are custom datatypes created by us.

class Car{
int tire1, tire2, tire3, tire4; //properties
};

int main{
Car nano;
cout << sizeof(nano) << endl; //Always gives the sum of size of all var in class(+padding)
}

Size of empty class is 1 for identi cation.

Access modi ers are: Public, Private(Default) and Protected


1. Public - Can be used inside and outside of class.
2. Private - Can only be inside the class. (Getter, Setter)
3. Protected - Can be used inside a class and by a derived class.

Getter and Setter Functions can be used in private access modi ers.

//Static Allocation
Car nano;
cout << nano.tire1 << endl;

//Dynamic Allocation
Car* comet = new Car;
cout << (*comet).tire1 << endl;
cout << comet—>tire1 << endl;

Constructors
class Car{
int tire1, tire2, tire3, tire4; //properties

Car() {
cout << “Constructor Called”;
}

// Copy Constructor
Car(Car& temp){
this—>tire1 = temp.tire1;
}

// Parametrised constructor
Car(int tire1) {
this—>tire1 = tire1; // this keyword points to address of current object
}

~Car() {
cout << “Destructor Called”;
}
};
fi
fi
fi
Object-Oriented Programming

If we make even one parametrised constructor then the default constructor is removed and we
have to explicitly write it again.

Deep copy vs Shallow copy

Destructor is automatically called for statically created objects.


But objects created by dynamic allocation will be deleted manually. //delete comet;

Static keyword makes a property which belongs to class and we don’t have to create object for it.
static int validity;
int Car::validity = 15; // “::” is scope resolution operator

Static functions: No need to create object and no this keyword(because this points to the address
of object). They can only access static members.

Encapsulation
Wrapping up of Data members and functions.
Main purpose of encapsulation is hiding information.

Advantages
Data hiding and security
Read-Only with getters
Code Reusability
Unit Testing

Inheritance
Human - Height, Weight, Age // Parent class or super class
Male and Female class inherits from Human. // Child class or sub class or derived class

class Human {
public:
int height;
int weight;
int age;

public:
int getAge(){
return this->age;
}
void setWeight(){
this—>weight = w;
}
};

class Man: public Human {


public:
string color;

public:
void attack(){
cout << “Growl” << endl;
}
};
Object-Oriented Programming
Super Class Modi er Access Modi er Mode Sub Class Modi er
Public Public Public
Public Protected Protected
Public Private Private
Protected Public Protected
Protected Protected Protected
Protected Private Private
Private Public Not accessible
Private Protected Not accessible
Private Private Not accessible

Types of Inheritance
1. Single: A —> B, example above.

2. Multi-level: A —> B —> C, Human —> Male —> Employee

3. Multiple: A —|__ C
B —|

class Hybrid: public Human, public Animal {

};

4. Hierarchical: A —> B and A —> C

5. Hybrid: Combination of more than one inheritance.

Inheritance ambiguity: It happens when you inherit 2 functions/parameters from 2 classes with
same name.
Avoid the above using scope resolution operator.
• C Obj;
• Obj.A :: func()
• Obj.B :: func()

Polymorphism
Many-forms.

1. Compile-time polymorphism
1. Function Overloading
So we can achieve this by changing the number of arguments, changing the types of
arguments and by using default arguments.

2. Operator Overloading, so that we can have our operator be applied on objects.


void operator+ (B &obj) {
int value1 = this -> a;
int value2 = obj.a;
cout << "output " << value2 - value1 << endl;

cout << "Hello Babbar" << endl;


}
fi
fi
fi
Object-Oriented Programming
2. Run-time polymorphism - method overriding and inheritance dependence.
Rules - Same function name, same function arguments and inheritance.

class Animal {
public:
void speak() {
cout << "Speaking "<< endl;
}
};
class Dog: public Animal {
public:
void speak() {
cout << "Barking " << endl;
}
};

Abstraction
Implementation Hiding

You might also like