0% found this document useful (0 votes)
146 views

Quick Guide For OOP in C++ - LeetCode Discuss

The document discusses object-oriented programming concepts in C++ including classes, constructors, access specifiers, encapsulation, and getter and setter methods. Classes can contain data members and member functions. Constructors initialize objects. Access specifiers include public, private, and protected. Encapsulation binds code and data by hiding sensitive data from users.

Uploaded by

A02Arnab Biswas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
146 views

Quick Guide For OOP in C++ - LeetCode Discuss

The document discusses object-oriented programming concepts in C++ including classes, constructors, access specifiers, encapsulation, and getter and setter methods. Classes can contain data members and member functions. Constructors initialize objects. Access specifiers include public, private, and protected. Encapsulation binds code and data by hiding sensitive data from users.

Uploaded by

A02Arnab Biswas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

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

You might also like