Chapter 1 Examples

  1. As a simple example, this program uses myclass to set the value of a for ob1 and ob2 and to display a's value for each object.

    #include<iostream.h>
    class myclass{
      //private to 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;
    }
    main()
    {
      myclass ob1, ob2;
      ob1.set_a(10);
      ob2.set_a(99);
    
      cout<<ob1.get_a()<<"\n";
      cout<<ob2.get_a()<<"\n";
      return 0;
    }
    

    As you should expect this program displays the values 10 and 99 on the screen.

  2. In myclass from the preceding example, a is private. This means that only member functions of myclass can access it directly. (This is one reason why the public function get_a() is required.) If you try to access a private member of a class from some part of your program that is not a member of that class, a compile time error will result. For example, assuming that myclass is defined as shown in the preceding example, the following main() function will cause an error:

    //This fragment contains an error.
    #include <iostream.h>
     
    main()
    {
      myclass ob1,ob2;
      ob1.a=10;//ERROR! Cannot access private member
      ob2.a=99;//by non-member functions.
    
      cout << ob1.get_a()<<"\n";
      cout << ob2.get_a()<<"\n";
      
      return 0;
    }
    
  3. Just as there can be public member functions, there can be public member variables as well. For example, if a were declared in the public section of myclass then a could be referenced by any part of the program, as shown here:

    #include <iostream.h>
    
    class myclass{
    public:
      //now a is public
      int a;
      // and there is no need for set_a() or get_a()
    };
    
    main()
    {
      myclass ob1, ob2;
    
      //here, a is accessed directly
      ob1.a=10;
      ob2.a=99;
    
      cout<<ob1.a << "\n";
      cout<<ob2.a << "\n";
    return 0;
    }
    

    In this example, since a is declared as a public member of myclass, it is directly accesible from main. Notice How the dot operator is used to access a. In general, whether you are calling a member function or accessing a member variable, the objects name followed by the dot operator followed by the member's name is required to fully specify which object's member you are referring to.

  4. To get a taste of the power of objects, let's look at a more practical example. This program creates a class called stack that implements a stack that can be used to store characters:

    #include <iostream.h>
    #define SIZE 10
    // Declare a stack class for characters
    class stack{
      char stck[SIZE];//holds the stack
      int tos; //index of top-of-stack
      
    public:
      void init(); //initialize stack
      void push(char ch); //push character on stack
      char pop(); //pop character from stack
    };
    
    // Initialize the stack
    void stack::init()
    {
      tos=0;
    }
    
    //Push a character
    void stack::push(char ch)
    {
      if(tos==SIZE){
        cout <<"Stack is full";
        return;
      }
      stck[tos]=ch;
      tos++;
    }
    
    //Pop a character.
    
    char stack::pop()
    {
      if(tos==0){
        cout <<"Stack is empty";
        return 0; //return null on empty stack
      }
      tos--;
      return stck[tos];
    }
    main()
    {
      stack s1, s2; // Create two stacks.
      int i;
      // Initialize the stacks.
      
      s1.init();
      s2.init();
    
      s1.push('a');
      s2.push('x');
      s1.push('b');
      s2.push('y');
      s1.push('c');
      s2.push('z');
    
      for(i=0;i<3;i++) cout << "Pop s1:" << s1.pop() << "\n";
      for(i=0;i<3;i++) cout << "Pop s2:" << s2.pop() << "\n";
       
      return 0;
    }
    

    This Program displays the following output:
    Pop s1: c
    Pop s1: b
    Pop s1: a
    Pop s2: z
    Pop s2: y
    Pop s2: x

    
    Chapter 1 More Examples
    

    1. This program shows inline code encapsulation

      #include <iostream.h>
      #include <string.h>
       
      class employee{
      public:
        char name[64];
        long employee_id;
        float salary;
      void show_employee(void)
          {
            cout << "Name: "<< name << endl;
            cout << "Id: "<< employee_id << endl;
            cout << "Salary: "<< salary << endl << endl;
          };
      };
       
      main (void)
      {
        employee worker, boss;
       
        strcpy(worker.name, "John Doe");
        worker.employee_id = 7;
        worker.salary = 25000;
       
        strcpy(boss.name, "James Doe");
        boss.employee_id = 1;
        boss.salary = 55000;
       
        worker.show_employee();
        boss.show_employee();
       
      return (0);
      }
      
      
      
    2. This program shows the concept of global resolution.

      #include <iostream.h>
      #include <string.h>
       
      class dogs{
      public:
        char breed[64];
        int avg_ht;
        int avg_wt;
        void show_breed(void);
      };
       
      void dogs::show_breed(void)
          {
            cout << "Breed: "<< breed << endl;
            cout << "Average Height: "<< avg_ht << endl;
            cout << "Average Weight: "<< avg_wt << endl << endl;
          }
       
      main (void)
      {
        dogs happy, matt;
       
        strcpy(happy.breed, "Dalmatian");
        happy.avg_ht = 58;
        happy.avg_wt = 24;
       
        strcpy(matt.breed, "Sheepdog");
        matt.avg_ht = 22;
        matt.avg_wt = 15;
       
        happy.show_breed();
        matt.show_breed();
       
      return (0);
      }
      
      
    3. This program shows the concept of Function Overloading.

      #include <iostream.h>
       
      // overload abs() in three ways
      int abs(int n);
      long abs(long n);
      double abs(double n);
       
       
      main()
      {
        cout <<"Absolute vaue of -10 :" << abs(-10) << endl;
        cout <<"Absolute vaue of -10L :" << abs(-10L) << endl;
        cout <<"Absolute vaue of -10.01 :" << abs(-10.01) << endl;
        return 0;
      }
       
       
      // abs() for ints
      int abs(int n)
      {
        cout << "Integer abs()" << endl;
        return n<0 ? -n : n;
      }
       
       
      // abs() for longs
      long abs(long n)
      {
        cout << "Long abs()" << endl;
        return n<0 ? -n : n;
      }
       
       
      // abs() for doubles
      double abs(double n)
      {
        cout << "Double abs()" << endl;
        return n<0 ? -n : n;
      }
      

      Lecture 1: Notes