主题:[原创]C++第二讲---继承
flagxu
[专家分:0] 发布于 2006-06-21 23:59:00
共和国的发展一日千里,程序代码也是如此,如此数量的代码,有了错误怎么修改,如需求变更,怎么升级?????
在这样的困境中,减少代码量是一种解决之道。工程师们在不断的思索,最终发现,一个大的程序有很多部分的代码量是完全一样的,一个几十万行的程序描述的事物千姿百态,这么多的事物之间有很多相似点,有一定的发展关系。各个事物都在“与时俱进”,从低级到高级。举例来说吧,人们最早发明了风筝,后来发明了飞机。人们发明了汽车之后,又发明了火车,数学家在认识圆形之后,又认识了矩形,从矩形又认识了圆锥。图示
用我们上节的类的思想,描述程序如下:
class Circle//圆形类
{
public:
int x;//圆心横坐标
int y;//圆心纵坐标
int r;//圆的半径
double Girth()//圆的周长
{
return 2*3.1415926*r;
}
};
class Column:public Circle//圆柱体类,继承开始了
{
public:
int h;//圆柱体的高
double Area()//圆的面积
{
return 2*3.1415926*r*r+Girth()*h;//调用继承过来的r,h
}
double V()//圆的体积
{
return 3.1415926*r*r*h;//调用继承过来的r,h
}
void Disp()//信息打印
{
printf("%f\n",Area());
printf("%f\n",V());
}
};
void main()//主函数
{
Column c1;//类与对象
c1.x=0;c1.y=0;c1.r=3;c1.h=5;//对象的初始化
c1.Disp();//对象调用成员函数
}
结果:
150.796445
141.371667
^_^,这是最简单的继承,也就是这样,减少了代码书写量,方便了程序的修改与升级,继承还有很多其它的概念,protected,private继承,多继承,二义性等其它问题,我们以后探讨。
[em9][em9][em9]
回复列表 (共22个回复)
11 楼
andows [专家分:310] 发布于 2006-06-23 09:59:00
我是一个新手
我能够感觉到新手在学习C++过程中遇到的困惑,比如说学习类的过程中,一个很基本的迷惘是:学了这东西,该怎么来用,或者说是这东西是拿来干什么的?所以这第一点建议就是坚持尽量多的多采用实例来做讲解的素材!
第二点建议:新手在学习过程中,往往会因为弄不懂一些东西的本质上是什么,而产生迷惑,比如说对于继承,往往会产生如下问题:为什么public继承仍然不能访问pri却可以访问protect?所以建议楼主在问题的深度上下一些工夫。
还有就是关于一些定义:比如什么是构造函数,什么是权限的问题,初学者往往弄不明白,希望可以注意一下这些问题。
第三点:---不是建议,只是为了说明,我的第二嗲建议,并不是在影射楼主的文章没有深度,对于楼主的工作,我是持支持态度,并在心中有感激之情的,我只是希望楼主将工作做的更好!
12 楼
Loverghh [专家分:520] 发布于 2006-06-27 18:33:00
11楼说的好顶,当然楼主更要顶了。
但楼主,可不可以讲一些类的其它关系了,比如说组合?我C++学的特别郁闷,学了都不知道怎么用。
小妹再次谢谢了。
13 楼
ituse [专家分:290] 发布于 2006-06-28 02:31:00
不是很全面...
有待完善。。。。
14 楼
shijizhisheng [专家分:180] 发布于 2006-06-29 16:22:00
感谢楼主,得到东西不少。希望将其他的几章内容也讲一下。
15 楼
chenqy344100 [专家分:0] 发布于 2006-07-02 21:09:00
确实是简单点了,请多给我讲点THIS指针的用法
16 楼
深邃之瞳 [专家分:0] 发布于 2006-07-06 17:56:00
我是小树苗 在大家你一言我一语的春雨下茁壮成长
(好傻的话 呵呵 就是这个意思啦)
17 楼
fanman1984t [专家分:20] 发布于 2006-07-11 22:52:00
谢谢搂主的讲解,有个问题想请教
对于一个操作系统,类通过继承,是不是最终形成了一个很多很繁琐的系统了吗?我们在编程的时候怎么去应用不记住他们呢?是不是很难啊!
18 楼
shiwy [专家分:40] 发布于 2006-07-12 09:13:00
应加上#include<stdio.h>
19 楼
Flouse [专家分:0] 发布于 2006-08-05 11:33:00
[size=5][color=FF0000]我的练习[/color][/size]
//文件Card.h
#if !defined(AFX_CARD_H__56325F26_9887_44F1_BCA8_A277F9B70530__INCLUDED_)
#define AFX_CARD_H__56325F26_9887_44F1_BCA8_A277F9B70530__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
enum Suit { club/*梅花*/, diamond/*方块*/, heart/*红桃*/, spade/*黑桃*/};
enum Rank { two = 2, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace};
class Card
{
public:
Card();
void SetSuit(Suit s ){ color = s; }
void SetRank(Rank r ){ number = r; }
void ShowCard();
int operator > (Card N);
private:
Suit color;
Rank number;
};
#endif // !defined(AFX_CARD_H__56325F26_9887_44F1_BCA8_A277F9B70530__INCLUDED_)
******************************************************************
//File:Card.cpp
#include "Card.h"
#include <iostream.h>
Card::Card()
{
color = club;
number = two;
}
void Card::ShowCard()
{
if(number < 11) cout << number;
else if(number == jack) cout << "J";
else if(number == queen) cout << "Q";
else if(number == king) cout<< "K";
else cout << "A";
switch(color)
{
case spade: cout << char(6)<< " "; break;
case heart: cout << char(3)<< " "; break;
case diamond: cout << char(4)<< " "; break;
case club: cout << char(5)<< " "; break;
}
}
int Card::operator >(Card N)
{
if(number > N.number)return 1;
if(number == N.number)
{
if(color > N.color)return 1;
else return 0;
}
return 0;
}
*********************************************************************
//文件Dealer.h
#ifndef _CH11CDEALER_H
#define _CH11CDEALER_H
#include "Card.h"
#include "Player.h"
class CDealer: public CPlayer
{
private:
Card card[52];
int index[52];
int top_card; // index that points at the top card in the deck
public:
CDealer() ;
void Shuffle();
void ShowOrigCards();
void ShowShuffledCards();
void Deal4Cards(CPlayer* X);
int WhereIsTheTop() { return top_card;}
};
#endif
20 楼
Flouse [专家分:0] 发布于 2006-08-05 11:35:00
****************************************************************
//File:Dealer.cpp
#include <iostream.h>
#include <stdlib.h> //for srand and rand
#include "Card.h"
#include "Dealer.h"
CDealer::CDealer()
{
//The CDealer constructor will set up the 52 cards and set top of deck to zero
cout << "\n In the Dealer constructor. ";
top_card = 0;
int i, j, card_ctr = 0; // array index for cards
for (i = 3; i >= 0; --i) // this loop is for the 4 suits
{
for(j = 14; j >=2; --j)
{
card[card_ctr].SetSuit((Suit) i);
card[card_ctr].SetRank((Rank) j);
++ card_ctr;
}
}
for (i=0; i<52; ++i) index[i]=i; // fill index array w/ integers
}
void CDealer::Shuffle() //change index
{
int i, random;
top_card = 0; //??
bool check[52]; //if used?
//zero all values in the check array--this will keep track of #'s we've used
for(i=0; i<52; ++i) check[i] = false;
int got_a_good_one = 0;
for(i=0; i < 52 ; ++i) // we will get a random number for each index value
{
while( got_a_good_one == 0) // loop until we get a non-used number
{
random = rand()%52; // gives us a number between 0 - 51
if(check[random] == false)
{
index[i] = random; // set the indexput random into index array
check[random] = true; // set check on--this number has been used
got_a_good_one = 1;
}
}
got_a_good_one = 0; // ready for next one
}
}
void CDealer::ShowOrigCards()
{
// write out the cards by use the ASCII symbol for card suits
int i, card_ctr = 0;
for(i = 0; i < 52; ++i)
{
card[i].ShowCard();
card_ctr++;
if( card_ctr == 13 )
{
cout << endl; // newline if new suit
card_ctr = 0;
}
}
}
void CDealer::ShowShuffledCards()
{
// write out the cards using the index array--shuffled order
int i, card_ctr = 0;
for(i = 0; i < 52; ++i)
{
card[ index[i] ].ShowCard();
card_ctr++;
if( card_ctr == 13 )
{
cout << endl; // newline if new suit
card_ctr = 0;
}
}
}
void CDealer::Deal4Cards(CPlayer *who)
{
int i;
for(i = 0; i<4; ++i)
{
who->GetCard(card[index[top_card]]) ;
this->Hand[i] = card[index[top_card + 1]];
top_card += 2;
}
this->cards_in_hand = 4;
}
**********************************************************************
//文件Player.h
#ifndef CPLAYER_H
#define CPLAYER_H
#include "Card.h"
class CPlayer //use Card
{
public:
CPlayer();
void GetCard( Card X );
void ShowHand();
protected:
Card Hand[4]; // the players have a hand of cards
int cards_in_hand; //有几张牌??
};
#endif
我来回复