哪个网络公司做网站好,东莞人才市场最新招聘信息,wordpress主题应该怎么添加,Python做网站难不难作业
实现一个图形类#xff08;Shape#xff09;#xff0c;包含受保护成员属性#xff1a;周长、面积#xff0c; 公共成员函数#xff1a;特殊成员函数书写
定义一个圆形类#xff08;Circle#xff09;#xff0c;继承自图形类#xff0c;包含私有属性#xf…作业
实现一个图形类Shape包含受保护成员属性周长、面积 公共成员函数特殊成员函数书写
定义一个圆形类Circle继承自图形类包含私有属性半径 公共成员函数特殊成员函数、以及获取周长、获取面积函数
定义一个矩形类Rect继承自图形类包含私有属性长度、宽度 公共成员函数特殊成员函数、以及获取周长、获取面积函数
在主函数中分别实例化圆形类对象以及矩形类对象并测试相关的成员函数。 03hmwk.h:
#ifndef __03HMWK_H__
#define __03HMWK_H__#include iostream
#define PI 3.14
using namespace std;class Shape{
protected:double circumference;double size;
public://无参构造Shape();//有参构造Shape(double c,double s);//析构~Shape();//拷贝构造Shape(const Shape other);
// //移动构造
// Shape(Shape other);//拷贝赋值Shape operator(const Shape other);
// //移动赋值
// Shape operator(Shape other);
};class Circle:public Shape{
private:double radius;
public://无参构造Circle();//有参构造Circle(double r);//析构~Circle();//拷贝构造Circle(const Circle other);
// //移动构造
// Circle(Circle other);//拷贝赋值Circle operator(const Circle other);
// //移动赋值
// Circle operator(Circle other);//获取周长double get_C();//获取面积double get_S();
};class Rectangle:public Shape{
private:double length;double width;
public://无参构造Rectangle();//有参构造Rectangle(double l,double w);//析构~Rectangle();//拷贝构造Rectangle(const Rectangle other);
// //移动构造
// Rectangle(Rectangle other);//拷贝赋值Rectangle operator(const Rectangle other);
// //移动赋值
// Rectangle operator(Rectangle other);//获取周长double get_C();//获取面积double get_S();
};#endif // 03HMWK_H03hmwk.cpp:
#include 03hmwk.h
//无参构造
Shape::Shape():circumference(0),size(0){coutShape::无参构造endl;
}
//有参构造
Shape::Shape(double c,double s):circumference(c),size(s){coutShape::有参构造endl;
}
//析构
Shape::~Shape(){coutShape::析构endl;
}
//拷贝构造
Shape::Shape(const Shape other):circumference(other.circumference),size(other.size){coutShape::拷贝构造endl;
}
移动构造
//Shape::Shape(Shape other):circumference(other.circumference),size(other.size){
// coutShape::移动构造endl;
//}
//拷贝赋值
Shape Shape::operator(const Shape other){if(this ! other){circumference other.circumference;size other.size;}coutShape::拷贝赋值endl;return *this;
}
移动赋值
//Shape Shape::operator(Shape other){
// circumference other.circumference;
// size other.size;
// coutShape::移动赋值endl;
// return *this;
//}/**********************************圆*************************************************///无参构造
Circle::Circle():Shape(0,0),radius(0){coutCircle::无参构造endl;
}
//有参构造
Circle::Circle(double r):Shape(2*PI*r,PI*r*r),radius(r){coutCircle::有参构造endl;
}
//析构
Circle::~Circle(){coutCircle::析构endl;
}
//拷贝构造
Circle::Circle(const Circle other):Shape(other),radius(other.radius){coutCircle::拷贝构造endl;
}
移动构造
//Circle::Circle(Circle other):Shape(other),radius(other.radius){
// coutCircle::移动构造endl;
//}
//拷贝赋值
Circle Circle::operator(const Circle other){if(this ! other){Shape::operator(other);radius other.radius;}coutCircle::拷贝赋值endl;return *this;
}
移动赋值
//Circle Circle::operator(Circle other){
// (Shape)other Shape(other);
// radius other.radius;
// coutCircle::移动赋值endl;
// return *this;
//}
//获取周长
double Circle::get_C(){return circumference;
}
//获取面积
double Circle::get_S(){return size;
}/***********************************矩形************************************************///无参构造
Rectangle::Rectangle():Shape(0,0),length(0),width(0){coutRectangle::无参构造endl;
}
//有参构造
Rectangle::Rectangle(double l,double w):Shape(2*(lw),l*w),length(l),width(w){coutRectangle::有参构造endl;
}
//析构
Rectangle::~Rectangle(){coutRectangle::析构endl;
}
//拷贝构造
Rectangle::Rectangle(const Rectangle other):Shape(other),length(other.length),width(other.width){coutRectangle::拷贝构造endl;
}
移动构造
//Rectangle::Rectangle(Rectangle other):Shape(other),length(other.length),width(other.width){
// coutRectangle::移动构造endl;
//}
//拷贝赋值
Rectangle Rectangle::operator(const Rectangle other){if(this ! other){Shape::operator(other);length other.length;width other.width;}coutRectangle::拷贝赋值endl;return *this;
}
移动赋值
//Rectangle Rectangle::operator(Rectangle other){
// (Shape)other Shape(other);
// length other.length;
// width other.width;
// coutRectangle::移动赋值endl;
// return *this;
//}
//获取周长
double Rectangle::get_C(){return circumference;
}
//获取面积
double Rectangle::get_S(){return size;
}main.cpp:
#include 03hmwk.hint main()
{Circle c1(5);coutc1.C c1.get_C()endl;coutc1.S c1.get_S()endl;Rectangle r1(5,4);coutr1.C r1.get_C()endl;coutr1.S r1.get_S()endl;Circle c2 c1;coutc2.C c2.get_C()endl;coutc2.S c2.get_S()endl;Rectangle r2 r1;coutr2.C r2.get_C()endl;coutr2.S r2.get_S()endl;Circle c3;c3 c2;coutc3.C c3.get_C()endl;coutc3.S c3.get_S()endl;Rectangle r3;r3 r2;coutr3.C r3.get_C()endl;coutr3.S r3.get_S()endl;
// Circle c4 move(c3);
// coutc4.C c4.get_C()endl;
// coutc4.S c4.get_S()endl;
// Rectangle r4 move(r3);
// coutr4.C r4.get_C()endl;
// coutr4.S r4.get_S()endl;
// Circle c5;
// c5 move(c4);
// coutc5.C c5.get_C()endl;
// coutc5.S c5.get_S()endl;
// Rectangle r5;
// r5 move(r4);
// coutr5.C r5.get_C()endl;
// coutr5.S r5.get_S()endl;return 0;
}效果图 一、静态成员static
程序员有时在实例化对象时想让某个成员或某几个成员独立于类对象而存在但是又属于类对象的属性不占用类对象的空间那么此时我们可以将这些成员设置成静态成员相当于在类体内定义一个全局的成员。静态成员分为静态成员变量和静态成员函数。
1.1 静态成员变量
定义格式在定义成员变量时在前面加关键字static那么该变量就是静态成员变量静态成员变量一般声明成public权限并且需要在类内声明类外定义定义时如果不初始化默认为0,静态成员变量不依附于类对象而存在不占用类对象的空间在编译时系统在静态区为其分配空间静态成员变量也是每个类对象拥有的属性一个对象对其更改所有对象的该属性都会更改使用方式每个类对象都可以使用成员运算符进行调用也可以通过类名加作用域限定符直接调用静态成员变量从功能上来说相当于全局变量但是相比于全局变量更能体现类的封装性
#include iostreamusing namespace std;
class Stu
{
private:int age;
public:static int score; //在类内声明Stu() {}
};//在类外定义一下静态成员变量
int Stu::score 100;int main()
{Stu::score 50; //通过 类名直接访问类中的静态成员变量Stu s;coutscore s.scoreendl;coutsizeof s sizeof (s)endl; //4字节静态成员变量不占用类对象的空间Stu s1;coutscore s1.scoreendl; //100Stu s2;s2.score 90; //一个对象的该值进行改变所有对象的该值都跟着改变coutscore s.scoreendl;coutscore s1.scoreendl;return 0;
}
1.2 静态成员函数
定义格式在定义成员函数前加关键static那么该函数就是静态成员函数静态成员函数相当于在类体中定义一个全局函数但是只能是该类对象或者该类进行调用静态成员函数的调用可以使用类对象通过成员运算符进行调用也可以使用类名直接进行调用静态成员函数中没有this指针静态成员函数不依赖于类对象而存在所以也可以不实例化对象直接使用类名进行调用在静态成员函数中只能使用静态成员变量不能使用非静态成员变量
#include iostreamusing namespace std;
class Stu
{
private:int age;
public:static int score; //在类内声明Stu() {}Stu(int a):age(a) {}//该函数是静态成员函数static void show(){//coutage ageendl; //在静态成员函数中不能使用非静态成员变量coutscore scoreendl; //可以使用静态成员变量}//静态成员函数和同名的非静态成员函数不构成重载关系原因是作用域不同
// void show()
// {
// coutage ageendl; //在静态成员函数中不能使用非静态成员变量
// coutscore scoreendl; //可以使用静态成员变量
// }
};//在类外定义一下静态成员变量
int Stu::score 100;int main()
{Stu::score 50; //通过 类名直接访问类中的静态成员变量Stu s;coutscore s.scoreendl;coutsizeof s sizeof (s)endl; //4字节静态成员变量不占用类对象的空间Stu s1;coutscore s1.scoreendl; //100Stu s2;s2.score 90; //一个对象的该值进行改变所有对象的该值都跟着改变coutscore s.scoreendl;coutscore s1.scoreendl;cout********************************************************endl;Stu s3(18);s3.show(); //通过类对象调用静态成员函数Stu::show(); //通过函数名直接调用静态成员函数return 0;
}
二、继承inherit
2.1 继承的概念
1 所谓继承就是在一个类的基础上去定义另一个新类的过程叫做继承
2 作用
继承能够提高代码的复用性继承是实现多态的必要条件
2.2 继承格式
class 子类名:继承方式 父类名
{//子类拓展的成员
}
继承方式有三种public、protected、private子类也叫派生类
父类也叫基类
2.3 继承方式对类中成员的权限的影响
父类中 public|protected|private|不能访问 public|protected|private|不能访问 public|protected|private|不能访问
继承方式 public protected private
子类中 public|protected|不能访问|不能访问 protected|protected|不能访问|不能访问 private|private|不能访问|不能访问
总结
所谓继承方式其实就是父类中的成员的访问权限在子类中的最高权限如果继承过程中没有加访问权限默认是私有继承常用的继承方式是public在所有继承方式中都不能在子类中访问父类的私有成员以及父类不能访问的成员
2.4 子类会继承父类中的所有成员
1 子类会继承父类中的所有成员包括私有成员只是私有成员不能访问而已
2 父类和子类是不同的类
3 为了对子类从父类中继承下来成员进行初始化工作需要在子类的构造函数初始化列表中显性调用父类的有参构造完成否则系统会自动调用父类的无参构造此时如果父类没有无参构造则系统报错
4 在这个过程中虽然调用了父类的构造函数但是并没有实例化一个父类对象
#include iostreamusing namespace std;class Person
{
public:string name;
protected:int value;
private:double money; //私房钱public:Person() {coutPerson::无参构造endl;}Person(string n, int v, double m):name(n),value(v),money(m) {coutPerson::有参构造endl;}void show(){coutPerson::name nameendl; //自己类中的公有成员自己类内可以访问coutPerson::value valueendl; //自己类中的受保护成员自己类内可以访问coutPerson::money moneyendl; //自己类中的私有成员自己类内可以访问}
};//定义一个员工类继承自人类
class Worker:public Person //公有继承
/*class Worker:protected Person*/ //受保护继承
//class Worker:private Person
{
private:double salary; //工资public:Worker() {coutWorker::无参构造endl;}//需要在子类的构造函数中显性调用父类的有参构造完成对子类从父类中继承下来的成员的初始化Worker(string n, int v, double m, double s):Person(n,v,m), salary(s){
// name n;
// value v;
// money m;coutWorker::有参构造endl;}void display(){coutWorker::name nameendl; //继承下来的公有成员子类中还是公有成员子类内可以访问coutWorker::value valueendl; //继承下来的受保护成员子类中还是受保护成员子类内可以访问//coutWorker::money moneyendl; //继承下来的父类私有成员子类中不可访问coutWorker::salary salaryendl; //自己的私有成员类内可以访问}
};int main()
{//cout sizeof(Worker) endl;// Worker w;// coutw.name w.nameendl; //继承下来的公有成员子类中也是公有的类外可以被访问// coutw.value w.valueendl; //继承下来的受保护成员子类中也是受保护的子类外不能被访问
// coutw.money w.moneyendl; //继承下来的私有成员子类中不能被访问类外不能被访问
// coutw.salary w.salaryendl; //子类的私有成员子类外无法访问Worker w1(zhangpp, 520, 1, 100);w1.display();w1.show();return 0;
}
2.5 类的关系模型
1 has - a模型
包含关系一个类中包含另一个类的成员对象
2 use - a模型
友元关系将某个类设置成友元那么另一个类就可以使用自己的所有权限下的成员
3 is - a模型
继承关系并且is-a模型是特殊的has-a模型 2.6 类的继承步骤
1 全盘吸收父类
2 改造父类继承方式、通过using关键字改变
3 拓展新成员
#include iostreamusing namespace std;class Person
{
public:string name;
protected:int salary;
private:int money;
public:Person() {}
};//定义学生类继承自Person
class Stu:public Person
{
protected:using Person::name; //将继承的name的权限更改成受保护权限public:using Person::salary; //将继承的salary权限更改成公共权限//using Person::money; //父类的私有成员不能进行改造double score; //拓展新成员public:Stu() {}};int main()
{Stu s;//s.name hello;//s.salary 1000;return 0;
}