学习目标:通过学习,能够理解不同的情况下创建不同的类、对象实现一类具有相同属性的物体,例如:公鸡、虎、剑齿虎等等。
例题(一):
#include <iostream>
using namespace std;
class Dog
{
private:
int age;
double weight;// 狗的体重
public:
void setdog(int D_a, double D_w)//设置狗的体重和年龄
{
age = D_a;
weight = D_w;
}
void showdog() //显示狗的年龄和体重
{
cout << "age:" << age << "weight:" << weight << endl;
}
};
int main()
{
Dog dog1;//定义一个狗的对象
dog1.setdog(3,56);//设置狗的年龄 体重
dog1.showdog();//显示狗的 年龄 体重
return 0;
}
例题2:
#include <iostream>
using namespace std;
class Person
{
private:
char name[20];//姓名
char sex;//性别
int age;//年龄
char* addr;//住址
public:
Person()//无参构造函数
{
strcpy(name, "张三");
age = 0;
sex = 'x';
addr = NULL;
}
Person(char* n, int age, char sex, char* addr)//有参构造函数
{
strcpy(name, n);
this->age = age;
this->sex = sex;
int L = strlen(addr);
this->addr = new char[L + 1];
strcpy(this->addr, addr);
}
//拷贝构造函数
Person(Person& p)
{
strcpy(name, p.name);
sex = p.sex;
age = p.age;
addr = new char[strlen(p.addr) + 1];
strcpy(addr, p.addr);
}
~Person()//析构函数
{
cout << "Now destroying Person" << name << endl;
if (addr != NULL)
{
delete[] addr;
}
}
void Modify(char* name, int age, char sex, char* addr);//信息修改函数
void ShowMe();//显示信息函数
};
void Person::Modify(char* n, int age, char sex, char* addr)//信息修改函数
{
strcpy(name, n);
this->age = age;
this->sex = sex;
int L = strlen(addr);
if (this->addr != NULL) delete[] this->addr;
this->addr = new char[L + 1];
strcpy(this->addr, addr);
}
void Person::ShowMe()//显示信息函数
{
cout << name << " " << age << " " << sex << " ";
if (addr != NULL) cout << addr;
cout << endl;
}
Person fun(Person p3)
{
return p3;
}
int main()
{
Person person1;//创建person1对象
Person person2("小丽", 19, 'w', "中国广东省深圳市");//创建person2对象
Person person3(person2);//以person2对象为蓝本,创建person3对象
cout << "person1: ";
person1.ShowMe();//显示person1信息
cout << "person2: ";
person2.ShowMe();//显示person2信息
person2.Modify("赵五", 29, 'm', "中国广州市");//修改person2信息
cout << "person2: ";
person2.ShowMe();//再次显示person2信息
cout << "person3: ";
person3.ShowMe();//显示person3信息
person3.Modify("李四", 30, 'm', "中国上海市");//修改person3信息
cout << "person3: ";
person3.ShowMe();//再次显示person3信息
return 0;
}
例题3:
#include<string>
using namespace std;
class CKind
{
private:
string m_kind_ID;//产品类型ID
string m_kind_name;//产品类型名称
static int m_count;//产品类型总数
public:
CKind(string id = "0000", string name = "未初始化");//带默认形参值的构造函数
CKind(const CKind&);//拷贝构造函数
~CKind();//析构函数
string get_id()const;//常成员函数,获取产品类型ID
void set_id(const string&);//成员函数,设置产品类型ID
string get_name()const;//常成员函数,获取产品类型名称
void set_name(const string&);//成员函数,设置产品类型名称
static int get_kind_count();//静态成员函数,获取产品类型总数
void display()const;//常成员函数,输出产品类型ID和产品类型名称
};
int CKind::m_count = 0;/// 静态数据成员初始化
CKind::CKind(string id, string name)//带默认形参值的构造函数
{
m_kind_ID = id;
m_kind_name = name;
m_count++;//类型总数+1
}
CKind::CKind(const CKind& obj)//拷贝构造函数
{
m_kind_ID = obj.m_kind_ID;
m_kind_name = obj.m_kind_name;
m_count++;//类型总数+1
}
CKind::~CKind()//析构函数
{
m_count--;//类型总数-1
cout << m_kind_name << "类型被删除了" << endl;
}
string CKind::get_id()const//常成员函数,获取产品类型ID
{
return m_kind_ID;
}
void CKind::set_id(const string &id)//成员函数,设置产品类型ID
{
m_kind_ID = id;
}
string CKind::get_name()const//常成员函数,获取产品类型名称
{
return m_kind_name;
}
void CKind::set_name(const string&name)//成员函数,设置产品类型名称
{
m_kind_name = name;
}
int CKind::get_kind_count()//静态成员函数,获取产品类型总数
{
return m_count;
}
void CKind::display()const//常成员函数,输出产品类型ID和产品类型名称
{
cout << "类型ID : " << m_kind_ID << "\t类型名:" << m_kind_name;
}
int main()
{
cout << "总类型个数为:" << CKind::get_kind_count() << endl;
CKind *kind1 = new CKind("1100","篮球");
cout << "新增加了一个类型为:";
kind1->display();
cout << endl;
cout << "总类型个数为:" << CKind::get_kind_count() << endl;
CKind *kind2 = new CKind("1101", "足球");
cout << "新增加了一个类型为:";
kind2->display();
cout << endl;
cout << "总类型个数为:" << CKind::get_kind_count() << endl;
delete kind1;
cout << "删除此类型后,总类型个数为:" << CKind::get_kind_count()<< endl;
return 0;
}
例题4:
#include<iostream>
#include<string>
using namespace std;
class Animal //动物类
{
private:
int m_age;//年龄
public:
Animal(int age = 0)
{
m_age = age;
cout << "Animal构造完毕!" << endl;
}
~Animal()
{
cout << "Animal析构完毕!" << endl;
}
void eat()
{
cout << "吃!" << endl;
}
void show_Animal()
{
cout << "age:" << m_age<< endl;
}
};
class Carnivore :public Animal //Carnivore类 公有继承Animal
{
private:
int m_weight; //体重
public:
Carnivore(int age,int weight):Animal(age)//Carnivore 构造函数
{
m_weight = weight;
cout << "Carnivore 构造完毕!" << endl;
}
~Carnivore()//Carnivore 析构函数
{
cout << "Carnivore析构完毕!" << endl;
}
void eat()
{
cout << "食肉动物" << endl;
}
void show_Carnivore()
{
Animal::show_Animal();
cout << "weight:" << m_weight << endl;
}
};
class Cat :public Carnivore //cat类公有继承食肉动物Carnivore
{
private:
string m_colour;
public:
Cat(int age, int weight,string colour ) : //cat 构造函数
Carnivore( age, weight)
{
m_colour = colour;
cout << "cat构造完毕!" << endl;
}
~Cat()//cat析构函数
{
cout << "cat 析构完毕" << endl;
}
void eat()
{
cout << "吃鱼" << endl;
}
void show_cat()
{
Carnivore::show_Carnivore();
cout << "colour:" << m_colour << endl;
}
};
int main()
{
Cat c1(1, 10, "白色");
c1.show_cat();
c1.eat();
return 0;
}
例题5:
# include < iostream >
using namespace std;
//定义抽象类Shape
class Shape
{
public:
Shape(){}
~Shape(){}
//定义纯虚函数 GetArea()求面积
virtual double GetArea()=0;
//纯虚函数GetPerimeter()求周长
virtual double GetPerimeter()=0;
};
//定义派生类Rectangle
class Rectangle : public Shape
{
public:
Rectangle( double L, double W)
{
length= L;
width= W;
}
~Rectangle(){};
double GetArea(){
return length* width;
}
double GetPerimeter(){
return 2 * length+ 2*width;
}
private:
double length;
double width;
};
class Circle :public Shape
{//定义派生类Circle
public:
Circle(double r){
radius= r;
}
~Circle(){}
double GetArea(){
return 3.14 * radius * radius;
}
double GetPerimeter(){
return 2* 3.14*radius;
}
private:
double radius;
};
int main()
{
Shape *s;
s=new Rectangle(10, 20);
cout <<"The area of the Rectangle is:"<< s->GetArea()<< endl;
cout <<" The perimeter of the Rectangle is: "<< s->GetPerimeter( )<< endl;
delete s; //释放s所指向的内存空间
s=new Circle(10);
cout<<"The area of the Circle is: "<< s->GetArea()<< endl;
cout <<"The perimeter of the Circle is: "<< s->GetPerimeter()<< endl;
delete s;
return 0;
}