2
0
mirror of https://github.com/chubin/cheat.sheets synced 2024-11-05 12:00:16 +00:00
cheat.sheets/sheets/_cpp/class

45 lines
1.0 KiB
Plaintext
Raw Normal View History

2017-07-18 04:35:42 +00:00
// classes are like structs, but everything is by default private.
// Declaration
ObjectName className {
// Sets everything below it to be private.
private:
// Declaration of variables
int a;
string b;
// Implement any private / helper functions below
// Sets everything below it to be public.
public:
// Constructor
structName(new_a, new_b) {
a = new_a;
b = new_b;
}
// accessors or getters functions
int getA() {
return a;
}
string getB() {
return b;
}
// mutators or setter functions
void setA(int new_a) {
a = new_a;
}
void setB(int new_b) {
b = new_b;
}
// Implement any public functions below
};
// Accessing PUBLIC variables
ObjectName v = structName(5, "Hello"); // Creates a struct via the constructor
std::cout << v.a << " " << v.b << std::endl; // Prints to console "5 Hello"