第四次实验
Posted 705397046see
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第四次实验相关的知识,希望对你有一定的参考价值。
1.
//Complex.h
class Complex{
public:
Complex(float X,FLOAT Y):x(X),y(Y){}
Complex(float X):x(X){}
void add(Complex &c);
void show();
private:
float x,y;
};
//Complex.cpp
include"Complex.h"
include
using namespace std;
void Complex::add(Complex &c){
x+=c.x;
y+=c.y;
}
void Complex::show(){
cout<<x<<"+"<<y<<"i"<<endl;
}
include
include"Complex.h"
int main(int argc,char**argv){
Complex c1(3,5);
Complex c2=4.5;
c1.add(c2);
c1.show();
return 0;
}
2.类的定义
//Graph.h
class Graph{
public:
Graph(char CH,int N):ch(CH),n(N){}//构造函数
void draw()://绘制函数
private:
char ch;
int n;
}
类的实现
//Graph.cpp
include"Graph.h"
include
using namespace std;
void Graph::draw(){
for(int y=1;y<=n;y++)//行
{
for(int i=y;i<=n,++i)//空格数
{
if(i!=n)
cout<<" ";
if(i==n)
{
for(int x=1,x<=2*y-1,++x)//符号个数
cout<<ch;
}
}
cout<<endl;
}
}
include
include"Graph.h"
int main(int argc,char**argv){
Fraph graph1(‘*‘,5);
graph1.draw();
Graph graph2(‘s‘,7);
graph2.draw();
return 0;
}
3
include
using namespace std;
class Fraction{
public:
Fraction(int t=0,int b=1);
void show();
private:
int top,bot;
}
Fraction::Traction(int t,int b);top(t),bot(b){}
void Fraction::show(){
cout<<top<<"/"<<bot<<endl;
}
int main(){
Fraction a;
a,show();
Fraction b(3,4);
b.show();
Fraction c(5);
c.show();
return 0;
}
以上是关于第四次实验的主要内容,如果未能解决你的问题,请参考以下文章