ÄúµÄλÖãºÑ°ÃÎÍøÊ×Ò³£¾±à³ÌÀÖÔ°£¾C/C++±à³Ì£¾Tutorial of C++

C++ Programming Tutorial I (b)

Overview of C++ Features


The origins of C++

  • Bjarne Stroustrup in 1980

  • Extension to C

  • Using object-oriented paradigm

  • Objectives:

    • Allows programmer to manage large, complex programs

    • Maintain C's efficiency and flexibility


Your first C++ program!


#include   <iostream.h>
    
main()
{
   int    i;

   cout << "This is output.\n";   // single line comment
        
   /*
       multi-line comment
   */
    
   // input a number using >>
   cout << "enter a number: ";
   cin >> i;
    
   // now, output a number using  <<
   cout << i << " squared is " << i*i << "\n";

   return 0;
}


  • iostream.h

  • The << operator

    • still is the left-shift operator

    • now also output operator

    • cascading several output elements

  • The >> operator

    • still is the right-shift operator

    • now also input operator

    • no & before the receiving variable is needed!

  • Comments

    • single line vs multiple lines


Declaring local variables


/* Incorrect in C. OK in C++ */
    
swap(int a, int b)
{
   char    reply;

   cout << "Swap the values? ";
   cin >> reply;

   if (reply == 'y') {
    cout << "Original values are: " << a << " and " << b;
    ...
    int k;    // won't compile in C
    k = a;
    a = b;
    b = k;
   }

   ...
}


  • You can declare local variables at any point within a block, not just the beginning.

  • Help to avoid accidental side effects.

  • However, someone may find it difficult to read.


Introducing C++ Classes


class employee {
   char     name[80];
   double    wage;

public:
   void putname (char *n);
   void getname (char *n);
   void putwage (double w);
   double getwage();
};
class  class-name  {
    private data and functions 

public:
    public data and functions 

}  object list ;


  • A class may contain private as well as public parts.

  • Private parts cannot be accessed by any function that is not a member of the class. (Encapsulation)

  • All variables and functions defined after public can be accessed by all other functions in the program.

  • member functions and member variables

  • The class name employee becomes a new data type specifier.

        
        employee  an_employee;
        
        

  • A class is a logical abstraction, an instantiated object is real.


Introducing C++ Classes


void employee::putname (char *n)
{
   strcpy (name, n);
}


void employee::getname (char *n)
{
   strcpy (n, name);
}


void employee::putwage (double w)
{
   wage = w;
}


double employee::getwage ()
{
   return wage;
}
class employee {
   char     name[80];
   double    wage;

public:
   void putname (char *n);
   void getname (char *n);
   void putwage (double w);
   double getwage();
};


  • "::" is called the scope resolution operator

  • Several different classes can use the same function name.


Introducing C++ Classes


main()
{
   employee  ted;
   char      l_name[80];

   ted.putname ("Ted Jones");
   ted.putwage (75000);

   ted.getname (l_name);
   cout << l_name << " makes $";
   cout << ted.getwage() 
        << " per year.\n";

   return 0;
}
class employee {
   char     name[80];
   double    wage;

public:
   void putname (char *n);
   void getname (char *n);
   void putwage (double w);
   double getwage();
};


void employee::putname (char *n)
{
   strcpy (name, n);
}


void employee::getname (char *n)
{
   strcpy (n, name);
}


void employee::putwage (double w)
{
   wage = w;
}


double employee::getwage ()
{
   return wage;
}


  • When you refer to a member of a class from a piece of code that is not part of the class, use the dot operator.

  • A member function can call another member function or refer to a data member directly, without using the dot operator.


Function Overloading


#include  <iostream.h>
#include  <stdio.h>
#include  <string.h>

void stradd (char *s1, char *s2);
void stradd (char *s1, int i);

main()
{
   char   str[80];

   strcpy (str, "Hello ");
   stradd (str, "there");
   cout << str << "\n";

   stradd (str, 100);
   cout << str << "\n";

   return 0;
}

// concatenate two strings
void stradd (char *s1,  char *s2)
{
   strcat (s1, s2);
}

// concatenate a string with a "stringized" integer
void stradd (char *s1,  int i)
{
   char  temp[80];

   sprintf (temp, "%d", i);
   strcat (s1, temp);
}


  • Two or more functions can share the same name as long as they have different parameter declarations.

  • The compiler knows which function to call in each situation because of the type of the arguments.

  • One way to achieve polymorphism.


Inheritance


class building {
   int     rooms;
   int  floors;
   int  area;

public:
   void   set_rooms (int num);
   int    get_rooms ();
   void   set_floors (int num);
   int    get_floors ();
   void   set_area (int num);
   int    get_area ();
};

// house is derived from building
 class house : public building {
   int   bedrooms;
   int   baths;

public:
   void   set_bedrooms (int num);
   int    get_bedrooms ();
   void   set_baths (int num);
   int    get_baths ();
};



  • Base class - defines those qualities common to all objects to be derived from the base class.

  • Derived class - classes derived from the base class.

  • A derived class includes all features of the base class and then adds qualities specific to the derived class.


A sample program illustrating inheritance


#include  <iostream.h>

class building {
   int     rooms;
   int  floors;
   int  area;
public:
   void   set_rooms (int num);
   int    get_rooms ();
   void   set_floors (int num);
   int    get_floors ();
   void   set_area (int num);
   int    get_area ();
};

// house is derived from building
 class house : public building {
   int   bedrooms;
   int   baths;
public:
   void   set_bedrooms (int num);
   int    get_bedrooms ();
   void   set_baths (int num);
   int    get_baths ();
};

// school is also derived from building
 class school : public building {
   int    classrooms;
   int    offices;
public:
   void   set_classrooms (int num);
   int    get_classrooms ();
   void   set_offices (int num);
   int    get_offices ();
};


// ---- base class building ---------------

void building::set_rooms (int num)
{
   rooms = num;
}

void building::set_floors (int num)
{
   floors = num;
}

void building::set_area (int num)
{
   area = num;
}

int building::get_rooms()
{
   return rooms;
}

int building::get_floors()
{
   return floors;
}

int building::get_area()
{
   return area;
}

// ---- derived class house ---------------

void house::set_bedrooms (int num)
{
   bedrooms = num;
}

void house::set_baths (int num)
{
   baths = num;
}

int house::get_bedrooms ()
{
   return bedrooms;
}

int house::get_baths ()
{
   return baths;
}

// ---- derived class school -----------------

void school::set_classrooms (int num)
{
   classrooms = num;
}

void school::set_offices (int num)
{
   offices = num;
}

int school::get_classrooms()
{
   return classrooms;
}

int school::get_offices()
{
   return offices;
}


main()
{
   house h;
   school s;

   h.set_rooms(12);
   h.set_floors(3);
   h.set_area(4500);
   h.set_bedrooms(5);
   h.set_baths(3);

   cout << "house has " << h.get_bedrooms();
   cout << " bedrooms\n";

   s.set_rooms(200);
   s.set_classrooms(180);
   s.set_offices(5);
   s.set_area(25000);

   cout << "school has " << s.get_classrooms();
   cout << " classrooms\n";
   cout << "Its area is " << s.get_area();

   return 0;
}


Constructors


  • Objects may require initialization before it can be used.

  • Automatic initialization is performed through the use of a constructor function.

  • A constructor function is a special function that is a member of a class and has the same name as that class.

  • An object's constructor function is automatically called when the object is created.

  • Constructor functions cannot return values and, thus, have no return type.


#define   <100>

class stack {
   int   stck[SIZE];
   int   tos;
public:
   stack();
   void push(int i);
   int pop();
};

// stack's constructor function
stack::stack()
{
   tos = 0;
   cout << "Stack Initialization\n";
}


Destructors


  • An object may need to perform some action or actions when it is destroyed.

  • When an object is destroyed, its destructor function (if it has one) is automatically called.

  • A destructor function is a special function that is a member of a class and has the same name as that class, but preceded by a ~.

  • A destructor function cannot return values.


#define   <100>

class stack {
   int   stck[SIZE];
   int   tos;
public:
   stack();    // constructor
   ~stack();   // destructor
   void push(int i);
   int pop();
};

// stack's constructor function
stack::stack()
{
   tos = 0;
   cout << "Stack Initialization\n";
}

// stack's destructor function
stack::~stack()
{
   cout << "Stack destroyed\n";
}


General Form of a C++ Program


#include ...

base-class declarations

derived-class declarations

non-member function prototypes

main()
{
   ...
   ...
}

non-member function definitions


End of Tutorial I (b)


 

[ tutorial 1(a)| tutorial 1(b)| tutorial 2| tutorial 3| tutorial 4]