第11周 程序阅读-继承和派生3

#include<iostream>
using namespace std;
class  A
{
private:
    int  x;
protected:
    int y;
public:
    int z;
    A(int a,int b,int c)
    {
        x=a;
        y=b;
        z=c;
    }
    int  Getx()
    {
        return x;
    }
    int  Gety()
    {
        return y;
    }
    void ShowA()
    {
        cout<< "x="<<x<<'\t';
        cout<<"y="<<y<<'\t';
        cout<<"z="<<z<<'\n';
    }
};
class B:public A   //修改点(见后面阅读要求)
{
private:
    int m,n;
public:
    B(int a,int b,int c,int d,int e):A(a,b,c)
    {
        m=d;
        n=e;
    }
    void Show()
    {
        cout<<"m="<<m<<'\t'<<"n="<<n<<'\n';
        cout<<"x="<<Getx()<<'\t';
        cout<<"y="<<y<<'\t'<<"z="<<z<<'\n';
    }
    int Sum()
    {
        return (Getx()+y+z+m+n);
    }
};
int main()
{
    B b1(1,2,3,4,5);
    b1.ShowA();
    b1.Show();
    cout<< "Sum="<<b1.Sum()<<'\n';
    cout<<"x="<<b1.Getx()<<'\t';
    cout << "y=" <<b1.Gety()<<'\t';
    cout << "z="<<b1.z<<'\n';
    return 0;
}

运行结果:

#include<iostream>
using namespace std;
class  A
{
private:
    int  x;
protected:
    int y;
public:
    int z;
    A(int a,int b,int c)
    {
        x=a;
        y=b;
        z=c;
    }
    int  Getx()
    {
        return x;
    }
    int  Gety()
    {
        return y;
    }
    void ShowA()
    {
        cout<< "x="<<x<<'\t';
        cout<<"y="<<y<<'\t';
        cout<<"z="<<z<<'\n';
    }
};
class B:protected A   //修改点(见后面阅读要求)
{
private:
    int m,n;
public:
    B(int a,int b,int c,int d,int e):A(a,b,c)
    {
        m=d;
        n=e;
    }
    void Show()
    {
        cout<<"m="<<m<<'\t'<<"n="<<n<<'\n';
        cout<<"x="<<Getx()<<'\t';
        cout<<"y="<<y<<'\t'<<"z="<<z<<'\n';
    }
    int Sum()
    {
        return (Getx()+y+z+m+n);
    }
};
int main()
{
    B b1(1,2,3,4,5);
    b1.ShowA();
    b1.Show();
    cout<< "Sum="<<b1.Sum()<<'\n';
    cout<<"x="<<b1.Getx()<<'\t';
    cout << "y=" <<b1.Gety()<<'\t';
    cout << "z="<<b1.z<<'\n';
    return 0;
}

运行结果:

不可在类外调用protected类型函数

时间: 2024-08-16 02:53:38

第11周 程序阅读-继承和派生3的相关文章

第11周 程序阅读-继承和派生4

#include<iostream> using namespace std; class Part //部件类 { public: Part(); Part(int i); ~Part(); private: int val; }; Part::Part() { val=0; cout<<"调用Part的默认构造函数:"<<val<<endl; } Part::Part(int i) { val=i; cout<<"

第十二周程序阅读5:基类和派生类的转换

问题及代码: #include <iostream> using namespace std; class A { protected: int a,b; public: A(int aa, int bb):a(aa), b(bb) {} void printA() { cout<<"a: "<<a<<"\tb: "<<b<<endl; } }; class B: public A { int

第11周 项目三-点类派生直线类

定义点类Point,并以点类为基类,派生出直线类Line,从基类中继承的点的信息表示直线的中点.请阅读下面的代码,并将缺少的部分写出来. #include<iostream> #include<Cmath> using namespace std; class Point //定义坐标点类 { public: Point():x(0),y(0) {}; Point(double x0, double y0):x(x0), y(y0) {}; void PrintPoint(); /

第九周 程序阅读-学生信息管理系统

阅读程序"简单C++学生信息管理系统",找出其中出现构造函数.友元函数.运算符重载.静态数成员语法现象出现的位置,仔细体会其用法,在以后的设计中能够灵活应用有关方法和技巧. #include <iostream> #include <cstring> using namespace std; #define MAX 100 class CDate // 定义日期类 { private: unsigned short int year; // 年 unsigned

第15周 程序阅读-二进制文件及文件的读取1

1.阅读并运行下面的两个程序,分别用记事本和二进制文件阅读器(请自行下载Binary Viewer等程序,或者用DOS中的Debug程序,并百度其用法).查看其内容,并理解文件存储的原理. (1) #include <iostream> #include <fstream> #include <cstdlib> using namespace std; int main( ) { int a; ofstream outfile("f1.dat",io

第15周 程序阅读-二进制及二进制文件的读取3

3.阅读下面的程序,指出其功能,体会seekg().tellg()等函数的功能及其用法 (1) #include<iostream> #include <fstream> using namespace std; const char * filename = "a.txt"; int main () { long l,m; ifstream file (filename, ios::in|ios::binary); l = file.tellg(); file

第二周 程序阅读

#include <iostream> #include <cstring> using namespace std; class Student { private: int num; char name[20]; char sex; public: void set_data(int n, char *p,char s) { num=n; strcpy(name,p); sex=s; } void display( ) { cout<<"num: &quo

第九周 程序阅读-字符串类的设计

阅读下面的程序,领会其中用到的设计方案.技术手段与算法. /* 对于要定义的字符串类CMyString, 数据成员包括: - 字符串的长度: - 指向字符串第一个字符的指针 成员函数包括: - 不带参数的构造函数: - 带一个类型为const char *类型的参数(用于对字符串初始化)的构造函数: - 带一个const CMyString&类型的复制构造参数: - 析构函数: - Strlen函数 (用于求字符串的长度): - int Find(char c) (找出字符c在本字符串中第一次出

第九周-程序阅读理解

/*Copyright (c)2016,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:my.cpp *作 者:张瀚文 *完成日期:2016年5月6日 * *问题描述: 阅读程序,写出程序的运行结果并理解其运行机制. */ #include <iostream> #include<cstring> using namespace std; class A { char *a; public: A(char *aa) { a=new char[s