C++ class Keyword
The class
keyword in C++ is used to define user-defined data types called classes. A class is a blueprint for creating objects, encapsulating data members (attributes) and member functions (methods) that operate on the data. It is a fundamental concept in object-oriented programming (OOP) that enables modular and reusable code.
Classes support features like encapsulation, inheritance, and polymorphism, which are core principles of OOP.
In this tutorial, we will explain how to use the class
keyword in C++ with syntax, examples, and detailed explanations.
Syntax
</>
Copy
class ClassName {
public:
// Public members
data_type attribute_name;
return_type method_name(parameter_list);
private:
// Private members
data_type attribute_name;
return_type method_name(parameter_list);
};
- class
- The keyword used to define a class.
- ClassName
- The name of the class. It should follow standard naming conventions.
- public
- Specifies that the members are accessible from outside the class.
- private
- Specifies that the members are accessible only within the class. This is the default access specifier.
Examples
Example 1: Basic Class with Public Members
This example demonstrates a simple class with public data members and a method.
</>
Copy
#include <iostream>
using namespace std;
class Rectangle {
public:
int width;
int height;
int area() {
return width * height;
}
};
int main() {
Rectangle rect;
rect.width = 5;
rect.height = 10;
cout << "Area: " << rect.area() << endl;
return 0;
}
Output:
Area: 50
Explanation:
- The
Rectangle
class has two public attributes,width
andheight
, and a public method,area()
. - In the
main()
function, an objectrect
of typeRectangle
is created. - The attributes
width
andheight
are assigned values, and thearea()
method is called to calculate the area. - The calculated area,
50
, is printed to the console.
Example 2: Class with Private Members
This example demonstrates how to use private members in a class and access them through public methods.
</>
Copy
#include <iostream>
using namespace std;
class Circle {
private:
double radius;
public:
void setRadius(double r) {
radius = r;
}
double getArea() {
return 3.14 * radius * radius;
}
};
int main() {
Circle c;
c.setRadius(5.0);
cout << "Area: " << c.getArea() << endl;
return 0;
}
Output:
Area: 78.5
Explanation:
- The
Circle
class has a private attribute,radius
, which cannot be accessed directly from outside the class. - Public methods
setRadius()
andgetArea()
are provided to set the radius and calculate the area, respectively. - In the
main()
function, thesetRadius()
method is used to set the radius of the circle. - The
getArea()
method calculates the area, which is printed to the console as78.5
.
Key Points about class
Keyword
- The
class
keyword is used to define a class in C++. - A class encapsulates data members and member functions into a single entity.
- Access specifiers (
public
,private
,protected
) control the accessibility of members. - Classes support OOP features such as inheritance, polymorphism, and abstraction.
- Objects are instances of a class that operate on the class’s attributes and methods.