Object Oriented Programming
The concepts of Object-Oriented Programming (OOP) in C++ and cover classes, objects, constructors, destructor, access modifiers, encapsulation, friend functions, and friend classes, along with examples for each.
IMAGE SOURCE :- TECHFLY.COM
C++ Classes and Objects
Classes: A class is a blueprint for creating objects. It defines a datatype by bundling data and methods that work on the data into a single unit.
Objects: Objects are instances of a class. When a class is defined, no memory is allocated until an object of that class is created.
Example:
using namespace std;
class Car {
public:
string brand;
string model;
int year;
};
int main() {
Car car1;
car1.brand = "Toyota";
car1.model = "Corolla";
car1.year = 2020;
cout << car1.brand << " " << car1.model << " " << car1.year << endl;
return 0;
}
C++ Constructors
Constructors: Constructors are special member functions of a class that initialize objects of that class. They have the same name as the class and do not have a return type.
Example:
using namespace std;
class Car {
public:
string brand;
string model;
int year;
// Constructor
Car(string b, string m, int y) {
brand = b;
model = m;
year = y;
}
};
int main() {
Car car1("Toyota", "Corolla", 2020);
cout << car1.brand << " " << car1.model << " " << car1.year << endl;
return 0;
}
C++ Constructor Overloading
Constructor Overloading: Constructor overloading is a concept where you can have multiple constructors in the same class with different parameter lists.
Example:
using namespace std;
class Car {
public:
string brand;
string model;
int year;
// Default constructor
Car() {
brand = "Unknown";
model = "Unknown";
year = 0;
}
// Parameterized constructor
Car(string b, string m, int y) {
brand = b;
model = m;
year = y;
}
};
int main() {
Car car1;
Car car2("Toyota", "Corolla", 2020);
cout << car1.brand << " " << car1.model << " " << car1.year << endl;
cout << car2.brand << " " << car2.model << " " << car2.year << endl;
return 0;
}
C++ Destructors
Destructors: Destructors are special member functions that are executed when an object is destroyed. They have the same name as the class, preceded by a tilde (~
), and they do not return any value.
Example:
using namespace std;
class Car {
public:
string brand;
string model;
int year;
// Constructor
Car(string b, string m, int y) {
brand = b;
model = m;
year = y;
cout << "Car object created." << endl;
}
// Destructor
~Car() {
cout << "Car object destroyed." << endl;
}
};
int main() {
Car car1("Toyota", "Corolla", 2020);
return 0;
}
C++ Access Modifiers
Access Modifiers: Access modifiers define the accessibility of the members of a class. The three main access modifiers in C++ are public
, private
, and protected
.
Example:
using namespace std;
class Car {
private:
string brand;
string model;
int year;
public:
void setDetails(string b, string m, int y) {
brand = b;
model = m;
year = y;
}
void getDetails() {
cout << brand << " " << model << " " << year << endl;
}
};
int main() {
Car car1;
car1.setDetails("Toyota", "Corolla", 2020);
car1.getDetails();
return 0;
}
C++ Encapsulation
Encapsulation: Encapsulation is the concept of wrapping data and methods that operate on the data within a single unit, typically a class. It restricts direct access to some of the object’s components.
Example:
using namespace std;
class Car {
private:
string brand;
string model;
int year;
public:
void setBrand(string b) {
brand = b;
}
string getBrand() {
return brand;
}
void setModel(string m) {
model = m;
}
string getModel() {
return model;
}
void setYear(int y) {
year = y;
}
int getYear() {
return year;
}
};
int main() {
Car car1;
car1.setBrand("Toyota");
car1.setModel("Corolla");
car1.setYear(2020);
cout << car1.getBrand() << " " << car1.getModel() << " " << car1.getYear() << endl;
return 0;
}
C++ friend Function and friend Classes
friend Functions: A friend function is a function that is not a member of a class but has access to the class’s private and protected members.
Example:
using namespace std;
class Car {
private:
string brand;
string model;
int year;
public:
Car(string b, string m, int y) : brand(b), model(m), year(y) {}
// Declare friend function
friend void displayDetails(Car &c);
};
void displayDetails(Car &c) {
cout << c.brand << " " << c.model << " " << c.year << endl;
}
int main() {
Car car1("Toyota", "Corolla", 2020);
displayDetails(car1);
return 0;
}
friend Classes: A friend class can access the private and protected members of another class in which it is declared as a friend.
Example:
using namespace std;
class Engine {
private:
int horsepower;
public:
Engine(int hp) : horsepower(hp) {}
friend class Car; // Declare Car as a friend class
};
class Car {
private:
string brand;
string model;
int year;
Engine engine;
public:
Car(string b, string m, int y, int hp) : brand(b), model(m), year(y), engine(hp) {}
void displayDetails() {
cout << brand << " " << model << " " << year << " " << engine.horsepower << " HP" << endl;
}
};
int main() {
Car car1("Toyota", "Corolla", 2020, 132);
car1.displayDetails();
return 0;
}
0 Comments