每日打卡虚函数
虚函数,纯虚函数不能直接定义对象,可以定义指针,但他的派生可以定义对象;
注意最后一道题,纯虚数,派生的类仍为纯虚数,因为派生中没有将基类的全部纯虚数重新定义;
a=d;
*a=&d;
&a=d;
#include<iostream>
using namespace std;
class people
{
protected:
int age;
string name;
public:
people() {};
people(int x,string y):age(x),name(y){}
~people() {};
void setValue(int m, string str)
{
age = m; name = str;
}
virtual void display()const = 0;
};
class student:public people
{
private:
int studentID;
public:
student(){}
student(int n):studentID(n){}
~student(){}
void setID(int m)
{
studentID = m;
}
void display()const
{
cout << name << " " << age << " " << studentID << endl;
}
};
class teacher :public people
{
private:
int teacherID;
public:
teacher(){}
teacher(int n):teacherID(n){}
~teacher(){}
void setID(int m)
{
teacherID = m;
}
void display()const
{
cout << name << " " << age << " " << teacherID << endl;
}
};
int main()
{
student a;
teacher b;
people *c;
c = &a;
c->setValue(10, "zhao");
a.setID(123);
c->display();
c = &b;
c->setValue(20, "liu");
b.setID(456);
c->display();
return 0;
}
#include<iostream>
using namespace std;
class Animal
{
public:
Animal(){}
virtual void speak()
{
cout << "My name is Animal."<<endl;
}
};
class Cat :public Animal
{
public:
Cat(){}
void speak()
{
cout << "My name si Cat."<<endl;
}
};
class Leopard :public Animal
{
public:
Leopard(){}
void speak()
{
cout << "My name is Leopard." << endl;
}
};
void fun(Animal *p)
{
p->speak();
}
int main()
{
Animal a;
Cat b;
Leopard c;
//a = &b;
//a->speak();
//a = &c;
//a->speak();
fun(&a);
fun(&b);
fun(&c);
return 0;
}
#include<iostream>
#define PI 3.1415
using namespace std;
class shape
{
public:
virtual void setvalues() = 0;
virtual float area() = 0;
};
class rectangle :public shape
{
public:
float x, y;
public:
void setvalues()
{
float n; float m;
cin >> n >> m;
x = n;
y = m;
}
float area()
{
return x * y;
}
};
class sanjiaoxing:public shape
{
public:
float x, y;
public:
void setvalues()
{
float n, m;
cin >> n >> m;
x = n;
y = m;
}
float area()
{
return (float)(x * y)/2;
}
};
class zhengfangxing :public shape
{
public:
float x;
public:
void setvalues()
{
float n;
cin >> n;
x = n;
}
float area()
{
return x * x;
}
};
class yuanxing :public shape
{
public:
float x;
public:
void setvalues()
{
float n;
cin >> n;
x = n;
}
float area()
{
return x * x*PI;
}
};
int main()
{
shape* a;
rectangle b;
sanjiaoxing c;
zhengfangxing d;
yuanxing e;
a = &b;
a->setvalues();
if (b.x <= 0 || b.y <= 0)
{
cout << "Set Value Error!";
return 0;
}
cout<<a->area()<<endl;
a = &c;
a->setvalues();
if (c.x <= 0 || c.y <= 0)
{
cout << "Set Value Error!";
return 0;
}
cout << a->area()<<endl;
a = &d;
a->setvalues();
if (d.x <= 0)
{
cout << "Set Value Error!";
return 0;
}
cout << a->area()<<endl;
a = &e;
a->setvalues();
if (e.x <= 0)
{
cout << "Set Value Error!";
return 0;
}
cout << a->area();
}
#include<iostream>
using namespace std;
class student
{
protected:
string name;
int studentID;
public:
student(string x, int y) :name(x), studentID(y){}
void setnameid()
{
int x;
string y;
cin >>y >>x;
name = y;
studentID = x;
}
void displaynameid()
{
cout << name <<studentID<<endl;
}
virtual void setmajor() = 0;
virtual void getmajor() = 0;
virtual void setadvisor() = 0;
virtual string getadvisor() = 0;
};
class understud :public student//也是抽象类
{
protected:
string major;
public:
understud(string x, int y, string z) :student(x, y)
{
major = z;
}
void setmajor()
{
string x;
cin >> x;
major = x;
}
void getmajor()
{
cout << major<<endl;
}
};
class poststud :public understud
{
protected:
string advisor;
public:
poststud(string x="***", int y=-1, string z="###", string m ="&&&") :understud(x,y,z)
{
advisor = m;
}
void setadvisor()
{
string x;
cin >> x;
advisor = x;
}
string getadvisor()
{
return advisor;
}
};
int main()
{
student* a;
understud *b;
poststud d;
int q;
cin >> q;
if (q ==1)
{
a = &d;
a->setnameid();
a->setmajor();
a->setadvisor();
a->displaynameid();
a->getmajor();
cout << a->getadvisor() << endl;
}
if (q == 0)
{
d;
a = &d;
a->displaynameid();
a->getmajor();
cout << a->getadvisor() << endl;
}
return 0;
}
本文来自博客园,作者:赵千万,转载请注明原文链接:https://www.cnblogs.com/zhaoqianwan/p/17331365.html
千万千万赵千万