实验4

文件1,类的定义,Complex.h 
class Complex 
{
public://
    Complex(float r1,float i1);
    Complex(float a1); 
    void add(Complex c2);
    void show();
private:
    float r,i;
};
//文件2,类的实现,Complex.cpp 
#include"Complex.h"
#include<iostream>
using namespace std;
Complex::Complex(float r1,float i1) 
{
    r=r1;i=i1;
}
Complex::Complex(float r1)
{
    r=r1;i=0;
}
void Complex::add(Complex c2)
{
    r=r+c2.r;i=i+c2.i;
}
void Complex::show()
{
    cout<<r<<"+"<<i<<"i";
 } 
//文件3,主函数,Complex.cpp
#include "Complex.h"
#include <iostream>
using namespace std; 
 int main()
 {
     Complex c1(3,5);
    Complex c2=4.5;
    c1.add(c2);
    c1.show();
    return 0;
 }

 

class Graph{
public:           
    Graph (char ch, int n);     
    void draw();      
private:     
    char symbol;
    int  size;
};
#include "Graph.h" 
#include <iostream>
using namespace std;
Graph::Graph(char ch, int n): symbol(ch), size(n) {
}
void Graph::draw() {
    for (int i=1;i<=size;i++)
    {
        for(int j=size-i-1;j>=0;j--)
        {
            cout<<" ";
}
for (int j=1;j<=i*2-1;j++)
{
    cout<<symbol;
}
cout<<endl;
    }
}
#include <iostream>
#include "Graph.h"
using namespace std;


int main() {
    Graph graph1('*',5), graph2('$',7) ; 
    graph1.draw(); 
    graph2.draw(); 
    
    return 0; 
} 
#include <iostream>
using namespace std;
class Fraction{
    public:
        Fraction();
        Fraction(int a);
        Fraction(int a,int b);
        void show_Fraction();
    private:
        int top;
        int bottom;    
};

void::Fraction::show_Fraction(){
    cout<<top<<"/"<<bottom<<endl; 
}
Fraction::Fraction() {
    top=0;
    bottom=1;
}
Fraction::Fraction(int a) {
    top=a;
    bottom=1;
}
Fraction::Fraction(int a,int b) {
    top=a;
    bottom=b;
}

int main()
{
Fraction a;
Fraction b(3,4);
a.show_Fraction();
b.show_Fraction();
return 0;    
}

 

posted @ 2018-04-23 23:41  *烟雨行舟*  阅读(104)  评论(0)    收藏  举报