久一网络科技

久一网络科技是您快速建站的首选,我们提供快速高效的服务:快速建站,服务器运维,整站建设,网络编程,网络安全等。持久的品质保证,始终如一的服务理念,久一网络科技-----您身边的网络专家。 如有需求,请联系:tech@jooyee.net


#include <cstdlib>
#include <iostream>
using namespace std;
//class Person
class Person
{
public:
Person(char*s):name(s)
{
name=new char[strlen(s)+1];
strcpy(name,s);
}
virtual void print()
{
cout<<"my name is "<<name<<endl;
}
protected:
char* name;

};


//class Student
class Student:public Person
{
public:
Student(char*s,float g):Person(s),gpa(g){}
void print()
{
cout<<"my name is "<<name<<" and my gpa is "<<gpa<<endl;
}
protected:
float gpa;

};


//class Professor
class Professor:public Person
{
public:
Professor(char*s,int n):Person(s),publs(n){}
void print()
{
cout<<"my name is "<<name<<" and I have "<<publs<<" publications"<<endl;
}
protected:
int publs;

};


//main function
int main(int argc, char *argv[])
{
Person* p;
Person x("bob");
p=&x;
p->print();
Student y("tom",3.14);
p=&y;
p->print();
Professor z("john",2);
p=&z;
p->print();
system("PAUSE");
return EXIT_SUCCESS;
}


代码复制可以直接点击右上角的复制图标,对于某些代码前面有数字的,可以用:

gawk '{print substr($0,8,length($0));}' test.c >t.c


去掉数字。

0 评论 ---->點閱數:

发表评论

关注者

热门帖子

网页浏览总次数