Classes and Objects in Object Oriented Programming.

Classes:

In Object-Oriented Programming, a class is a blueprint or a template for creating objects. It is a user defined data types that holds its own data members(attributes) and member functions(methods) that can be accessed and used by creating an instance of that class.

For example, if we map a class with real-world entity, let’s consider human as a class or blueprint and it contains some attributes like human has name, age and height etc. In OOP terminologies, all these attributes are data members. On the other hand, human can speak, walk, eat and sleep etc are actions that he can perform. In OOP terminologies, all these actions are methods or member functions.

  • Data members/attributes: In OOP attributes are like characteristics or properties of a class. For example, in a “Human” class, name, age and height are data members because they describe physical traits of a person.
  • Member functions/methods: In OOP methods are actions or behaviors the class can perform. For example, in a “Human” class, walk( ), sleep( ), and eat( ) are member functions because they represent actions a human can do.

How to Create a Class?

In C++, a class is defined by a reserved keyword class followed by the name of class. Than started the body of a class using curly brackets {...} that contains the attributes and functions of an identity. And a class is ended with a semicolon ; .

Syntax:

class className{
//class Attributes
//class Functions
};

For Example:

class Human {
public:
    // Attributes
    string name;
    int age;
    double height;
    
    // Functions
    void eat() {
        cout << "Human can eat." << endl;
    }
    void sleep() {
        cout << "Human can sleep." << endl;
    }
    void walk() {
        cout << "Human can walk." << endl;
    }
};

Example Explaination:

  • The class keyword is used to create a class Human.
  • name, age and height are the attributes of human class while string, int and height are the data types of the attributes.
  • The public keyword is an access specifier that we will discuss later.
  • Than void eat(), void sleep() and void walk() are the functions of a Human class.

What is an Object in OOP?

In Object-Oriented Programming, an object is an instance of a class. An object is a physical entity. A class can be used to create many objects like in an organization, for proper check and balance there’s a class Human and from this class we can create different objects that can use the same attributes and functions.

How to Define an Object?

In C++, we can define an object like this:

ClassName object_name;

For Example:

int main() {
    // Create an object of Human
    Human person1;
    
    // Accessing the attributes
    person.name = "John";
    person.age = 30;
    person.height = 5.9;

    // Print the attributes of the person
    cout << "Name: " << person.name << endl;
    cout << "Age: " << person.age << endl;
    cout << "Height: " << person.height << " feet" << endl;

    // Calling the functions
    person.eat();
    person.sleep();
    person.walk();
    
    return 0;
}

Example Explaination:

  • person1 is an object of class Human.
  • person.name = "John"; statement is used to accessing the attribute of Human class and directly assigning them a value for a person1 object.
  • cout << "Name: " << person.name << endl; statement is used to print the name attribute of person1.

Conclusion:

  • Class is a blueprint that define some attributes and functions while, objects is an instance of that class that has these attributes and functions.
  • When a class is defined no memory is allocated for a class but when an object is created, the required memory from head is allocated to the object.
  • Class is a logical entity on the other hand object is an physical entity or sometimes it may be logical.
  • A class is created only once whlie we can create multiple objects for a single class.
  • In C++, for a class a reserved keyword Class is used to create a class and an object is declared as className objectName.

Similar Posts