Classes and Objects
Expert Answer & Key Takeaways
Understanding the building blocks of C++ OOP: Defining classes, creating objects, access specifiers, and member functions.
Classes and Objects in C++
In C++, a Class is the fundamental building block of Object-Oriented programming. It is a user-defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. An Object is an instance of a Class.
1. What is a Class?
A class is a blueprint or template for creating objects. It doesn't consume any memory space when it is defined. It is simply a logical representation.
- Analogy: A blueprint of a house. The blueprint itself is not a physical house; it just defines what the house will look like and what rooms it will have.
1.1 Defining a Class
A class is defined in C++ using the keyword
class followed by the name of the class. The body of the class is enclosed inside curly brackets and terminated by a semicolon at the end.class Car {
// Access specifier
public:
// Data Members (Attributes)
string brand;
int speed;
// Member Functions (Methods)
void accelerate() {
speed += 10;
}
}; // <-- Important semicolon!2. What is an Object?
An Object is an identifiable entity with some characteristics and behavior. It is a physical reality that occupies memory in the computer.
- Analogy: The actual physical house built from the blueprint. You can build many houses (objects) from one blueprint (class).
2.1 Creating and Using an Object
Once a class is defined, you can create objects of that type. To access the data members and member functions of a class, you use the dot (
.) operator with the object.int main() {
// Creating an object of class Car
Car myCar;
// Accessing data members
myCar.brand = "Toyota";
myCar.speed = 50;
// Accessing member function
myCar.accelerate();
return 0;
}3. Access Specifiers (Visibility Modes)
Access specifiers define how the members (attributes and methods) of a class can be accessed. This implements the concept of Data Hiding / Encapsulation.
C++ provides three access specifiers:
public: Members are accessible from outside the class anywhere within the program.private: Members cannot be accessed (or viewed) from outside the class. Only the class's own member functions can access them. By default, all members of a class are private if no specifier is mentioned.protected: Similar to private, but the members can be accessed in child classes (derived classes) during Inheritance.
Example of Access Specifiers
class Employee {
private:
int salary; // Hidden from outside world
public:
// Setter function to modify private data safely
void setSalary(int s) {
if (s > 0) salary = s;
}
// Getter function to read private data
int getSalary() {
return salary;
}
};4. Member Functions Outside the Class
Member functions can be defined either inside the class definition or outside the class definition.
To define a member function outside the class, you must use the Scope Resolution Operator (
::) to link the function name to the class it belongs to.class Calculator {
public:
int add(int a, int b); // Only declaration inside
};
// Definition outside using scope resolution
int Calculator::add(int a, int b) {
return a + b;
}5. Struct vs Class in C++
In C++, a
struct is almost identical to a class. They can both contain functions, constructors, and data. The ONLY difference is the default access specifier:- In a
struct, members arepublicby default. - In a
class, members areprivateby default.
Course4All Editorial Board
Verified ExpertSubject Matter Experts
Comprising experienced educators and curriculum specialists dedicated to providing accurate, exam-aligned preparation material.
Pattern: 2026 Ready
Updated: Weekly
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.