美文网首页
C艹之路 1.3de--

C艹之路 1.3de--

作者: 农家小升 | 来源:发表于2020-01-09 16:43 被阅读0次

例子5.1p160

//访问公有基类的成员//书上不完整,不浪费时间了
#include<string>
#include<iostream>
using namespace std;
class Student
{
public:
    void getValue()
    {cin >> num >> name >> sex;};
    void display()
    {
        cout << "num:" << num << endl;
        cout << "name" << name << endl;
        cout << "sex:" << sex << endl;
    }
private:
    int num;
    string name;
    char sex;
};
class Student1 :public Student
{
public:
    void getValue1()
    {cin>>age>>addr:}
    void display1()
    {
        cout << "age:" << age << endl;
        cout << "address:" << addr << endl;
    }
private:
    int age;
    string addr;
};

例子5.2p160


例子5.3p167

//在派生类中引用保护成员//与栗子5.1结果相同
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
protected:
    int num;
    string name;
    char sex;
};
class Student1 :protected Student
{
public:
    void getValue1();
    void display1();
private:
    int age;
    string addr;
};
void Student1::getValue1()
{
    cin >> num >> name >> sex;
    cin >> age >> addr;
}
void Student1::display1()
{
    cout << "num:" << num << endl;
    cout << "name:" << name << endl;
    cout << "sex:" << sex << endl;
    cout << "age:" << age << endl;
    cout << "address:" << addr << endl;
}
int main()
{
    Student1 stud1;
    stud1.getValue1();
    stud1.display1();
    return 0;
}

例子5.4p169


例子5.5p171

//定义简单的派生类的构造函数//注意基类和派生类的构造函数的区别
#include <iostream>
#include<string>
using namespace std;
class Student
{
public:
    Student(int n, string nam, char s)
    {
        num = n;
        name = nam;
        sex = s;
    }
    ~Student(){}
protected:
    int num;
    string name;
    char sex;
};
class Student1 :public Student
{
public:
    Student1(int n, string nam, char s, int a, string ad) :Student(n, nam, s)
    {
        age = a;
        addr = ad;
    }
    void show()
    {
        cout << "num:" << num << endl;
        cout << "name:" << name << endl;
        cout << "sex:" << sex << endl;
        cout << "age:" << age << endl;
        cout << "address:" << addr << endl << endl;
    }
    ~Student1(){}
private:
    int age;
    string addr;
};
int main()
{
    Student1 stud1(10010, "Wang-li", 'f', 19, "115road");
    Student1 stud2(10011, "Zhang-fan", 'm', 21, "213road");
    stud1.show();
    stud2.show();
}

例子5.6p175

//包括子对象的派生类的构造函数
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
    Student(int n, string nam)
    {
        num = n;
        name = nam;
    }
    void display()
    {
        cout << "num:" << num << endl << "name:" << name << endl;
    }
protected:
    int num;
    string name;
};
class Student1 :public Student
{
public:
    Student1(int n, string nam, int n1, string nam1, int a, string ad) :Student(n, nam), monitor(n1, nam1)
    {
        age = a;
        addr = ad;
    }
    void show()
    {
        cout << "This student is : " << endl;
        display();
        cout << "age:" << age << endl << "address" << addr << endl << endl;
    }
    void showMonitor()
    {
        cout << endl << "Class nonitor is: " << endl;
        monitor.display();
    }
private:
    Student monitor;//这个对象是由基类创建的对象,不是派生类
    int age;
    string addr;
};
int main()
{
    Student1 stud1(10010, "Wang_li", 10001, "Li_jun", 19, "115 Road");
    stud1.show();
    stud1.showMonitor();
        return 0;
}

例子5.7p177

//多级派生情况下的派生类的构造函数//Student1里面的char应该改了
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
    Student(int n, string nam)
    {
        num = n;
        name = nam;
    }
    void display()
    {
        cout << "num:" << num << endl << "name:" << name << endl;
    }
protected:
    int num;
    string name;
};
class Student1 :public Student
{
public:
    Student1(int n, string nam, int a) :Student(n, nam)
    {
        age = a;
    }
    void show()
    {
        display();
        cout << "age:" << age << endl;
    }
private:
    int age;
};
class Student2 :public Student1
{
public:
    Student2(int n, string nam, int a, int s) :Student1(n, nam, a)
    {
        score = s;
    }
    void showAll()
    {
        show();
        cout << "score:" << score << endl;
    }
private:
    int score;
};
int main()
{
    Student2 stu(10010, "Li", 17, 89);
    stu.showAll();
    return 0;
}

例子5.8p181

//使用多重继承的派生类的构造函数(意思是说有两个父类)//代码有问题
#include<iostream>
#include<string>
using namespace std;
class Teacher
{
public:
    Teacher(string nam, int a, string t)
    {
        name = nam;
        age = a;
        title = t;
    }
    void display()
    {
        cout << "name:" << name << endl;
        cout << "age:" << age << endl;
        cout << "title" << title << endl;
    }
protected:
    string name;
    int age;
    string title;
};
class Student
{
public:
    Student(string nam, char s, float sco)
    {
        strcpy(name1, nam);
        sex = s;
        score = s;
    }
    void display1()
    {
        cout << "name:" << name1 << endl;
        cout << "sex:" << sex << endl;
        cout << "score" << score << endl;
    }
protected:
    string name1;
    char sex;
    float score;
};
class Graduate :public Teacher, public Student
{
public:
    Graduate(string nam, int a, char s, string t, float sco, float w) :Teacher(nam, a, t), Student(nam, s, sco), wage(w) {}
    void
};

例子5.9p190

从5.8里面对两个父类增加了个基类


例子5.10p193

从5.9里面进行了更改


相关文章

  • C艹之路 1.3de--

    例子5.1p160 例子5.2p160 例子5.3p167 例子5.4p169 例子5.5p171 例子5.6p1...

  • C艹之路总览

    提醒:目前还没有完成,所以请不用看本文章,因为谁也不知道,会不会太监了进度:目前完成了V1.1,V1.3,基本完成...

  • C艹之路 1.3c--

    3.5 例子3.1p70 例子3.2p72 例子3.3p74 例子3.4p 例子3.5p79 例子3.6p 例子3...

  • C艹之路 1.3a--

    例子1.1p2 //输出一行字符 例子1.2p3 // 例子1.3p4 例子1.4p5 例子1.5p 例子1.5p...

  • C艹之路 1.3b--

    目录 例子2.1;用类来实现输入和输出时间(时:分:秒)//;这里面没有函数//;author 例子2.1p54 ...

  • C艹之路 1.3f--

    例子6.1p204 例子6.2p204

  • C艹之路 V1.1 知识补习

    V1.1主要以谭浩强的书籍为主 简单知识 与C语言(所有定义必须放在函数体最前面)相比,随用随定义namespac...

  • C艹之路 V1 C++基础语法复习

    正在快速熟悉语法中... 因为需要恢复手感,所以干脆就拿着谭浩强书籍开始第一步,然后在快速进入状态.等语法差不多了...

  • C艹之路 1.1a--引用的目的,注意

    目的 联想swap()函数机会明白了[p20]使用引用时,在swap()的参数直接设为引用参数就可以了 注意 可以...

  • C艹之路 1.1b--类的知识

    类是抽象的,不占据内存空间,对象是具体的,占用存储空间 struct也可以声明类但是默认都是公有地private,...

网友评论

      本文标题:C艹之路 1.3de--

      本文链接:https://www.haomeiwen.com/subject/twoyactx.html