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

📄 hour13_1.cpp

📁 《24学时精通c++》的随书源码的下半部分。欢迎下载学习。
💻 CPP
字号:
 //Listing 13.1 Overloading class member functions

 #include <iostream>

 

 // Rectangle class declaration

 class Rectangle

 {

 public:

     // constructors

     Rectangle(int width, int height);

     ~Rectangle(){}

 

     // overloaded class function DrawShape

     void DrawShape() const;

     void DrawShape(int aWidth, int aHeight) const;

     void DrawShape(int aWidth=2, int aHeight=3) const;   // Well, this is how you'd create the prototype, but
                                                          // how do you make a different function definition?
                                                          // You will get a compile error because the compiler
                                                          // can't figure out which version to call!  Sorry.
                                                          // This means that the args to overloaded functions
                                                          // must be different in type or number in order to work.
                                                          // In this case they are the same types and number.

 

 private:

     int itsWidth;

     int itsHeight;

 };

 

 //Constructor implementation

 Rectangle::Rectangle(int width, int height)

 {

     itsWidth = width;

     itsHeight = height;

 }

 

 

 // Overloaded DrawShape - takes no values

 // Draws based on current class member values

 void Rectangle::DrawShape() const

 {

     DrawShape( itsWidth, itsHeight);

 }

 

 

 // overloaded DrawShape - takes two values

 // draws shape based on the parameters

 void Rectangle::DrawShape(int width, int height) const

 {

     for (int i = 0; i<height; i++)

     {

         for (int j = 0; j< width; j++)

         {

             std::cout << "*";

         }

         std::cout << "\n";

     }

 }

 

 // Driver program to demonstrate overloaded functions

 int main()

 {

     // initialize a rectangle to 30,5

     Rectangle theRect(30,5);

     std::cout << "DrawShape(): \n";

     theRect.DrawShape();

     std::cout << "\nDrawShape(40,2): \n";

     theRect.DrawShape(40,2);

     return 0;

 }

⌨️ 快捷键说明

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