5-1 继承与派生
#include <iostream>
#include <cstdio>
using namespace std;
class Point
{
public:
Point()
{
x = y = 0;
}
void getpoint()
{
cin>>x>>y;
}
float getX(float);
float getY(float);
void Move(float, float);
void show();
private:
float x, y;
};
void Point::Move(float a, float b)
{
x = x+a;
y = y+b;
}
float Point::getX(float a)
{
return x+a;
}
float Point::getY(float b)
{
return y+b;
}
void Point::show()
{
cout<<x<<" "<<y;
}
class Rectangle:public Point
{
public:
Rectangle()
{
w = h = 0;
}
void setdata()
{
getpoint();
cin>>w>>h;
if ( w <= 0 )
w = 0;
if ( h <= 0 )
h = 0;
}
void display();
float getW();
float getH();
private:
float w, h;
};
float Rectangle::getW()
{
return w;
}
float Rectangle::getH()
{
return h;
}
void Rectangle::display()
{
show();
cout<<" "<<w<<" "<<h<<endl;
}
int main()
{
float m,n;
Rectangle re;
re.setdata();
cin>>m>>n;
re.Move(m,n);
re.display();
return 0;
}
5-2 派生类的构造函数
#include <iostream>
#include <cstdio>
using namespace std;
class Person
{
protected:
char name[105];
char sex[105];
int age;
public:
Person()
{
cin>>name>>sex>>age;
}
};
class Employee:public Person
{
private:
int basicsalary;
int leavedays;
public:
Employee()
{
cin>>basicsalary>>leavedays;
}
void show()
{
cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"basicSalary:"<<basicsalary<<endl;
cout<<"leavedays:"<<leavedays<<endl;
}
};
int main()
{
Employee e;
e.show();
return 0;
}
5-3 多级派生类的构造函数
#include <iostream>
#include <cstdio>
using namespace std;
class Person
{
protected:
char name[105];
char sex[105];
int age;
public:
Person()
{
cin>>name>>sex>>age;
}
};
class Employee:public Person
{
private:
int basicsalary;
int leavedays;
public:
Employee()
{
cin>>basicsalary>>leavedays;
}
void show()
{
cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"basicSalary:"<<basicsalary<<endl;
cout<<"leavedays:"<<leavedays<<endl;
}
};
class Manager:public Employee
{
private:
float performance;
public:
Manager()
{
cin>>performance;
}
void show1()
{
show();
cout<<"performance:"<<performance<<endl;
}
};
int main()
{
Manager e;
e.show1();
return 0;
}
6-1 多态性与虚函数
#include <cstring>
#include <iostream>
using namespace std;
class Pet
{
public:
virtual void Speak()
{
cout<<"How does a pet speak ?"<<endl;
}
};
class Dog:public Pet
{
public:
virtual void Speak()
{
cout<<"wang!wang!"<<endl;
}
};
class Cat:public Pet
{
public:
virtual void Speak()
{
cout<<"miao!miao!"<<endl;
}
};
int main()
{
Pet h;
h.Speak();
Cat h2;
h2.Speak();
Dog h1;
h1.Speak();
return 0;
}
6-2 多态性与虚函数
#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
class Pet
{
protected:
char name[50];
public:
void Scanf(char *s)
{
strcpy( name, s );
}
virtual void Speak()
{
cout<<"How does a pet speak ?"<<endl;
}
};
class Dog:public Pet
{
public:
virtual void Speak()
{
cout<<"I am a dog,My name is "<<name<<" My sound is wang!wang!"<<endl;
}
};
class Cat:public Pet
{
public:
virtual void Speak()
{
cout<<"I am a cat,My name is "<<name<<" My sound is miao!miao!"<<endl;
}
};
int main()
{
Pet h;
h.Speak();
Cat h2;
h2.Scanf("Tom");
h2.Speak();
Dog h1;
h1.Scanf("Snoppy");
h1.Speak();
}
代码菜鸟,如有错误,请多包涵!!!
如有帮助记得支持我一下,谢谢!!!