C++ For Programmers - Object-Oriented Programming in C++ Cheatsheet - Codecademy
C++ For Programmers - Object-Oriented Programming in C++ Cheatsheet - Codecademy
int main() {
Dog buddy;
buddy.age = 5;
int main()
{
Computer dell;
dell.setPassword(12345);
std::cout << dell.getPassword();
return 0;
}
Constructors
For a C++ class, a constructor is a special kind of method #include <iostream>
that enables control regarding how the objects of a class
should be created. Different class constructors can be
specified for the same class, but each constructor using namespace std;
signature must be unique.
A constructor can have multiple parameters as well as
class House {
default parameter values.
In order to initialize const or reference type private:
attributes, use member initializer lists instead of normal std::string location;
constructors.
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)
House big_florida_house("Florida", 10);
// Calls House("Florida", 10)
return 0;
}
Inheritance
In C++, a class can inherit attributes and methods from #include <iostream>
another class. In an inheritance relationship, there are
two categories of classes:
Base class: The class being inherited from. class Base {
Derived class: The class that inherits from the public:
base class.
int base_id;
It’s possible to have multi-level inheritance where classes
are constructed in order from the “most base” class to
the “most derived” class. Base(int new_base) : base_id(new_base)
{}
};
void show() {
std::cout << base_id << " " <<
derived_id;
}
};
int main() {
Derived temp(1, 2);
temp.show(); // Outputs: 1 2
return 0;
}
Polymorphism
In C++, polymorphism occurs when a derived class #include <iostream>
overrides a method inherited from its base class with the
same function signature.
Polymorphism gives a method many “forms”. Which form class Employee {
is executed depends on the type of the caller object. public:
void salary() {
std::cout << "Normal salary.\n";
}
};
int main() {
Employee newbie;
Manager boss;
return 0;
}
Class Members
A class is comprised of class members: class City {
Attributes, also known as member data, consist of
information about an instance of the class.
Methods, also known as member functions, are // Attribute
functions that can be used with an instance of the int population;
class.
public:
// Method
void add_resident() {
population++;
}
};
Constructor
For a C++ class, a constructor is a special kind of method #include "city.hpp"
that enables control regarding how the objects of a class
should be created. Different class constructors can be
specified for the same class, but each constructor class City {
signature must be unique.
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 City nyc;
encapsulates data and functionality pertaining to that
data.
Class
A C++ class is a user-defined data type that encapsulates class Person {
information and behavior about an object. It serves as a
blueprint for future inherited classes.
};
Access Control Operators
C++ classes have access control operators that designate class City {
the scope of class members:
public
private int population;
public members are accessible everywhere;
private members can only be accessed from within public:
the same instance of the class or from friends classes.
void add_resident() {
population++;
}
private:
bool is_capital;
};
Print Share