oop_cppp
oop_cppp
Class Members
public:
// Method
void add_resident() {
population++;
}
};
Constructor
For a C++ class, a constructor is a special kind of method that enables control #include "city.hpp"
regarding how the objects of a class should be created. Di�erent class
constructors can be speci�ed for the same class, but each constructor signature
must be unique. class City {
std::string name;
int population;
public:
City(std::string new_name, int new_pop);
};
Objects
In C++, an object is an instance of a class that encapsulates data and City nyc;
functionality pertaining to that data.
Class
A C++ class is a user-de�ned data type that encapsulates information and class Person {
behavior about an object. It serves as a blueprint for future inherited classes.
};
C++ classes have access control operators that designate the scope of class class City {
members:
• public
• private int population;
public members are accessible everywhere; private members can only be
accessed from within the same instance of the class or from friends classes. public:
void add_resident() {
population++;
}
private:
bool is_capital;
};
Constructors
For a C++ class, a constructor is a special kind of method that enables control #include <iostream>
regarding how the objects of a class should be created. Di�erent class
constructors can be speci�ed for the same class, but each constructor signature
must be unique. using namespace std;
A constructor can have multiple parameters as well as default parameter values.
In order to initialize const or reference type attributes, use member initializer
class House {
lists instead of normal constructors.
private:
std::string location;
int rooms;
public:
// Constructor with default parameters
House(std::string loc = "New York", int num = 5) {
location = loc;
rooms = num;
}
// Destructor
~House() {
std::cout << "Moved away from " << location << "\n";
}
};
int main()
{
House default_house; // Calls House("New York", 5)
House texas_house("Texas"); // Calls House("Texas",
5)
return 0;
}
Inheritance
In C++, a class can inherit attributes and methods from another class. In an #include <iostream>
inheritance relationship, there are two categories of classes:
• Base class: The class being inherited from.
• Derived class: The class that inherits from the base class. class Base {
It’s possible to have multi-level inheritance where classes are constructed in public:
order from the “most base” class to the “most derived” class.
int base_id;
void show() {
std::cout << base_id << " " << derived_id;
}
};
int main() {
Derived temp(1, 2);
temp.show(); // Outputs: 1 2
return 0;
Access Speci�ers
Access speci�ers are C++ keywords that determine the scope of class #include <iostream>
components:
• public : Class members are accessible from anywhere in the program.
• private : Class members are only accessible from inside the class. class Computer {
Encapsulation is achieved by declaring class attributes as private : private:
• Accessor functions: return the value of private member variables. int password;
• Mutator functions: change the value of private member variables.
public:
int getPassword() {
return password;
}
int main()
{
Computer dell;
dell.setPassword(12345);
std::cout << dell.getPassword();
return 0;
}
A C++ class is a user-de�ned data type that encapsulates information and #include <iostream>
behavior about an object.
A class can have two types of class members:
• Attributes, also known as member data, consist of information about an class Dog {
instance of the class. public:
• Methods, also known as member functions, are functions that can be
int age;
used with an instance of the class.
An object is an instance of a class and can be created by specifying the class
name. void sound() {
std::cout << "woof\n";
}
};
int main() {
Dog buddy;
buddy.age = 5;
Polymorphism
In C++, polymorphism occurs when a derived class overrides a method inherited #include <iostream>
from its base class with the same function signature.
Polymorphism gives a method many “forms”. Which form is executed depends
on the type of the caller object. class Employee {
public:
void salary() {
std::cout << "Normal salary.\n";
}
};
int main() {
Employee newbie;
Manager boss;
return 0;
}
Print Share