0% found this document useful (0 votes)
12 views10 pages

C

The document provides an overview of Object-Oriented Programming (OOP) concepts including encapsulation, abstraction, inheritance, and polymorphism. It explains key components such as classes, objects, access modifiers, constructors, and copy constructors, along with examples in C++. Additionally, it discusses shallow and deep copies, destructors, and the principles of inheritance and polymorphism in OOP.
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)
12 views10 pages

C

The document provides an overview of Object-Oriented Programming (OOP) concepts including encapsulation, abstraction, inheritance, and polymorphism. It explains key components such as classes, objects, access modifiers, constructors, and copy constructors, along with examples in C++. Additionally, it discusses shallow and deep copies, destructors, and the principles of inheritance and polymorphism in OOP.
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/ 10

/**/

/*
OOPS:
1-->Encapsulation
2-->Abstraction
3-->Ineritence
4-->Polymorphism
*/

/*
ENCAPSULATION:
it is the weapping up of data & member functions in a single unit called class.
Practically a class with functions and properties is encapsulation
*helps in hiding private datas using privates anol.
*/

/*
Objects - the entities in the real world;; eg pen, lap, phone
Class - its like a blueprint of these entities

Toyota has a blueprint(class) for making a car here the cars(entities) might be different but all
belongs to the same blueprint(class)

CASE:1
imagine a college system
Teacher(object)-->Properties--> Name, dept, subject, salary + changeDept()(this fuction is technically
a method)
so here there will be multiple teachers and each teacher uses the same blueprint so make a common
class and create no of objects as necessary

CASE:2
imagine amazon's webpage where there are many products(object i.e entity)-->properties--> name,
pricem description, discount
do here too we can create a class and set no of products
*/

/*
ACCESS MODIFIERS
private : data & methods accessible inside the particular class alone
public : data & methods accessible to everyone
protected : data & methods accessible inside class & to its derived class (chk inheritence comments)
*/

/*Constructors
Special method invoked automatically at time of object creation. used for initialisation
*it has same name as class
*constructr doesn't have a return type
*only called once(automatically), at the time of object creation
* Memory allocation happens when the constructor is called not when the var is defined int the
class.
* Constructors are called automatically when each object of the class is called
It does not have a return type (not even void).

Has 3 types
-->Non parameterized
-->Paramaterized
-->Copy

*/

#include <iostream>
#include<string>
using namespace std;

//here by default everything inside the class will be private inorder to use it in main class we should
make it to public
class Teacher{
private:
double salary; // so the salary data cant be accessed by the main class

public:
//non-parameterized costructor
Teacher(){
Dept="Computer Science"
cout<<"Hi i am costructor\n";
}
//properties or attributes
string name;
string dept;
string subject;
string Dept;

//methods or functions
void changeDept(string newDept){
dept=newDept;
}

//setter(sets the private values)


void setSalary(double s){
salary = s;
}

//getter(now the salary can be used from main class using getter)
double getSalary(){
return salary;
}

//here salary is not accessed directly instead they are accessed using public functions from the
same class
};

class Student{
public:
string name;
int rollno;
int age;

};

class Account{

private:
double salary;
string password;

public:
string accID;
string username;
//double balance; these things needs to be hidden
//string password;
};

int main(){
Teacher t1;
Teacher t2;
//to access a value from a class to access its prop we will use dot operator
t1.name="Sir";
t1.subject="C";
t1.dept="CS"; //inorder to give the dept here we can give it inside the constructor and it will be
assingned for all the objects when called

cout<<t1.name<<endl;
cout<<t1.getSalary()<<endl;
return 0;

//parameterized constructor
class Teacher{
private:
double salary;

public:
Teacher(string n, string d, string s, double sal){
ame = n;
dept = d;
subject s;
salary = sal;
}
string name;
string dept;
string subject;

void getInfo(){
cout<<"Name :" << name<< endl;
cout<<"Subject : "<<subject<<endl;
}
};

int main(){
Teacher t1("sir","CS","C++",25000);
t1.getInfo();
return 0;
}

//copy constructor
special constructor used to copy the properties of one object to another

//If there are more than one constructor in a class with same name but with diff values or
parameters there will be no error
//this case with multiple constructor with same name and diff type causes constructor overloading it
is an example of POLYMORPHISM

//COPY CONSTRUCTOR
class Teacher{
private:
double salary;

public:
Teacher(string n, string d, string s, double sal){
ame = n;
dept = d;
subject s;
salary = sal;
}
string name;
string dept;
string subject;

Teacher(Teacher &orgObj){ //pass by reference


cout<<"This is a custom copy constructor"<<endl;
this->name = orgObj.name;
this->dept = orgObj.dept;
this->subject = orgObj.subject;
this->salary = orgObj.salary}
void getInfo(){
cout<<"Name :" << name<< endl;
cout<<"Subject : "<<subject<<endl;
}
};

int main(){
Teacher t1("sir","CS","C++",25000);
Teacher t2(t1);//if the above custom copy is not mentioned it is default copy constructor invoke
else it is custom copy constructor invoke
t2.getInfo();
return 0;
}

/*
THIS
this-> is used when both the object's property and the function's parameter have the same name

Teacher(string n, string d, string s, double sal){


ame = n;
dept = d;
subject s;
salary = sal;
}

the above can also be written as

Teacher(string name, string dept, string subject, double salary){


this->name=name;
this->dept = d;
this->subject = subject;
this->salary = salary;
}
here the left one is object's property and the right one is function's parameter

this->prop is same as *(this).prop


*/
Shallow and Deep copy

A shallow copy of an object copies all of the member values from one object to another.

A deep copy, on the othen hand, not only copies the member values but also makes copies of any
dynamically allocated memory that the members point to.

there will be no prob in shallow copy untill the dynamic memory allocation is also considered like
the times when using pointers

eg code

class Student{

public:
string name:
double cgpa;

Student(string name, double cgpa){


this-name=name;
this->cgpa=cgpa;
}

Student(Student &obj){//just making a custom copy constructor


this->name=obj.name;
this->cgpa=obj.cgpa;
}

void getInfo(){
cout<<"name: "<<name<<endl;
cout<<"cgpa: "<<cgpa<<endl;
}
};

int main(){
Student s1("Rahul kumar", 8.9);
Student s2(s1);
s2.getInfo();
return 0;
}

//the above works perfectly but when it comes to pointers

class Student{
public:
string name:
double* cgpaPtr;

Student(string name, double cgpa){


this->name=name;
cgpaPtr = new double;
*cgpaPtr=cgpa;
}

Student(Student &obj){//just making a custom copy constructor


this->name=obj.name;
this->cgpa=obj.cgpa;
}

void getInfo(){
cout<<"name: "<<name<<endl;
cout<<"cgpa: "<<*cgpaPtr<<endl;
}
};

int main(){
Student s1("Rahul kumar", 8.9);
Student s2(s1);
s1.getInfo();
*(s2.cgpaPtr)=9.2;
s1.getInfo();//therefore the cgpa will be 8.9 in the start and 9.2 in the 2nd call but actually there
should be no change it happens due to dynamic mem alloc as shallow copy is done here

// so when a shallow copy is done the address for the copied s2 will be same as the s2 so if the
value of s2 changed the value of s1 also changes
return 0;
}

to make a deep copy alter the copy constructer like

Student(Student &obj){//just making a custom copy constructor


this->name=obj.name;
cgpaPtr= new double;
*cgpaPtr=*obj.cgpaPtr;
}
//to dynamically allocate smtg we use new
//to dynamically deallocate smtg we use delete

//DESTRUCTOR:opp of constructor so it deallocate the objects


class Student{

public:
string name:
double* cgpaPtr;

Student(string name, double cgpa){


this->name=name;
cgpaPtr = new double;
*cgpaPtr=cgpa;
}

Student(Student &obj){//just making a custom copy constructor


this->name=obj.name;
cgpaPtr= new double;
*cgpaPtr=*obj.cgpaPtr;
}

void getInfo(){
cout<<"name: "<<name<<endl;
cout<<"cgpa: "<<*cgpaPtr<<endl;
}
//destructor
~Student(){
cout<<"Destructor called";
delete cgpaPtr;//to deallocate the allocated one there is no change in o/p but in memory used
in dsa.
}
};

int main(){
Student s1("Rahul kumar", 8.9);
s1.getInfo();
return 0;//after this destructor will be called
}

//INHERITENCE : when the properties & member functions of base class are passed on to the derived
class.
class A-> class B here A is parent or base and B is child or derived

eg

class Prson{
public:
string name;
int age;

Person(string name, int age){


this->name=name;
this->age=age;
}
};

class Student{
//name age roll no
// we def already so we can inherit
}

soo
class Prson{
public:
string name;
int age;

Person
}
};
class Student: public Person{
public:
int rollno;
void getInfo(){
cout <<"Name : "<<name<<endl;
cout <<"Age : "<<age<<endl;
cout<<"rollno: "<<rollno<<endl;
}
};

int main(){
Student s1;
s1.name="rahul";
s1.age=21;
s1.rollno=123;

s1.getInfo();
return 0;
}

// First parent class's constructor will be called then the child's class's constructor will be called
//opp for destructor

POLYMORPHISM
it is the aility of objects to take on different forms or behave in different ways depending on the
context in which they are used

2 type compile time and run time

i.e depending on the context costructor, function, operator, will be called

for run time both parent and child has same function with diff implementations

ABSTRACT
hiding all unecessay details & showing only the imt parts

You might also like