C++

Classes and Objects

Classes and Objects


Classes and Objects:
Class is a user defined data type just like Structures, but with the difference. It also has 3 sections namely
1.       private,
2.       public and
3.       protected.
Using these, access to member variable of a class can be strictly controlled.
public  : variable and functions are accessible to any function of the program. 
private: The program can access private members of the class only by using public member functions of the class.
This insulation of data members from direct access in a program is called as information hiding.
Class Definition: 

Syntax:
    class tag-name
    {
        public:                                                                          //MUST
                type member_variable_name;
              ——
              ——
                type member_function_name();
              ——
              ——
       private:                                                                         //Optional
               type member_variable_name;
              ——
              ——
                type member_function_name();
              ——
              ——
};
keyword class= defines a class template.
Private and publickeywords = determines the accessibility of the members.
Example to understand the syntax of Classes:
class player
{
  public:                                                                 
              
//MUST
                void getstats(void);
                void showstats(void);
                int no_player;
  private:                                                                              //OPTIONAL
                char name[40];
                int age;
                int run;
                int test;
                float average;
                float calcaverage(void);
};
Example 1:
Program to find area of the circle by defining a class area with “r” as data members and member function to read calculate and display.
#include <iostream>                           //header file
using namespace std;
class Area                                            //class-name
{
private:
    float r,area;                                      //declaration of radius and area.
    void calculate()
    {
      area=3.142*r*r;                             //calculating of area.
      cout<<“area=”<<area;
    }
public:
    void read()
    {
        cout<<“Enter radius”;                    //radius is asked.
        cin>>r;
    }
    void display()
    {
        cout<<“r=”<<r;
        calculate();
    }
};
main()                                                         //main function
{
   Area A;
   A.read();
   A.display();
}
Example 2:
Program to read two complex numbers and addition of two complex numbers and display.
#include <iostream>                                                //header file
using namespace std;
class complex                                                             //class-name
{
private:
    int r,i;                                                                      //declaration of r and i.
public:
    void read()
    {
        cout<<“Enter the complex number”;                    //asking for the complex numbers.
        cin>>r>>i;
    }
    void display()
    {
        cout<<“n”<<r<<“+i”<<i;                                     //displaying of all complex numbers.
    }
    void Add (complex t1, complex t2)                          //addition of 2 complex numbers.
    {
        r=t1.r+t2.r;
        i=t1.i+t2.i;
    }
};
main()                                                                             //main function.
{
   complex c1,c2,c3;
   c1.read();
   c2.read();
   c3.Add(c1,c2);
   c1.display();
   c2.display();
   c3.display();
}




Example 3:
Program to read two complex numbers and addition of two complex numbers and display.
-:Writing function definition outside the class:-

#include <iostream>                                                            //header file.
using namespace std;
class complex                                                                        //class-name
{
private:
    int r,i;                                                                                  //declaration of r and i.
public:
    void read()
    {
        cout<<“Enter the complex number”;                            //asking for the complex numbers.
        cin>>r>>i;
    }
    void display()
    {
        cout<<“n”<<r<<“+i”<<i;                                              //displaying of the complex numbers.
    }
    void Add (complex, complex);
};
void complex::Add(complex t1,complex t2)                         //addition of complex numbers.
{
    r=t1.r+t2.r;
    i=t1.i+t2.i;
}
main()                                                                                     //main function.
{
   complex c1,c2,c3;
   c1.read();
   c2.read();
   c3.Add(c1,c2);
   < /span>c1.display();
   c2.display();
   c3.display();
}




Example 4:
Program to read Roll number, name, total marks and percentage of “N” students and display using class and objects.
#include <iostream>                                                         //header file.
using namespace std;
#define MAX 20                                                               //defining the maximum size of students.
class student                                                                      //class-name.
{
    private:
         char  name[30];
         int   rollNumber;
         int   total;
         float percentage;
    public:
         void getDetails(void);                                                //get function declaration
         void putDetails(void);                                                //put function declaration
};
void student::getDetails(void)                                             //get function
{
     cout << “Enter name: ” ;
    cin >> name;
     cout << “Enter roll number: “;
    cin >> rollNumber;
     cout << “Enter total marks out of 125: “;
    cin >> total;
     percentage=(float)total/125*100;
}
void student::putDetails(void)                                                 //put function
 {
    cout << “Student details:n”;
    cout << “Name:”<< name << “,Roll Number:” << rollNumber << “,Total:” << total << “,Percentage:” << percentage;
}
int main()                                                                                     //main function
{
    student std[MAX];
    int n,i;
    cout << “Enter number of students: “;
    cin >> n;
    for(i=0;i< n; i++)
    {
        cout << “Enter student details” << i+1 << “:n”;
        std[i].getDetails();
    }
    cout << endl;
   
    for(i=0;i< n; i++)
    {
        cout << “student detail” << (i+1) << “:n”;
        std[i].putDetails();
    }
    return 0;
}