Class

Class

Class

In C++, a class is a user-defined data type that encapsulates data and functions together into a single unit. Classes allow you to define your own data types with their own properties and behaviors.

Here’s an example of a class definition:

				
					#include<iostream>

class  Rectangle {

  private:

    int width, height;

  public:

    void set_values(int, int);

    int area();

};

void Rectangle::set_values(int w, int h) {

    width = w;

    height = h;

}

int Rectangle::area() {

    return width * height;

}

int main() {

    Rectangle rect; 

   rect.set_values(5, 7);

    std::cout << "The area of the rectangle is " << rect.area() << std::endl;

    return 0;

}

​
				
			

In this example, we define a class called Rectangle with two private member variables width and height. We also define two member functions: set_values and area.

The set_values function is a public member function that takes two integer arguments w and h and sets the values of the width and height member variables.

The area function is also a public member function that calculates and returns the area of the rectangle, which is the product of the width and height.

In the main function, we create an instance of the Rectangle class called rect and set its values using the set_values function. We then call the area function to calculate the area of the rectangle and print it to the console.

Classes can be very useful for organizing code and encapsulating data and functions together into a single unit. They can also be used for inheritance and polymorphism, which allows you to create new classes based on existing ones and use them interchangeably.

In C++, an object is an instance of a class. A class is a user-defined data type that encapsulates data and functions into a single entity. When you create an object of a class, it is allocated in memory, and it has its own set of data members and member functions.

For example, consider the following class definition:

				
					class Person {
public:
  std::string name;
  int age;

  void introduce() {
    std::cout << "Hi, my name is " << name << " and I am " << age << " years old." << std::endl;
  }
};Top of Form

				
			

This defines a class called Person with two data members (name and age) and one member function (introduce). To create an object of this class, you simply use the class name followed by parentheses:

				
					Person john; 

				
			

This creates an object of type Person called john. You can then access its data members and member functions using the dot (.) operator:

				
					john.name= "John";

john.age= 30;

john.introduce();
// prints "Hi, my name is John and I am 30 years old."
​
				
			

In C++, objects can also be created dynamically using the new operator, which returns a pointer to the object:

				
					Person* jane = new Person();

jane->name= "Jane";

jane->age= 25;

jane->introduce();
// prints "Hi, my name is Jane and I am 25 years old."
​
				
			

It is important to remember to delete dynamically allocated objects using the delete operator when they are no longer needed:

				
					delete jane;

				
			
Join To Get Our Newsletter
Spread the love