第13周 程序阅读-虚函数

#include<iostream>
using namespace std;
class A {
int a;
public:
   A():a(5){}
   virtual void print()const { cout<<a;}
};
class B: public A {
   char b;
public:
    B() { b='E'; }
    void print() const { cout<<b; }
};
void show(A &x) { x.print(); }
int main()
{
   A d1,*p;
   B d2;
   p=&d2;
   d1.print();
   d2.print();
   p->print();
   show(d1);
   show(d2);
   return 0;
}

运行结果:

时间: 2024-08-25 15:32:51

第13周 程序阅读-虚函数的相关文章

第13周 程序阅读-虚析构函数

#include <iostream> using namespace std; class BASE { private: char c; public: BASE(char n):c(n) {} virtual ~BASE() { cout<<c; } }; class DERIVED:public BASE { private: char c; public: DERIVED(char n):BASE(n+1),c(n) {} ~DERIVED(){ cout<<

第13周 程序阅读-纯虚函数

#include <iostream> using namespace std; class Base { public: virtual void Who() =0; }; class FirstDerived:public Base { public: void Who() { cout<<"F"; } }; class SecondDerived:public Base { public: void Who() { cout<<"S&

第13周项目2-纯虚函数形类家庭

写一个程序.定义一个抽象基类Shape,它是从衍生3派生类.Circle(周围).Rectangle(矩形).Triangle(三角).例如,下面的main()性能.划定区域并找到一些几何. int main() { Circle c1(12.6),c2(4.9);//建立Circle类对象c1,c2,參数为圆半径 Rectangle r1(4.5,8.4),r2(5.0,2.5);//建立Rectangle类对象r1,r2,參数为矩形长.宽 Triangle t1(4.5,8.4),t2(3.

第二周 程序阅读

#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

第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

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

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

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

阅读下面的程序,领会其中用到的设计方案.技术手段与算法. /* 对于要定义的字符串类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

第九周-程序阅读

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