通过同名的add函数实现数据求和。
#include <iostream> using namespace std; int add(int a,int b){return a+b;} double add(double a,double b){return a+b;} char add(char a,int b){return a+b;} int main() { cout<<add(2,5)<<endl; cout<<add(4.6,-1.4)<<endl; cout<<add('A',32)<<endl; return 0; }给出圆的半径,圆心坐标以及待判点的坐标,以面向对象的方式判断圆和点的关系,其中圆上的点算入圆内。在圆内则输出1,否则输出0。
#include<iostream> using namespace std; class Point { public: double x,y; void getXY(double x,double y){this->x=x;this->y=y;} }; class Circle { public: double R; double x,y; void getR(double r){R = r;} void getXY(double x,double y){this->x=x;this->y=y;} bool Judge(Point aa) { double tmp = (aa.x-x)*(aa.x-x)+(aa.y-y)*(aa.y-y); return tmp<=R*R; } }; int main() { Circle c1; Point p1; c1.getR(10); c1.getXY(0,0); p1.getXY(5, 5); if (c1.Judge(p1) == true) cout << "1"; else cout << "0"; return 0; }根据所给代码和输入输出将代码补充完整。 1、设计一个程序满足以下要求: 1) 矩形类(Rectangle),有长(length)和宽(width),有计算其面积的函数Area(),并设其为虚函数。 2) 以 Rectangle 类为基类派生一个长方体类( Cuboid),增加高(height)。有与基类相同的覆盖函数 Area(),计算该长方体的表面积。 3) 另外,以 Rectangle 类为基类派生一个正方体类(Cube),其中有变量side 可选择以长(length)为边(1)或以宽(width)为边(2)。 有与基类相同的覆盖函数Area(),计算该正方体的表面积。
#include <iostream> using namespace std; class Rectangle { protected: double length, width; public: Rectangle(double len, double wid):length(len),width(wid){} virtual double Area(){ return length*width;} }; class Cuboid:public Rectangle { public: double height; Cuboid(double a,double b,double c): Rectangle(a,b){height=c;} double Area() { return height*width*2+height*length*2+width*length*2; } }; class Cube:public Rectangle { public: Cube(double l,double w,double s):Rectangle(l,w){side=s;} double Area() { if(side==1) area=length*length*6; if(side==2) area=width*width*6; } private: double side,area; }; int main() { Rectangle* rectArray[4]; rectArray[0] = new Rectangle(2,3); rectArray[1]=new Cuboid(2,3,4); rectArray[2] = new Cube(2,3,1); rectArray[3] = new Cube(2,3,2); double areaSum=0.0; for(int i=0; i < 4; i++) areaSum += rectArray[i]->Area(); cout<< areaSum << endl; for(int i=0; i < 4; i++) delete rectArray[i]; return 0; }shape类是一个表示形状的抽象类,length()为求图形周长的函数,total()则是一个通用的用以求不同形状的图形周长总和的函数。请从shape类派生圆形类(triangle)、矩形类(rectangle),并给出具体的求周长函数。通过主函数的功能测试。
#include <iostream> using namespace std; const float pi = 3.14; class shape { public: virtual float length( )=0; }; float total(shape *s[],int n) { float sum=0.0; for(int i=0;i<n;i++) sum+=s[i]->length( ); return sum; } //请定义圆形类和矩形类 class circle:public shape { public: float R; circle(float r){R=r;} float length() { return 2.0*3.141595*R; } ~circle(){} }; class rectangle:public shape { public: float L,W; rectangle(float l,float w){L = l;W = w; } float length() { return (L+W)*2; } ~rectangle(){} }; int main() { float w,h,r; cin>>w>>h; cin>>r; rectangle rec(w,h); circle cir(r); shape *s[2] = {&rec,&cir}; cout<<total(s,2); return 0; }定义复数类的加法与减法,同时能够输出复数对象,执行如下运算: Complex a(2,5),b(7,8),c(0,0); c=a+b; c=a-b;
#include<iostream> using namespace std; class Complex{ public: Complex(double r=0, double v=0):real(r),virt(v){} Complex operator+(const Complex &op); Complex operator-(const Complex &op); friend ostream& operator<<(ostream& out, Complex& a); private: double real; double virt; }; Complex Complex::operator+(const Complex &op) { Complex tep; tep.real = real + op.real; tep.virt = virt + op.virt; return tep; } Complex Complex::operator-(const Complex &op) { Complex tep; tep.real = real - op.real; tep.virt = virt - op.virt; return tep; } ostream& operator<<(ostream& out, Complex& a) { out<<a.real<<"+"<<a.virt<<"i"<<endl; return out; } int main(){ Complex a(2,5), b(7,8), c(0,0); c = a+b; cout <<c; c = a-b; cout <<c; }