Class and Object (OOP concept)

Hii from Team S.O.A.P

   Today we are going to start with "class and object" OOP concept of C++..
lets head to the definition of Class and instance/object.since C++ is an Object oriented programming language.programmer

class

The building block of C++ that leads to Object Oriented programming is a Class. It is a 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 class is like a blueprint for an object.
In simple words

Object/instance

An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated.Object is a real world entity, for example, chair, car, pen, mobile, laptop etc.

In other words, object is an entity that has state and behavior. Here, state means data and behavior means functionality.Object is a runtime entity, it is created at runtime.Object is an instance of a class. All the members of the class can be accessed through object.

how to defining class and object/instance 

class class_name
{

 Access specifier; //by default it is set to be private

 Variables/data members; //class variables section

Members Functions();//methods to work with class data member
};
class_name object_name; //creation of object/instance

Implementation of class & object(Data members)

#include "iostream"

   using namespace std;

class test1

{

 public:

 string name; //class member and also instance

 int age; //class member and also instance

 string work; //class member and also instance



};


int main()

{

 test1 t1; //object creation

t1.name = "Imtiyaz";

t1.age = 124 ;//just kidding

t1.work = "S.O.A.P";

cout<<"user name: "<<t1.name<<endl;

cout<<"Age: "<<t1.age<<"yrs"<<endl;

cout<<"working for "<<t1.work<<endl;

return 0;
}

 Here is the output for above program

Implementation of class & obj (Data members & Functions)

  #include "iostream"

   using namespace std;

class test1

{

 public:

 string name; //class member and also instance

 int age; //class member and also instance

 string work; //class member and also instance

    void Get_input(string n,int a,string w)
    {
        name = n;
        age = a;
        work = w;
    }
   
    void print()
    {
        cout<<"User name: "<<name<<endl;
        cout<<"User age: "<<age<<"yrs"<<endl;
        cout<<"Working for "<<work<<endl;
   
    }

};


int main()
{

 test1 t1; //object creation
 test1 t2; //second object

     //calling method with the first instance of class
cout<<"First user data"<<endl;
     t1.Get_input("Imtiyaz",20,"S.O.A.P");
     t1.print();

    //calling method with the second instance of class
cout<<"Second user data"<<endl;
    t2.Get_input("Baka",20,"S.O.A.P");
    t2.print();
return 0;
}

Output for above program


Thanks for Reading this for any Query please message us or comment
Have great day




Comments