Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2545117
  • 博文数量: 308
  • 博客积分: 5547
  • 博客等级: 大校
  • 技术积分: 3782
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 09:47
个人简介

hello world.

文章分类

全部博文(308)

分类: C/C++

2010-11-25 14:41:03

    将例5.3的程序修改,补充,写成一个完整,正确的程序,用保护继承方式。在程序中应包括输入数据的函数。

#include <iostream>
using namespace std;
class Student
{
    public:
    void get_value();
    void display();
        
    protected:
    int num;
    string name;
    char sex;
};

void Student :: get_value()
{
    cin >> num >> name >> sex;
}

void Student :: display()
{
    cout << "num:" << num << endl;
    cout << "name:" << name << endl;
    cout << "sex:" << sex << endl;
}

class Student1:protected Student
{
    public:
    void display_1();
    void get_value1();
     
    private:
    int age;
    string addr;
};

void Student1 :: display_1()
{
    cout << "num:" << num << endl;
    cout << "name:" << name << endl;
    cout << "sex:" << sex << endl;
    cout << "age:" << age << endl;
    cout << "address:" << addr << endl;
}

void Student1 :: get_value1()
{
     cin >> num >> name >> sex >> age >> addr;
}

int main()
{
    Student1 stu;
    stu.get_value1();
    stu.display_1();
    system("pause");
    return 0;
}


阅读(3008) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~