码迷,mamicode.com
首页 > 其他好文 > 详细

实验四

时间:2018-04-24 00:20:08      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:isp   ret   构造函数   错误   类对象   http   private   思路   return   

#include<iostream>
using namespace std;
// 类Graph的声明 
class Graph {
    public:
        Graph(char ch, int n);   // 带有参数的构造函数 
        void draw();    // 绘制图形 
    private:
        char symbol;
        int size;
};
// 带参数的构造函数的实现 
Graph::Graph(char ch, int n): symbol(ch), size(n) {}
// 成员函数draw()的实现
// 功能:绘制size行,显示字符为symbol的指定图形样式 
//       size和symbol是类Graph的私有成员数据 
void Graph::draw() 
{
    int i,j;
    for(i=0;i<=size;i++)
    {
    for(j=0;j<=size-i;j++)
    cout<<" ";  
    for(int j=0;j<2*i-1;j++)
            cout<<symbol;
        cout<<endl;
}
}
int main() {
    Graph graph1(‘*‘,5), graph2(‘$‘,7) ;  // 定义Graph类对象graph1, graph2 
    graph1.draw(); // 通过对象graph1调用公共接口draw()在屏幕上绘制图形 
    graph2.draw(); // 通过对象graph2调用公共接口draw()在屏幕上绘制图形
    
    return 0; 
} 

技术分享图片

#include<iostream>
using namespace std;
class Fraction
{
    public:
        Fraction():top(0),bottom(1) {};
        Fraction(int a,int b):top(a),bottom(b) {};
        Fraction(int c):top(c),bottom(1){};
        void add(Fraction p);     
        void sub(Fraction p);      
        void mul(Fraction p);      
        void div(Fraction p);
        void display()
        {
            cout<<top<<"/"<<bottom<<endl; 
        };  
        void op();    
        private:
            int top;
            int bottom;
};
void Fraction:: add(Fraction p)
{
    top=top*p.bottom+p.top*bottom;
    bottom=bottom*p.bottom;
    void display();
    
}
void Fraction::sub(Fraction p)
{
top=top*p.bottom-p.top*bottom;
    bottom=bottom*p.bottom;
    void display(); 
    
}
void Fraction::mul(Fraction p)
{
    top=top*p.top;
    bottom=bottom*p.bottom;
    void display();
    
}
void Fraction::div(Fraction p)
{
    top=top*p.bottom;
    bottom=p.top*bottom;
    void display();
    
}
void Fraction::op()
{
    cout<<"输入分子"<<endl;
    cin>>top;
    cout<<"输入分母"<<endl;
    cin>>bottom;
}
int main()
{
Fraction a;
Fraction b(3,4);
Fraction c(5);
a.display();
b.display();
c.display();
return 0;
}

技术分享图片

第一个代码还蛮简单的,第二个是真的有点懵,交作业前不习惯参考别人的代码,交上去之后我会参考一下别的同学的思路再找一下我第二个代码的错误,然后完善一下我代码的功能

实验四

标签:isp   ret   构造函数   错误   类对象   http   private   思路   return   

原文地址:https://www.cnblogs.com/Nicholastwo/p/8922647.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!