WRITE A PROGRAM FOR CLASSES AND OBJECTS IN C++

 Que- what is CLASSES and OBJECTS ?

Ans- Classes are the user defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A C++ class is like a blueprint for an object.

a class is a user defined data-type which has data members and member functions.
data members are the data variables and member functions are the functions used to manipulate these variables and together these data members and member functions defines the properties and behavior of the objects in a class. 

  1. An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated means memory is allocated.
  2. class is defined in C++ using keyword class followed by the name of class. The body of class is defined inside the curly brackets and terminated by a semicolon at the end. 

classes-and-objects-in-c++

Declaring Objects: When a class is defined, only the specification for the object is defined; no memory or storage is allocated. To use the data and access functions defined in the class, you need to create objects.

Example of classes and objects in class++

#include<iostream>
using namespace std;

class krishna
{
    int num1, num2 ;
public:
    void getdata ()
    {
        cout<<"enter a number :";
        cin>>num1;
    }
    void display ()
    {
        for (int i=1; i<=10; i++)
        {
            cout<<num1<<"×"<<i<<"="<<num1*i<<endl;
        }
    }
};
int main()
{
    cout << "Table program\n" ;
    krishna ob ;
    ob. getdata ();
    ob. display ();
    return 0;
}

Result:-


Post a Comment

Previous Post Next Post