回 帖 发 新 帖 刷新版面

主题:[原创]C++第二讲---继承

共和国的发展一日千里,程序代码也是如此,如此数量的代码,有了错误怎么修改,如需求变更,怎么升级?????
    在这样的困境中,减少代码量是一种解决之道。工程师们在不断的思索,最终发现,一个大的程序有很多部分的代码量是完全一样的,一个几十万行的程序描述的事物千姿百态,这么多的事物之间有很多相似点,有一定的发展关系。各个事物都在“与时俱进”,从低级到高级。举例来说吧,人们最早发明了风筝,后来发明了飞机。人们发明了汽车之后,又发明了火车,数学家在认识圆形之后,又认识了矩形,从矩形又认识了圆锥。图示


    用我们上节的类的思想,描述程序如下:
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个回复)

21 楼



******************************************************************
//File:Player.cpp

#include <iostream.h>
#include "Card.h"
#include "Player.h"

CPlayer::CPlayer()
{
    cout << "\n In the CCPlayer constructor. ";
    cards_in_hand = 0;


void CPlayer::GetCard(Card newcard)
{
    Hand[cards_in_hand] = newcard;
    ++cards_in_hand;
}

void CPlayer::ShowHand()
{
    int i;

    for(i=0; i < cards_in_hand; ++i) Hand[i].ShowCard();

    cards_in_hand = 0; //reset count for next hand
}
******************************************************************
/*
Deal the Cards and Inheritance

File: Ch11ShuffleAndDealCardsDriver.cpp

This program will use a class Card and class Dealer that will shuffle and deal
the cards. 

The Dealer is a Player   
*/

//文件main_ShuffleAndDealCards

#include <iostream>
using namespace std;
#include <stdlib.h>
#include "Dealer.h"
#include "Player.h"


int ShowMenu();

int main()
{
    cout <<"\n Welcome to the Shuffle and Deal Program!"
        << "\n\n Please enter an integer for the random number generator seed.   ";

    char buf[10];
    int seed;
    cin.getline(buf,10);
    seed=atoi(buf);
    srand(seed);

    CDealer Bob;   // we have Bob, our Dealer Object, he has 52 Cards
    CPlayer Lucy;  // we have Lucy, our Player Object

    Bob.Shuffle();  // Our dealer shuffles the deck before we get started

    int choice, enough;
    bool goodbye = false;
    while (goodbye == false)
    {
        choice = ShowMenu();  // Shows user options

        switch(choice)
        {
        case 1:   // show cards
            Bob.ShowOrigCards(); 
            break;
        case 2:    // show shuffled card
            Bob.ShowShuffledCards(); 
            break;
        case 3:
            Bob.Shuffle();    
            Bob.ShowShuffledCards(); 
            break;    
        case 4:    // deal 4 cards to the dealer and player
            enough = Bob.WhereIsTheTop();
            if(enough < 43)
            {
                Bob.Deal4Cards(&Lucy);
                cout << "\n Player--Lucy's Hand: ";
                Lucy.ShowHand();  // Player shows her hand
                cout << "\n Dealer--Bob's Hand: ";
                Bob.ShowHand();  //Dealer shows his hand
            }
            else
                cout << "\n\n Not enough cards to deal, sorry.  \n";
        
            break;
        
        case 5:
            goodbye = true;
            break;
        default:
            cout << "\n I don't do that option!  \n ";
        }
    }
    cout << "\n Good bye!!  \n";

    return 0;
}

int ShowMenu()
{
    int choice;
    char buf[6];
    cout << "\n Pick your option:"
        << "\n\n 1 Show Original Deck         3 New Shuffle"
        << "\n 2 Show Shuffled Cards        4 Deal 4 Cards and Show Hands    5 Exit  \n\n" ;
    cin.getline(buf,6);
    choice = atoi(buf);
    return choice;
}
    

22 楼

我是初学者,感谢楼主的讲解!
辛苦了!

我来回复

您尚未登录,请登录后再回复。点此登录或注册