⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch05_3.cpp

📁 Since the field of object oriented programming is probably new to you, you will find that there is a
💻 CPP
字号:
                             // Chapter 5 - Programming exercise 3
#include <iostream.h>

class rectangle {              // A simple class
   int height;
   int width;
public:
   rectangle(void);            // with a constuctor
   int area(void);             // two methods
   void initialize(int, int);
   ~rectangle(void);           // and a destructor
};

rectangle::rectangle(void)     // constuctor
{
   cout << "Rectangle constructor speaking\n";
   height = 6;
   width = 6;
}

int rectangle::area(void)      //Area of a rectangle
{
   return height * width;
}

void rectangle::initialize(int init_height, int init_width)
{
   height = init_height;
   width = init_width;
}

rectangle::~rectangle(void)    // destructor
{
   cout << "Rectangle destructor speaking\n";
   height = 0;
   width = 0;
}

struct pole {
   int length;
   int depth;
};



main()
{
rectangle box, square;
pole flag_pole;

   cout << "The area of the box is " << 
                       box.area() << "\n";
   cout << "The area of the square is " << 
                       square.area() << "\n";

// box.height = 12;
// box.width = 10;
// square.height = square.width = 8;

   box.initialize(12, 10);
   square.initialize(8, 8);

   flag_pole.length = 50;
   flag_pole.depth = 6;

   cout << "The area of the box is " << 
                       box.area() << "\n";
   cout << "The area of the square is " << 
                       square.area() << "\n";
// cout << "The funny area is " << 
//                     area(square.height, box.width) << "\n";
// cout << "The bad area is " << 
//                     area(square.height, flag_pole.depth) << "\n";
}




// Result of execution
//
// Rectangle constructor speaking
// Rectangle constructor speaking
// The area of the box is 36
// The area of the square is 36
// The area of the box is 120
// The area of the square is 64
// Rectangle destructor speaking
// Rectangle destructor speaking

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -