C++ - Classes and Objects
C++ is an object-oriented programming language and it allows us to create objects. To create an object, first of all, the object should be defined in the program which is done by creating a class. A class is a code template that is used to define a new object type. Within a class, the object's variables and functions are defined and when an object is created, its variables and functions are determined by the class in which it is created.
Create Class
To create a class in C++, class or struct keyword is used. It starts with the keyword followed by class name and a pair of curly braces { }. All its variables and functions goes inside the braces:
Syntax
//defining a class class className { access_specifier: members; //variables and functions };
access_specifier: It defines the access type of the class members (variables and functions). There are three types of access specifier in C++.
- public: members of the class are accessible from everywhere.
- protected: members of the class are accessible within the class and by friend and inherited classes.
- private: members of the class are accessible within the class and by friend classes.
Create Object
To create an object, specify the class name followed by object name. To access the member of the class, dot . operator is used. See the syntax below:
Syntax
//creating an object className objectName; //access class member (variables and functions) objectName.variable objectName.function(parameters)
Example:
In the example below, a class (new object type) called Circle is created. An object called MyCircle of the class Circle is created in the main() function. The object has only one integer public member radius which is accessed in the main function using dot . operator.
#include <iostream> using namespace std; class Circle { public: int radius = 10; }; int main () { Circle MyCircle; cout<<MyCircle.radius; return 0; }
The output of the above code will be:
10
Class Functions
Class function also known as class method can be defined in two ways.
- Inside class definition
- Outside class definition
Example: Class function defined inside class
In the example below, a class function called area is defined inside the class Circle. A class function is accessed in the same way as other class members, i.e, using dot . operator. Here, the area() member function returns the area of the circle object.
#include <iostream> using namespace std; class Circle{ public: int radius = 10; //class function defined inside class void area() { float x; x = 22/7.0*radius*radius; cout<<x; } }; int main () { Circle MyCircle; MyCircle.area(); return 0; }
The output of the above code will be:
314.286
Example: Class function defined outside class
To define a class function outside the class, scope operator :: is used. It starts with class name followed by scope operator and function name. Along with this, the function must be declared inside the class as shown in the example below. Here, the area() member function returns the area of the circle object.
#include <iostream> using namespace std; class Circle { public: int radius = 10; void area(); }; //class function defined outside class void Circle::area() { float x; x = 22/7.0*radius*radius; cout<<x; } int main () { Circle MyCircle; MyCircle.area(); return 0; }
The output of the above code will be:
314.286