标签:square 析构函数 不同类 point stream col 非成员函数 base com
/*例题8-1*//*复数的加减法*/
#include<iostream>
using namespace std;
class complex {
private:
double real; //负数实部
double imag; //实数虚部
public:
complex(double r = 0.0, double i = 0.0) :real(r), imag(i) {}
complex operator +(const complex& c2) const;
complex operator -(const complex& c2) const;
void display() const {
cout << "(" << real << "," << imag << ")" << endl;
}
};
complex complex ::operator +(const complex& c2) const {
return complex(real + c2.real, imag + c2.imag);
}
complex complex ::operator -(const complex& c2) const {
return complex(real - c2.real, imag - c2.imag);
}
int main() {
complex c1(5, 4), c2(2, 10), c3;
cout << "c1="; c1.display();
cout << "c2="; c2.display();
c3 = c2 - c1;
cout << "c3=c2-c1="; c3.display();
c3 = c2 + c1;
cout << "c3=c2+c1="; c3.display();
}
#include<iostream>
using namespace std;
class Point {
private:
float x, y;
public:
Point(float x, float y) {
this->x = x;
this->y = y;
}
void show() {
cout << "point=(" << x << "," << y << ")" << endl;
}
friend Point operator ++(Point p); //前置++
friend Point operator ++(Point &p,int); //后置++
};
Point operator++(Point p) {
p.x++;
p.y++;
return p;
}
Point operator++(Point &p,int) {
Point old = p;
p.x++;
p.y++;
return old;
}
int main() {
Point p(2, 1.5);
Point* pt = &p;
p.show();
cout << "run p++: ";
(p++).show();
cout << "run ++p: ";
(++p).show();
return 0;
}
/*附加题*//*重载 ==,判断两个字符串是否相等*/
#include<iostream>
#include<cstdlib>
#include<cassert>
using namespace std;
class My_string{
public:
char* ch;
My_string(char *ch):ch(ch){} //构造函数
int operator ==(My_string s2)const; //重载 ==
};
int My_string::operator==(My_string s2)const {
if (strcmp(ch, s2.ch) == 0)return 1;
else return 0;
}
int main() {
char *op1=new char[100], *op2=new char[100];
cout << "输入两个字符串:";
cin >>op1>>op2;
assert(strlen(op1) <= 100 && strlen(op2) <= 100); //防止字符数组越界
My_string a(op1);
My_string b(op2);
if (a.operator==(b)) {
cout << "两字符串相等" << endl;
}
else cout << "两字符串不等" << endl;
delete[] op1; //删除字符串
delete[] op2;
return 0;
}
vitual 函数类型 函数名 (形参表);
vitual 函数类型 函数名 (参数表) = 0;
[抽象类]:带有纯虚函数的类是抽象类。/抽象类不能实例化。
#include<iostream>
using namespace std;
#define pi 3.14
class Shape {
public:
virtual float getArea() = 0; //纯虚函数
virtual float getPerim() = 0; //纯虚函数
};
class Circle :public Shape {
private:
float r;
public:
Circle(float r) { this->r = r; }
float getArea() {
return (float)pi * r * r;
}
float getPerim() {
return (float)pi * 2 * r;
}
void show() {
cout << "Circle:" << endl;
cout << "r=" << r << endl;
}
};
class Rectangle :public Shape {
private:
float width, height;
public:
Rectangle(float w, float h) {
width = w;
height = h;
}
float getArea() {
return width * height;
}
float getPerim() {
return 2 * (width + height);
}
void show() {
cout << "Rectangle:" << endl;
cout << "w=" << width << " h=" << height << endl;
}
};
int main() {
Circle circle(2);
Rectangle rectangle(3, 4);
circle.show();
cout << "area = " << circle.getArea()
<< " perim = " << circle.getPerim() << endl << endl;
rectangle.show();
cout << "area = " << rectangle.getArea()
<< " perim = " << rectangle.getPerim() << endl << endl;
return 0;
}
#include<iostream>
using namespace std;
class BaseClass {
public:
virtual void fun1() { cout << "BaseClass.fun1" << endl; }
void fun2() { cout << "BaseClass.fun2" << endl; }
};
class Derived :public BaseClass {
public:
void fun1() { cout << "Derived.fun1" << endl; }
void fun2() { cout << "Derived.fun2" << endl; }
};
int main() {
Derived d;
Derived* derived = &d;
BaseClass* base = &d;
cout << "通过base指针调用:" << endl;
base->fun1(); base->fun2();
cout <<endl<< "通过derived指针调用:" << endl;
derived->fun1(); derived->fun2();
return 0;
}
在此前基础上,通过继承Rectangle类得到Square。在Shape中增加一个函数int getVertexCount() const用来获得当前顶点的个数,并使用以下几个方法
#include<iostream>
using namespace std;
#define pi 3.14
class Shape {
public:
virtual float getArea() = 0;
virtual float getPerim() = 0;
virtual int getVertexCount(Shape *s)const;
};
class Circle :public Shape {
private:
float r;
public:
int VertexCount = 0;
Circle(float r) { this->r = r; }
float getArea() {
return (float)pi * r * r;
}
float getPerim() {
return (float)pi * 2 * r;
}
void show() {
cout << "Circle:" << endl;
cout << "r=" << r << endl;
}
};
class Rectangle :public Shape {
private:
float width, height;
public:
int VertexCount = 4;
Rectangle(float w, float h) {
width = w;
height = h;
}
float getArea() {
return width * height;
}
float getPerim() {
return 2 * (width + height);
}
void show() {
cout << "Rectangle:" << endl;
cout << "w=" << width << " h=" << height << endl;
}
};
class Square :public Rectangle {
public:
int VertexCount = 4;
Square(float len) :Rectangle(len, len) { length = len; }
float getArea() {
return length * length;
}
float getPerim() {
return 4 * length;
}
void show() {
cout << "Square:" << endl;
cout << "length=" << length << endl;
}
private:
float length;
};
//使用dynamic_cast的方式实现求顶点函数
int Shape::getVertexCount(Shape *s)const {
Rectangle *r=dynamic_cast<Rectangle*>(s);
Circle* c = dynamic_cast<Circle*>(s);
Square* sq = dynamic_cast<Square*>(s);
if (r) //如果可以转换
return r->VertexCount;
if (c)
return c->VertexCount ;
if (sq)
return sq->VertexCount;
}
int main() {
Circle circle(2);
Rectangle rectangle(3, 4);
Square square(5);
circle.show(); //圆
cout << "area = " << circle.getArea()
<< " perim = " << circle.getPerim() << endl ;
cout<<"顶点个数:"<<circle.getVertexCount(&circle)<< endl<<endl;
rectangle.show(); //长方形
cout << "area = " << rectangle.getArea()
<< " perim = " << rectangle.getPerim() << endl;
cout <<"顶点个数"<< rectangle.getVertexCount(&rectangle) << endl<<endl;
square.show(); //正方形
cout << "area = " << square.getArea()
<< " perim = " << square.getPerim() << endl;
cout << "顶点个数" << square.getVertexCount(&square) << endl;
return 0;
}
#include<iostream>
#include<cstring>
#include<typeinfo>
using namespace std;
#define pi 3.14
class Shape {
public:
virtual float getArea() = 0;
virtual float getPerim() = 0;
int getVertexCount(Shape&s)const;
};
class Circle :public Shape {
private:
float r;
public:
Circle(float r) { this->r = r; }
float getArea() {
return (float)pi * r * r;
}
float getPerim() {
return (float)pi * 2 * r;
}
void show() {
cout << "Circle:" << endl;
cout << "r=" << r << endl;
}
};
class Rectangle :public Shape {
private:
float width, height;
public:
Rectangle(float w, float h) {
width = w;
height = h;
}
float getArea() {
return width * height;
}
float getPerim() {
return 2 * (width + height);
}
void show() {
cout << "Rectangle:" << endl;
cout << "w=" << width << " h=" << height << endl;
}
};
class Square :public Rectangle {
public:
Square(float len) :Rectangle(len, len) { length = len; }
float getArea() {
return length * length;
}
float getPerim() {
return 4 * length;
}
void show() {
cout << "Square:" << endl;
cout << "length=" << length << endl;
}
private:
float length;
};
int Shape::getVertexCount(Shape& s)const {
if (!strcmp(typeid(s).name(), "class Circle"))return 0;
else if (!strcmp(typeid(s).name(), "class Rectangle") || !strcmp(typeid(s).name(), "class Square"))return 4;
else return 1;
}
int main() {
Circle circle(2);
Rectangle rectangle(3, 4);
Square square(5);
circle.show(); //圆
cout << "area = " << circle.getArea()
<< " perim = " << circle.getPerim() << endl;
cout << "顶点个数:" << circle.getVertexCount(circle) << endl << endl;
rectangle.show(); //长方形
cout << "area = " << rectangle.getArea()
<< " perim = " << rectangle.getPerim() << endl;
cout << "顶点个数" << rectangle.getVertexCount(rectangle) << endl << endl;
square.show(); //正方形
cout << "area = " << square.getArea()
<< " perim = " << square.getPerim() << endl;
cout << "顶点个数" << square.getVertexCount(square) << endl;
return 0;
}
#include<iostream>
using namespace std;
#define pi 3.14
class Shape {
public:
virtual float getArea() = 0;
virtual float getPerim() = 0;
virtual int getVertexCount(Shape* s)const = 0;
};
class Circle :public Shape {
private:
float r;
public:
int VertexCount = 0;
Circle(float r) { this->r = r; }
float getArea() {
return (float)pi * r * r;
}
float getPerim() {
return (float)pi * 2 * r;
}
void show() {
cout << "Circle:" << endl;
cout << "r=" << r << endl;
}
int getVertexCount(Shape* s)const {
return VertexCount;
}
};
class Rectangle :public Shape {
private:
float width, height;
public:
int VertexCount = 4;
Rectangle(float w, float h) {
width = w;
height = h;
}
float getArea() {
return width * height;
}
float getPerim() {
return 2 * (width + height);
}
void show() {
cout << "Rectangle:" << endl;
cout << "w=" << width << " h=" << height << endl;
}
int getVertexCount(Shape* s)const {
return VertexCount;
}
};
class Square :public Rectangle {
public:
int VertexCount = 4;
Square(float len) :Rectangle(len, len) { length = len; }
float getArea() {
return length * length;
}
float getPerim() {
return 4 * length;
}
void show() {
cout << "Square:" << endl;
cout << "length=" << length << endl;
}
int getVertexCount(Shape* s)const {
return VertexCount;
}
private:
float length;
};
int main() {
Circle circle(2);
Rectangle rectangle(3, 4);
Square square(5);
circle.show(); //圆
cout << "area = " << circle.getArea()
<< " perim = " << circle.getPerim() << endl;
cout << "顶点个数:" << circle.getVertexCount(&circle) << endl << endl;
rectangle.show(); //长方形
cout << "area = " << rectangle.getArea()
<< " perim = " << rectangle.getPerim() << endl;
cout << "顶点个数" << rectangle.getVertexCount(&rectangle) << endl << endl;
square.show(); //正方形
cout << "area = " << square.getArea()
<< " perim = " << square.getPerim() << endl;
cout << "顶点个数" << square.getVertexCount(&square) << endl;
return 0;
}
标签:square 析构函数 不同类 point stream col 非成员函数 base com
原文地址:https://www.cnblogs.com/TSmeredithH/p/11749349.html