实验三
// 类graph的实现 #include "graph.h" #include <iostream> using namespace std; // 带参数的构造函数的实现 Graph::Graph(char ch, int n): symbol(ch), size(n) { } // 成员函数draw()的实现 // 功能:绘制size行,显示字符为symbol的指定图形样式 void Graph::draw() { int i, j, k; for (i = 1; i <= size; i++) { for (j = 1; j <= size - i; j++) cout << " "; for (k = 1; k <= 2 * i - 1; k++) cout << symbol; cout << endl; } // 补足代码 // ... }
#ifndef GRAPH_H #define GRAPH_H // 类Graph的声明 class Graph { public: Graph(char ch, int n); // 带有参数的构造函数 void draw(); // 绘制图形 private: char symbol; int size; }; #endif
#include <iostream> #include "graph.h" using namespace std;
int main() { Graph graph1('*',5); graph1.draw(); system("pause"); system("cls"); Graph graph2('$',7); graph2.draw(); system("pause"); return 0; }