Table of contents | |
Introduction | |
Creating a Class | |
Creating Objects | |
Accessing Class Members | |
Member Functions | |
Constructors | |
Sample Problems |
In C++, classes and objects are fundamental concepts that form the basis of object-oriented programming. A class is a blueprint or template for creating objects, while an object is an instance of a class. Classes allow us to define our own data types and encapsulate data and methods into a single entity.
To create a class in C++, we use the 'class' keyword followed by the class name. Let's consider an example where we create a class called Person that represents a person's attributes such as name and age:
class Person {
public:
std::string name;
int age;
};
In the above code, we declare two member variables: 'name' of type 'std::string' and 'age' of type 'int'. The 'public': keyword denotes that these variables can be accessed from outside the class.
Once we have defined a class, we can create objects of that class. Objects are instances of the class and hold the actual data. To create an object, we use the class name followed by the object name and optional parentheses:
Person person1;
In the above code, we create an object named 'person1' of type 'Person'.
We can access the member variables and member functions of a class using the dot ('.') operator. Let's see an example:
person1.name = "John";
person1.age = 25;
In the above code, we access the 'name' and 'age' member variables of the 'person1' object and assign values to them.
Classes can also have member functions, which are functions defined inside the class. These functions can operate on the member variables of the class. Let's add a member function called 'displayDetails' to our 'Person' class:
class Person {
public:
std::string name;
int age;
void displayDetails() {
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
}
};
The 'displayDetails' function prints the name and age of the person. We can call this function on an object of the 'Person' class as follows:
person1.displayDetails();
The output will be:
Name: John
Age: 25
class Person {
public:
std::string name;
int age;
Person(const std::string& n, int a) {
name = n;
age = a;
}
void displayDetails() {
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
}
};
In the above code, we define a constructor that takes the 'name' and 'age' as parameters and initializes the member variables. We can now create objects of the 'Person' class using this constructor:
Person person1("John", 25);
person1.displayDetails();
The output will be the same as before:
Name: John
Age: 25
Problems 1: Create a class called 'Rectangle' with member variables 'width' and 'height'. Add a member function 'calculateArea' that calculates and returns the area of the rectangle.
class Rectangle {
public:
int width;
int height;
int calculateArea() {
return width * height;
}
};
Problems 2: Create a class called 'Circle' with member variable 'radius'. Add a constructor that initializes the radius. Add a member function 'calculateArea' that calculates and returns the area of the circle.
class Circle {
public:
double radius;
Circle(double r) {
radius = r;
}
double calculateArea() {
return 3.14159 * radius * radius;
}
};
In this article, we explored the concepts of classes and objects in C++. We learned how to create a class, create objects of that class, access class members, define member functions, and use constructors. Classes and objects provide a powerful way to organize and manipulate data in object-oriented programming. By understanding these concepts, you can build complex data structures and algorithms in the field of data structures and algorithms in C++.
153 videos|115 docs|24 tests
|
|
Explore Courses for Software Development exam
|