Classes

Estimated reading: 5 minutes 821 Views

What is a Class?

In C++, a class is a user-defined data type that encapsulates data and the functions that operate on that data. It serves as a blueprint for creating objects, which are instances of the class. Imagine a class as a template that defines the structure and behavior of objects.

To declare a class in C++, you use the class keyword.

Once a class is defined, you can create objects of that class. Objects are instances of the class, each with its own set of data.

In C++, classes support access modifiers like public, private, and protected. These modifiers control the visibility and accessibility of class members.

Example 1:

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

class car{
    private: 
        char name[15];
        char color[10];
        int maxSpeed;
        int model;
    public:
        void setName(char n[])
        {
            strcpy_s(name, n);
        }
        void setColor(char n[])
        {
            strcpy_s(color, n);
        }
        void setMaxSpeed(int m)
        {
            maxSpeed = m;
        }
        void setModel(int m)
        {
            model = m;
        }
        char* getName()
        {
            return name;
        }
        char* getColor()
        {
            return color;
        }
        int getMaxSpeed()
        {
            return maxSpeed;
        }
        int getModel()
        {
            return model;
        }
        void print()
        {
            cout << "name = " << name << "\n"
            << "color = " << color << "\n"
            << "maxspeed = " << maxSpeed << "\n"
            << "model = " << model << "\n";
        }
};
int main()
{
    car x;
    x.setName("kia");
    x.setColor("red");
    x.setMaxSpeed(300);
    x.setModel(2017);
    x.print();
    return 0;
}

This C++ code defines a simple class named car that represents information about a car, such as its name, color, maximum speed, and model. The program creates an instance of this class, sets its attributes, and prints the details using member functions. Let’s break down the code step by step:

  • Class Definition: car
class car {
private: 
    char name[15];
    char color[10];
    int maxSpeed;
    int model;
public:
    // Member functions for setting attributes
    void setName(char n[]) { strcpy_s(name, n); }
    void setColor(char n[]) { strcpy_s(color, n); }
    void setMaxSpeed(int m) { maxSpeed = m; }
    void setModel(int m) { model = m; }

    // Member functions for retrieving attributes
    char* getName() { return name; }
    char* getColor() { return color; }
    int getMaxSpeed() { return maxSpeed; }
    int getModel() { return model; }

    // Member function to print details
    void print() {
        cout << "name = " << name << "\n"
             << "color = " << color << "\n"
             << "maxspeed = " << maxSpeed << "\n"
             << "model = " << model << "\n";
    }
};

– Private Members: The class has private member variables (name, color, maxSpeed, and model), which can only be accessed within the class.

– Public Members: The class provides public member functions (setName, setColor, setMaxSpeed, etc.) to set and retrieve the values of these private variables.

  • main Function:
int main() {
    car x;

    // Setting attributes of the car
    x.setName("kia");
    x.setColor("red");
    x.setMaxSpeed(300);
    x.setModel(2017);

    // Printing the details using the print() member function
    x.print();

    return 0;
}

– Object Creation: An object x of type car is created.

– Setting Attributes: The attributes of the car (name, color, maxSpeed, and model) are set using the member functions (setName, setColor, setMaxSpeed, and setModel).

– Printing Details: The details of the car are printed using the print member function, which displays the values of the car’s attributes.

The output of this program would be:

name = kia
color = red
maxspeed = 300
model = 2017

This demonstrates a simple usage of a class in C++, encapsulating data and behavior related to a car. The class provides methods to set and retrieve attributes, promoting encapsulation and data hiding. This is a fundamental concept in object-oriented programming.

Example 2:

#include <iostream>
using namespace std;

class triangle{
    private: 
        float base;
        float height;
    public:
        void setBase_height(float b, float h)
        {
            base = b;
            height = h;
        }
        float area()
        {
            return 0.5 * base * height;
        }
        void print()
        {
            cout << "base = " << base << endl
            << "height = " << height << endl
            << "area = " << area() << endl;
        }
};
int main()
{
    triangle ob;
    ob.setBase_height(5, 10);
    ob.print();
    return 0;
}

This code defines a class named triangle that represents a triangle. The class has private member variables for the base and height of the triangle, and it provides member functions for setting these values, calculating the area, and printing the details of the triangle. Let’s break down the code step by step:

  • Class Definition: triangle
class triangle {
private: 
    float base;
    float height;

public:
    // Member function to set base and height
    void setBase_height(float b, float h) {
        base = b;
        height = h;
    }

    // Member function to calculate the area of the triangle
    float area() {
        return 0.5 * base * height;
    }

    // Member function to print details of the triangle
    void print() {
        cout << "base = " << base << endl
             << "height = " << height << endl
             << "area = " << area() << endl;
    }
};

– Private Members: The class has private member variables base and height, which can only be accessed within the class.

– Public Members: The class provides public member functions (setBase_height, area, and print) to set the base and height, calculate the area, and print the details of the triangle.

  • main Function:
int main() {
    triangle ob;

    // Setting base and height of the triangle
    ob.setBase_height(5, 10);

    // Printing the details of the triangle
    ob.print();

    return 0;
}

– Object Creation: An object ob of type triangle is created.

– Setting Base and Height: The setBase_height member function is called to set the base to 5 and height to 10 for the triangle.

– Printing Details: The print member function is called to print the details of the triangle, including the base, height, and calculated area.

The output of this program would be:

base = 5
height = 10
area = 25

This demonstrates a basic usage of a class in C++ to represent and manipulate data related to a triangle. The class encapsulates the data and behavior related to the triangle, promoting encapsulation and making the code more organized and reusable. The program calculates and displays the area of the triangle using the provided member functions.

Share this

Classes

Or copy link

CONTENTS
English