In this tutorial, we will cover everything you need to know about classes in C++, including:
In C++, both class
and struct
are used to define user-defined data types. The key difference lies in their default access specifiers:
private
.public
.// Using class
class MyClass {
int x; // private by default
public:
void setX(int val) { x = val; }
int getX() { return x; }
};
// Using struct
struct MyStruct {
int x; // public by default
void setX(int val) { x = val; }
int getX() { return x; }
};
int main() {
MyClass obj1;
obj1.setX(10); // Accessing public method
// obj1.x = 20; // Error: x is private
MyStruct obj2;
obj2.x = 20; // Accessing public member
obj2.setX(30); // Accessing public method
return 0;
}
A class is defined using the class
keyword. It encapsulates data members (attributes) and member functions (methods).
class ClassName {
// Access specifier
private:
// Data members
dataType member1;
dataType member2;
public:
// Member functions
returnType method1(parameters) {
// Method body
}
returnType method2(parameters);
};
// Definition of method2 outside the class
returnType ClassName::method2(parameters) {
// Method body
}
Access specifiers define the accessibility of class members:
class Box {
private:
double length, width, height;
public:
// Default constructor
Box() : length(0), width(0), height(0) {}
// Parameterized constructor
Box(double l, double w, double h) : length(l), width(w), height(h) {}
// Destructor
~Box() {
std::cout << "Destructor called!" << std::endl;
}
double volume() {
return length * width * height;
}
};
Static members belong to the class rather than any specific object.
class Counter {
private:
static int count; // Static data member
public:
Counter() { count++; }
static int getCount() { return count; } // Static member function
};
int Counter::count = 0; // Initialize static member
Friend functions or classes can access private and protected members of another class.
class Box {
private:
double length;
public:
Box(double l) : length(l) {}
friend void printLength(Box box); // Friend function
};
void printLength(Box box) {
std::cout << "Length: " << box.length << std::endl;
}
Inheritance allows a class to inherit properties and behaviors from another class.
class Animal {
public:
void eat() { std::cout << "Eating..." << std::endl; }
};
class Dog : public Animal {
public:
void bark() { std::cout << "Barking..." << std::endl; }
};
Polymorphism allows a single interface to represent different underlying forms.
class Animal {
public:
virtual void speak() { std::cout << "Animal speaks" << std::endl; }
};
class Dog : public Animal {
public:
void speak() override { std::cout << "Dog barks" << std::endl; }
};
Use the ->
operator to access members of an object through a pointer.
Car *myCar = new Car("Toyota", 2020);
myCar->displayDetails();
delete myCar;
Member initializer lists allow direct initialization of data members in constructors.
class Car {
private:
std::string brand;
int year;
const int id;
public:
Car(std::string b, int y, int i) : brand(b), year(y), id(i) {}
};
This is equivlant to
class Car {
private:
std::string brand;
int year;
const int id;
public:
Car(std::string b, int y, int i) {
brand=b;
year=y;
id=i;
}
};
class
for encapsulation and struct
for plain data structures.private
, protected
, and public
.->
.