3/23/23, 10:55 PM (10) Quick Guide for OOP in C++ - LeetCode Discuss
New
Explore Problems Interview Contest Discuss Store Premium
brand = x;
model = y;
year = z;
}
};
Just like functions, constructors can also be defined outside the class.
class Car {
public:
string brand;
string model;
int year;
// Constructor declaration
Car(string x, string y, int z);
};
// Constructor definition outside the class
Car::Car(string x, string y, int z) {
brand = x;
model = y;
year = z;
}
Access Specifiers
In C++, there are three access specifiers:
Public - Members are accessible from outside the class
Private - Members cannot be accessed (or viewed) from outside the class
Protected - members cannot be accessed from outside the class, however, they can be accessed in inherited classes.
KEY Points: By default class are private if you don't specify an access specifier
Encapsulation
Encapsulation is defined as the wrapping up of data under a single unit.
It is the mechanism that binds together code and the data.
Hidding the sensitive Data from user.
Encapsulation ensures better control of your data, because you (or others) can change one part of the code without a
To achieve this Class/Variables/Attributes declare as private.
KEY Points: To access a private attribute, use public "get" and "set" methods
class Employee {
private:
int salary;
public:
void setSalary(int s) {
salary = s;
}
int getSalary() {
return salary;
https://leetcode.com/discuss/study-guide/3293285/Quick-Guide-for-OOP-in-C%2B%2B 1/1