码迷,mamicode.com
首页 > 编程语言 > 详细

C++程序设计_第6章_继承和派生

时间:2016-06-16 06:45:06      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

 

 

例6.1

使用默认内联函数实现单一继承。

 

 1 #include<iostream>
 2 
 3 using namespace std;
 4 
 5 class Point
 6 {
 7 private:
 8     int x, y;
 9 public:
10     Point(int a, int b)
11     {
12         x = a;
13         y = b;
14         cout << "Point..." << endl;
15     }
16     void Showxy()
17     {
18         cout << "x=" << x << "y=" << y << endl;
19     }
20     ~Point()
21     {
22         cout << "Delete Point" << endl;
23     }
24 };
25 
26 class Rectangle :public Point
27 {
28 private:
29     int H, W;
30 public:
31     Rectangle(int a, int b, int h, int w) :Point(a, b)//构造函数初始化列表
32     {
33         H = h;
34         W = w;
35         cout << "Rectangle..." << endl;
36     }
37     void Show()
38     {
39         cout << "H=" << H << ",W" << W << endl;
40     }
41     ~Rectangle()
42     {
43         cout << "Delete Rectangle" << endl;
44     }
45 };
46 
47 void main()
48 {
49     Rectangle r1(3, 4, 5, 6);
50     r1.Showxy();
51     r1.Show();
52 
53     system("pause");
54 }

 

 

123

C++程序设计_第6章_继承和派生

标签:

原文地址:http://www.cnblogs.com/denggelin/p/5589630.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!