|
The origins of C++
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; }
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; } ... }
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(); }; |
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; } |
#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); }
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 (); };
#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; }
#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"; }
#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"; }
#include ... base-class declarations derived-class declarations non-member function prototypes main() { ... ... } non-member function definitions