⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 8_17.cpp

📁 10个比较经典的C++程序。初学者就先多学习学习别人吧。
💻 CPP
字号:
#include <iostream>
using namespace std;
class Person
{public:
  Person(char* s)   {   name = new char[strlen(s)+1];   strcpy(name, s);   }
  virtual void Disp() { cout << "My name is " << name << ".\n"; }//声明虚函数
protected:
  char* name;
};
class Student : public Person
{public:
	Student(char* s, float g) : Person(s), gpa(g) { }
	void Disp()  { cout << "my name is " << name << " and my G.P.A. is " << gpa << ".\n"; }
private:
	float gpa;
};
class Professor : public Person
{public:
	Professor(char* s, int n) : Person(s), publs(n) { }
	void Disp() {cout<< "My name is "<<name<<",I have " << publs << " publications.\n";  }
private:
	int publs;
};
int main()
{	Person* p, x("zrf");
	p = &x; p->Disp();//将基类对象地址赋给基类指针
	Student y("ssh", 3.88);
	p = &y; p->Disp();//将派生类对象y地址赋给基类指针
	Professor z("zzz", 8);
	p = &z; p->Disp();//将派生类对象z地址赋给基类指针
	return 0;
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -