C++ is an Object-Oriented Programming Language and classes and objects are the fundamental components of OOP’s. Everything in C++ is linked with classes and objects. The main purpose of C++ is to introduce the OOPs concept in the C programming language which itself is a powerful language.
Objects are real-life entity and classes are the blueprint for creating an object. For example: consider a class for a car, it has color, some weight, size, etc, these are its attributes and drive, gear, brake, etc are its behaviour.
These attributes and behaviour in a program are variables and functions in a class, also called the class members.
C++ Class
A class is a user-defined blueprint or prototype for creating Objects. It is a logical entity. We can say that classes are categories, and objects are items within each of those categories.
It contains fields, methods, constructors, etc. These members of a class can be accessed and used by creating an instance of that class.
Defining Class:
The definition of the class starts with the keyword class followed by a class name. The body of the class is enclosed within the curly braces and ends with a semicolon. All the members of the class are declared within the body of the class as shown below.
Syntax for class:
1 2 3 4 5 6 7 | class Rectangle { public: //access specifiers float length; // Length of a rectangle float breadth; // Breadth of a rectangle }; |
Explanation:
The class keyword is used to create a class followed by a class name ‘Rectangle‘. the public is one of the access specifiers that are used with the class members to determine their scope that is their visibility outside of the class. There are two public variables length and breadth (attributes). And at the end of braces, a semicolon is provided to end the class.
C++ Objects
Objects are real-world entities that have states and behavior. Example. bike, Lamp, pen, table, etc. An object is an instance of a class. It can be physical and logical.
All the class members are accessed by creating an object of that class. Objects are created at runtime so is called runtime entity.
- State: It is represented by attributes of an object that is the data value.
- Behaviour: It represents the functionality that is the behaviour of an object. Example withdraw, deposit, etc.
1 2 | //creating an object of class Rectangle Rectangle rect; |
Here the type is of Rectangle
type rect
is a reference variable that refers to the instance of a Rectangle class.
Let us understand class and objects in C++ through an example.
Example of C++ Class and Object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <iostream> using namespace std; class StudentDetail //class { public: // Access specifier int stID; string stName; }; int main() { // Create an instance (object) of StudentDetail StudentDetail stObj; // Accessing members with dot operator stObj.stID = 32; stObj.stName = "Steve Rogers"; // Displaying cout << stObj.stID << endl; cout << stObj.stName; return 0; } |
Output:
32
Steve Rogers
Example: C++ multiple objects with methods
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #include <iostream> using namespace std; //Car class class Car { public: string brand; int year; void carInfo(string br, int y) { brand = br; year = y; } void display() { cout << brand << " " << year << endl; } }; int main() { // Create two objects of Car class Car obj1; Car obj2; obj1.carInfo("BMW", 1999); obj1.display(); obj2.carInfo("Ford", 1969); obj2.display(); return 0; } |
Output:
BMW 1999
Ford 1969
We created two objects (obj1, obj2
) and call the methods present in the Car
class with two separate objects by passing different values.