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

实验4 类与对象2

时间:2018-04-24 00:23:16      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:样式   efi   private   clu   turn   info   运行   参数   屏幕   

实验结论

1.实验内容2

(2)文件源码

  • graph.h
#ifndef GRAPH_H
#define GRAPH_H

// 类Graph的声明
class Graph {
public:
    Graph(char ch, int n);   // 带有参数的构造函数
    void draw();     // 绘制图形
private:
    char symbol;
    int size;
};
#endif
  • graph.cpp
// 类graph的实现

#include "graph.h"
#include <iostream>
using namespace std;

// 带参数的构造函数的实现
Graph::Graph(char ch, int n): symbol(ch), size(n) {
}
// 成员函数draw()的实现
// 功能:绘制size行,显示字符为symbol的指定图形样式
//       size和symbol是类Graph的私有成员数据
void Graph::draw() {
    for (int i=1;i<size+1;i++){        //控制行数
        for (int j=0;j<size-i;j++){    //输出每行符号前的空格
            cout<<‘ ‘;
        }
        for (int k=0;k<2*i-1;k++){     //输出图案符号
            cout<<symbol;
        }
        cout<<endl;                 //换至下一行  
        // 补足代码,「实验4.pdf」文档中展示的图形样式
    }
}
  • main.cpp
#include <iostream>
#include "graph.h"
using namespace std;
int main() {
    Graph graph1(‘*‘,5), graph2(‘$‘,7) ;  // 定义Graph类对象graph1, graph2
    graph1.draw(); // 通过对象graph1调用公共接口draw()在屏幕上绘制图形
    graph2.draw(); // 通过对象graph2调用公共接口draw()在屏幕上绘制图形
    
    return 0;
}

(3) Xcode 下运行结果截图:
技术分享图片

未完待续……

实验4 类与对象2

标签:样式   efi   private   clu   turn   info   运行   参数   屏幕   

原文地址:https://www.cnblogs.com/jiahewang/p/8922596.html

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