Classes Objects
Classes Objects
Intro
int main() {
MyClass myObj; // Create an object of MyClass
myObj.myMethod(); // Call the method
return 0;
Class Methods
To define a function outside the class definition, you have to declare it
inside the class and then define it outside of the class. This is done by
specifying the name of the class, followed the scope resolution ::
operator, followed by the name of the function:
class MyClass { // The class
public: // Access specifier
void myMethod(); // Method/function declaration
};
int main() {
MyClass myObj; // Create an object of MyClass
myObj.myMethod(); // Call the method
return 0;
}
Constructors
int main() {
MyClass myObj; // Create an object of MyClass (this will call the
constructor)
return 0;
}
Constructor Parameters
Constructors can also take parameters (just like regular functions), which can be useful for
setting initial values for attributes.
class Car { // The class
public: // Access specifier
string brand; // Attribute
string model; // Attribute
int year; // Attribute
Car(string x, string y, int z) { // Constructor with parameters
brand = x;
model = y;
year = z;
}
};
int main() {
// Create Car objects and call the constructor with different values
Car carObj1("BMW", "X5", 2020);
Car carObj2("Ford", "Mustang", 1969);
// Print values
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
return 0;
}
Access Specifiers
The public keyword is an access specifier. Access specifiers define how the
members (attributes and methods) of a class can be accessed. In the
example above, the members are public - which means that they can be
accessed and modified from outside the code.
However, what if we want members to be private and hidden from the
outside world?
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. You will learn more about Inheritance later.
By default, all members of a class are private if you don't specify an
access specifier
In the following example, we demonstrate the differences between public
and private members:
Access Specifiers Example
class MyClass {
public: // Public access specifier
int x; // Public attribute
private: // Private access specifier
int y; // Private attribute
};
int main() {
MyClass myObj;
myObj.x = 25; // Allowed (public)
myObj.y = 50; // Not allowed (private)
return 0;
}
Encapsulation
// Derived class
class Car: public Vehicle {
public:
string model = "CLS";
};
int main() {
Car myCar;
myCar.honk();
cout << myCar.brand + " " + myCar.model;
return 0;
}
Note About Inheritance
// Derived class
class Pig : public Animal {
public:
void animalSound() {
cout << "The pig says: wee wee \n";
}
};
// Derived class
class Dog : public Animal {
public:
void animalSound() {
cout << "The dog says: bow wow \n";
}
};
Destructor