Classes
COS2614 - Programming: Contemporary Concepts · Introduction to C++ and Qt 4
Classes
In C++, a class is a blueprint for creating objects. It defines the properties (attributes) and behaviours (methods) of the objects you create. Understanding classes is essential because they form the foundation of object-oriented programming (OOP). This allows for better code organization, reusability, and abstraction.
Key idea: After this topic, you should be able to define a class, create objects from a class, and understand the basic components of classes in C++.
What is a Class?
A class is a user-defined data type. It can contain data members (variables) and member functions (methods). The data members represent the state of an object, while the member functions define the actions that can be performed on the object.
Defining a Class
To define a class in C++, use the following syntax:
class ClassName {
// data members
// member functions
};Here is an example of a simple class:
class Car {
string brand;
int year;
public:
void displayInfo() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};In this example, the class Car has two data members: brand and year. It also has a member function called displayInfo that prints the car's information.
Creating Objects
Once you have defined a class, you can create objects (instances) of that class. You can do this as follows:
Car myCar;
myCar.brand = "Toyota";
myCar.year = 2021;
myCar.displayInfo();This code creates an object called myCar of the class Car, sets its attributes, and calls its member function to display the information.
Access Modifiers
Access modifiers determine the visibility of class members. The three main access modifiers are:
- public: Members are accessible from outside the class.
- private: Members are not accessible from outside the class.
- protected: Members are accessible in the class and by derived classes.
In the Car example, the member function displayInfo is declared as public, allowing it to be accessed from outside the class.
Constructors
A constructor is a special member function that is called when an object is created. It can be used to initialize data members. Here is an example:
class Car {
string brand;
int year;
public:
Car(string b, int y) {
brand = b;
year = y;
}
void displayInfo() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};In this code, the constructor Car(string b, int y) initializes the brand and year attributes when a new Car object is created.
Destructors
A destructor is called when an object goes out of scope. It is used to free resources allocated to the object. The syntax is similar to the constructor, but it uses a tilde (~) before the class name:
~Car() {
// cleanup code
}Example: Complete Class Implementation
Here is a complete example of using a class:
#include <iostream>
#include <string>
using namespace std;
class Car {
string brand;
int year;
public:
Car(string b, int y) {
brand = b;
year = y;
}
void displayInfo() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};
int main() {
Car myCar("Toyota", 2021);
myCar.displayInfo();
return 0;
}This program creates a Car object and displays its information.
Watch out: Remember to declare member functions as public if you want them to be accessible from outside the class. Private members cannot be accessed directly.
Summary
- A class is a blueprint for creating objects.
- Classes can have data members and member functions.
- Access modifiers control the visibility of class members.
- Constructors are used to initialize objects.
- Destructors are used for cleanup.
Check your understanding
- What is the purpose of a class in C++?
- Explain the difference between public and private members in a class.
- What is a constructor, and how is it different from a normal member function?
- What happens when an object goes out of scope in terms of destructors?