C++ Classes and Object

Advertisements

Classes in C++

Class is an encapsulation of data and coding. Classes are an expanded version of structures. Structure can only contain multiple variables. Classes can contain multiple variables, even more, classes can also contain functions as class member. Variables declared in class are called Data Members(Attributes). Functions declared or defined in class are called Member Functions.

Class Declaration

Declaration of class must start with the keyword class followed by the class name and class members are declared within braces.

Syntax of declaring class


              class class-name
              {
                     datatype var1;
                     datatype var2;
                     - - - - - - - - - -
                     datatype varN;

                     method1();
                     method2();
                     - - - - - - - - - -
                     methodN();
              };
			

Example of declaring class


              class Employee
              {
                     int Id;
                     char Name[25];
                     int Age;
                     long Salary;

                     public:
                     void GetData();
                     void PutData();
              };

Instantiation of objects OR Accessing the class members

Instantiation of object means, create an object of class to access its members. Object is a variable of class type. Class members are accessed using the dot operator(.) between class's object and class's member name.

Syntax for creating an object of class


              class-name obj;
					

Example for creating an object of class


              Employee Emp;
					

Defining Class Member Functions

We can define member function inside or outside class.

Syntax of defining member function outside class


              return_type class_name :: function_name(argument list)
              {
                           - - - - - - - - - -
                           body of function
                           - - - - - - - - - -
              }

return_type Type of value function will return.

class_name:: A program may contain more than one class and these classes may have similar member functions. Class_name:: tells the compiler which class the function belongs to and the scope of the member function is restricted to the class_name.

function_name Can be any valid C++ identifier.

argument list Represents the type and number of value function will take, values are sent by the calling statement.

Example of defining member function outside class


       #include<iostream.h>
       #include<conio.h>

       class Employee
       {
              int Id;
              char Name[25];
              int Age;
              long Salary;

              public:
              void GetData();
              void PutData();

       };

       void Employee :: GetData()           //Statement 1 : Defining GetData()
       {
              cout<<"\n\tEnter Employee Id : ";
              cin>>Id;

              cout<<"\n\tEnter Employee Name : ";
              cin>>Name;

              cout<<"\n\tEnter Employee Age : ";
              cin>>Age;

              cout<<"\n\tEnter Employee Salary : ";
              cin>>Salary;
       }

       void Employee :: PutData()           //Statement 2 : Defining PutData()
       {
              cout<<"\n\nEmployee Id : "<<Id;
              cout<<"\nEmployee Name : "<<Name;
              cout<<"\nEmployee Age : "<<Age;
              cout<<"\nEmployee Salary : "<<Salary;
       }

       void main()
       {

              Employee E;           //Statement 3 : Creating Object

              E.GetData();          //Statement 4 : Calling GetData()
              E.PutData();          //Statement 5 : Calling PutData()

       }

   Output :

              Enter Employee Id : 1
              Enter Employee Name : Kumar
              Enter Employee Age : 29
              Enter Employee Salary : 45000

              Employee Id : 1
              Employee Name : Kumar
              Employee Age : 29
              Employee Salary : 45000

In the above program, we have declared two member functions GetData() and PutData() within class and statement 1 and 2 is defining these methods outside class. Statement 3 is creating an object E of Employee class. Statement 4 and 5 is invoking/calling member function through the object E of Employee class.

Syntax of defining member function inside class


              return_type function_name(argument list)
              {
                           - - - - - - - - - -
                           body of function
                           - - - - - - - - - -
              }

return_type Type of value function will return.

function_name Can be any valid C++ identifier.

argument list Represents the type and number of value function will take, values are sent by the calling statement.

Definition of member function inside class is similar to defining normal function. There is no need to tell compiler about the class the function belongs to because the definition of member function is already in the class.

Example of defining member function inside class


       #include<iostream.h>
       #include<conio.h>

       class Employee
       {
              int Id;
              char Name[25];
              int Age;
              long Salary;

              public:
              void GetData()           //Statement 1 : Defining GetData()
              {
                    cout<<"\n\tEnter Employee Id : ";
                    cin>>Id;

                    cout<<"\n\tEnter Employee Name : ";
                    cin>>Name;

                    cout<<"\n\tEnter Employee Age : ";
                    cin>>Age;

                    cout<<"\n\tEnter Employee Salary : ";
                    cin>>Salary;
              }

              void PutData()           //Statement 2 : Defining PutData()
              {
                    cout<<"\n\nEmployee Id : "<<Id;
                    cout<<"\nEmployee Name : "<<Name;
                    cout<<"\nEmployee Age : "<<Age;
                    cout<<"\nEmployee Salary : "<<Salary;
              }

       };


       void main()
       {

              Employee E;           //Statement 3 : Creating Object

              E.GetData();          //Statement 4 : Calling GetData()
              E.PutData();          //Statement 5 : Calling PutData()

       }

   Output :

              Enter Employee Id : 1
              Enter Employee Name : Kumar
              Enter Employee Age : 29
              Enter Employee Salary : 45000

              Employee Id : 1
              Employee Name : Kumar
              Employee Age : 29
              Employee Salary : 45000

In the above program, we have defined two member functions GetData() and PutData() within class. Statement 3 is creating an object E of Employee class. Statement 4 and 5 is invoking/calling member function through the object E of Employee class.

Advertisement