Header Ads

CHAPTER-1.2: Class and object

CHAPTER-1.2: Class and object
Program:
#include<iostream.h>
//accessing private data using member function
 class myclass
{
 int a;
public:
void set_a(int num);
int get_a();
};

  void myclass::set_a(int num)
{
a=num;
}
 int myclass::get_a()
{
return a;
}
int main()
{
myclass ob1, ob2;
ob1.set_a(10);
ob2.set_a(99);
 cout<<ob1.get_a()<<"\n";
cout<<ob2.get_a()<<"\n";
return 0;
}
 output: 10
               99
   
#include<iostream.h>
//accessing public data using objects

class myclass
{
public:
int a;

};

  int main()
{
myclass ob1, ob2;
ob1.a=10;
ob2.a=99;
 cout<<ob1.a<<"\n";
cout<<ob2.a<<"\n";
return 0;
}
 output: 10
99




Program:  
  // test_circle.cpp
#include <iostream.h>

class Circle               
{
  public:
    Circle(double X, double Y, double R);  // a constructor
    void Show();                           // a member function
    void Set(double R);                    // change the radius
 
 void Move(double X, double Y);         // move the circle
  private:
    double Xcenter;
    double Ycenter;
    double radius;
};
  Circle::Circle(double X, double Y, double R)
{
  Xcenter = X;
  Ycenter = Y;
  radius  = R;
}
 
void Circle::Show()
{
  cout << "X, Y, R " << Xcenter << "  " << Ycenter << "  "
       << radius << endl;
}
void Circle::Set(double R)
{
  radius = R;
}
 
void Circle::Move(double X, double Y)
{
  Xcenter = X;
  Ycenter = Y;
}
int main()
{
  Circle c1(1.0, 2.0, 0.5);  // construct an object named 'c1' of type 'Circle'
  Circle circle2(2.5, 3.0, 0.1);  // another object named 'circle2'
   c1.Show();       // tell the object c1 to execute the member function Show
  circle2.Show();  // circle2 runs its member function Show
   c1.Move(1.1, 2.1);  // move center
  c1.Show();
   circle2.Set(0.2);   // set a new radius
  circle2.Show();
  return 0;
}

Output:
X, Y, R 1  2  0.5
X, Y, R 2.5  3  0.1
X, Y, R 1.1  2.1  0.5
X, Y, R 2.5  3  0.2  

  Private Section
All data at the beginning of a class defaults to private and the private keyword is optional.  This means, the data at the beginning of the class cannot be accessed from outside of the class; it is hidden from any outside access.
Therefore, the variable named keep_data which is part of the object named John_cat defined in line 37 and 38, is not available for use anywhere in the main() program. 
That is why we have to comment out the following codes:
  // John_cat.keep_data = 100;
  // Joe_cat.keep_data = 110;
It is as if we have built a wall around the variables to protect them from accidental corruption by outside programming influences.

 Public Section
A new keyword public, introduced in line 10 which states that anything following this keyword can be accessed from outside of this class as shown below:
 public:   // public part
Because the two functions are declared following the keyword public, they are both public and available for use by any calling program that is within the scope of this object.
This essentially opens two small peepholes in the solid wall of protection that we built around the class and the private keep_data variable is not available to the calling program.
Thus, we can only use the variable by calling one of the two functions defined within the public part of the class.  These are called member functions because they are members of the class.
Since we have two functions, we need to define them by saying what each function will actually do.  This is done in lines 17 through 24 where they are each define in the normal way, except that the class name is added with the function name and separated from it by a double colon ( :: ), called scope operator as shown below:
void item::set(int enter_value)
{
                keep_data = enter_value;
}
 int item::get_value(void)
{
                return keep_data;
}

You can do anything with the private data within the function implementations which are a part of that class; also the private data of other classes is hidden and not available within the member functions of this class.
This is the reason we must mention the class name to the function names of this class when defining them
  It is legal to declare variables and functions in the private part, and additional variables and functions in the public part also.
In most practical situations, variables only declared in the private section and functions only declared in the public part of a class definition.  Occasionally, variables or functions are declared in the other part. 

   #include <iostream.h>
// the class declaration part
       class item
      {
         int  keep_data;    // private by default, it is public in struct
       public:                 // public part
       void  set(int enter_value);
       int  get_value(void);
   }; 
 // class implementation part
      void  item::set(int enter_value)
   {
       keep_data = enter_value;
   }
 int  item::get_value(void)
   {
       return  keep_data;
    }
 // main program
  void main()
    {
          item    John_catJoe_catBig_cat
          // three objects instantiated
          int   garfield;    // a normal variable
         John_cat.set(10);   // assigning values 

Joe_cat.set(11);
          Big_cat.set(12);
          garfield = 13;
          cout<<"Accessing data using class\n";
          cout<<"-------------------------\n";
          cout<<"Data value for John_cat is "<<John_cat.get_value()<<"\n";
          cout<<"Data value for Joe_cat is "<<Joe_cat.get_value()<<"\n";
cout<<"Data value for Big_cat is "<<Big_cat.get_value()<<"\n";
          cout<<"\nAccessing data normally\n";
          cout<<"---------------------------\n";
          cout<<"Data value for garfield is "<<garfield<<"\n"; 
}

Output:
Accessing data using class
-------------------------
Data value for John_cat is 10
Data value for Joe_cat is  11
Data value for Big_cat is 12
Accessing data normally
---------------------------
Data value for garfield is 13

 // program classobj.cpp - using class instead of struct
     #include <iostream.h>
   // a simple class declaration part
     class  rectangle
     {
        // private by default, member variables
        int height;
      int width;
   public:
     // public, with two methods
      int area(void);
      void initialize(int, int);
   };
 // class implementation part
  int rectangle::area(void)
  {
       return (height * width);
  }
void  rectangle::initialize(int initial_height, int initial_width)
  {
        height = initial_height;
        width = initial_width;
  }
  
  // a normal structure - compare it with class
  struct  pole
  {
      int  length;  // public
       int  depth;   // public
    };
// main program
  void main ( )
  {
      rectangle wall, square;
      pole             lamp_pole;
      // wall.height = 12;
      // wall.width  = 10;
      // square.height  =  square.width  =  8;
      // these 3 lines invalid now, private, access only through methods
      wall.initialize(12,10);    // access data through method
      square.initialize(8,8);
 lamp_pole.length  =  50;  // a normal struct data access
      lamp_pole.depth  =  6;
      cout<<"Using class instead of struct\n";
      cout<<"access through method area()\n";
      cout<<"------------------------------\n";
      cout<<"Area of the wall-->wall.area() = "<<wall.area()<< "\n\n";
      cout<<"Area of the square-->square.area()= "<<square.area()<<"\n\n";
      cout<<lamp_pole.length<<"\n";
      cout<<lamp_pole.depth;
        }
Output:

Using class instead of struct
access through method area()
------------------------------
Area of the wall-->wall.area() =120
 Area of the square-->square.area()= 64
50
6
  // program obstring.cpp (string within an object)
           #include <iostream.h>
             // class declaration part
            class wall
            {
                int    length;
                int    width;
              char   *line_of_text;    // a pointer variable
 public:
                 wall(char *input_line);  // a constructor declaration
                 void  set(int new_length, int new_width);
                 int  get_area(void);
         };
          // class implementation part
wall::wall(char *input_line)    // a constructor implementation
         {
             length = 8;
             width = 8;
             line_of_text = input_line;
         }
 // this method will set a wall size to the two input parameters
         void wall::set(int new_length, int new_width)
         {
             length = new_length;
             width = new_width;
         }
        // this method will calculate and return
         // the area of a wall instance
 int wall::get_area(void)
         {
             cout<<line_of_text<< "= ";
             return (length * width);
         }
        // the main program
 void main()
         {
             // objects are instantiated with a string
             // constant as an actual parameters
             wall  small("of small size "),
             medium("of medium size "),
             large("of large size ");
           small.set(5, 7);
             large.set(15, 20);
            cout<<"    Embedded string used as an object\n";
             cout<<"    ----------------------------------\n\n";
             cout<<"Area of wall surface ";
cout<<small.get_area()<<"\n";
             cout<<"Area of wall surface ";
             cout<<medium.get_area()<<"\n";
             // use default value of constructor
             cout<<"Area of wall surface ";
             cout<<large.get_area()<<"\n";
              // system("pause");
         }
 Output:
Embedded string used as an object
----------------------------------
Area of wall surface of small size=35
Area of wall surface of medium size=64
Area of wall surface of large size = 300
 

No comments

Theme images by Blogger. Powered by Blogger.