#include <iostream> using namespace std; int main() { int i, j, sum = 0; for (i = 0; i <= 20; i++) { for (j = 0; j <= 50; j++) { if (100 - 5 * i - 2 * j >= 0) { sum++; cout << "1分:" << (100 - 5 * i - 2 * j) << "\t2分:"; cout << j << "\t5分:" << i << endl; } } } cout << "sum is "<< sum << endl; return 0; }
#include<iostream> using namespace std; int main() { void change(int &, int &); int a, b; cin >> a >> b; if (a > b) change(a, b); cout << a <<" "<< b << endl; return 0; } void change(int &a1, int &b1) { int t; t = a1; a1 = b1; b1 = t; }
用类外定义类内成员函数,实现年月日显示。
#include<iostream> using namespace std; class Date { public : void print(); void setday(int d); void setmonth(int m); void setyear(int y); private: int day, month, year; }; void Date::print() { cout << "日期为" << year << "年"; cout << month << "月" << day<<"日"<< endl; } void Date::setday(int d) { day = d; } void Date::setmonth(int m) { month = m; } void Date::setyear(int y) { year = y; } int main() { Date thisday; thisday.setday(3); thisday.setmonth(6); thisday.setyear(2021); thisday.print(); return 0; }
构建一个类book,其中含有两个私有数据成员qu和price,将qu初始化为1~5,将price初始化为qu的10倍,建立一个有5个元素的数组元素,显示每个对象数组元素的qu*price值。
#include <iostream> using namespace std; class book { private: int qu, price; public: book(int a, int b) { qu = a; price = b; } void show_money() { cout << qu * price << endl; } }; int main() { book ob[5] = { book(1,10), book(2,20), book(3,30), book(4,40), book(5,50) }; for (int i = 0; i < 5; i++) ob[i].show_money() ; return 0; }
建立基类area_cl的两个派生类rectangle与iosceles 让每一个派生类都包含一个函数area ()分别用来返回与三角形的面积。用构造函数对height与width进行初始化。
写出主程序,矩形高10.0 宽5.0,三角形宽长6.0 高4.0;
通过基类访问虚函数的方法即运行时的多态分别求面积
#include<iostream> using namespace std; class area_c1{ protected: double height; double width; public : area_c1(double r, double s) { area_c1::height = r; area_c1::width = s; } virtual double area() = 0; }; class rectangle :public area_c1{ public : rectangle(double r, double s):area_c1(r, s) {} double area(){ return height * width; } }; class isosceles : public area_c1 { public : isosceles (double r, double s) :area_c1(r, s) {} double area(){ return height * width/2; } }; int main() { area_c1 *ob; rectangle ob1(10.0, 5.0); isosceles ob2(4.0, 6.0); ob = &ob1; cout<< "矩形面积" << ob->area() << endl; ob = &ob2; cout << "三角形面积" << ob->area() << endl; }
编写一个学生和教师数据输入和显示数据程序,学生数据有编号,姓名,班号,成绩,教师有编号,姓名,职称,和部门。要求将编号,姓名输入和显示设计成一个类person ,并作为学生数据操作类 student 和教师数据操作类teacher的基类。
#include <iostream> using namespace std; class person { private: int no; char name[10]; public : void shuru() { cout << "编号"; cin >> no; cout << "姓名"; cin >> name; } void print() { cout << "编号" << no << endl; cout << "姓名" << name << endl; } }; class student : public person { private: int banhao; int score; public: void shuru() { person::shuru(); cout << "班号"; cin >> banhao; cout << "分数"; cin >> score; } void print() { person::print(); cout << "班号" << banhao << endl; cout << "分数" << score <<endl; } }; class teacher :public person { private: char zhicheng[10]; char apart[10]; public : void shuru() { person::shuru(); cout << "职称"; cin >> zhicheng; cout << "部门"; cin >> apart; } void print() { person::print(); cout << "职称" << zhicheng<< endl; cout << "部门" << apart << endl; } }; int main() { student s1; teacher t1; cout << "输入学生数据" << endl; s1.shuru(); cout << "\n输入老师数据" << endl; t1.shuru(); cout << "\n显示学生数据" << endl; s1.print(); cout << "\n显示老师数据" << endl; t1.print(); return 0; }
写一个程序,定义抽象基类Container,由它派生出三个派生类:Spere(球体),Cylinder(圆柱体),Cude (正方体),用虚函数分别计算几种图形的表面积和体积。
#include <iostream> using namespace std; class Container { protected : double radiux; double height; public : Container(double ra) { Container::radiux = ra; } double print_ra() { return radiux; } double print_he() { return height; } virtual double s_area() = 0; virtual double volume() = 0; }; class sphere :public Container{ public: sphere(double ra) : Container(ra) { } double s_area() { return 4 * 3.14*radiux* radiux; } double volume() { return 3.14*radiux* radiux*radiux*4/3; } }; class cylinder :public Container { public: cylinder(double ra,double he) : Container(ra) { height = he; } double s_area() { return 2 * 3.14*radiux* (radiux+height); } double volume() { return 3.14*radiux* radiux*height; } }; class cube :public Container { public: cube(double ra) : Container(ra) { } double s_area() { return 6*radiux* radiux; } double volume() { return radiux* radiux*radiux; } }; int main() { Container *ptr; sphere obj1(8); cylinder obj2(3,5); cube obj3(5); ptr = &obj1; cout << "球的半径为" << ptr->print_ra ()<< endl; cout << "球的表面积为" << ptr->s_area ()<< endl; cout << "球的体积为" << ptr->volume ()<< endl; ptr = &obj2; cout << "圆柱的半径为" << ptr->print_ra() << endl; cout << "圆柱的高为" << ptr->print_he ()<< endl; cout << "圆柱的表面积为" << ptr->s_area() << endl; cout << "圆柱的体积为" << ptr->volume() << endl; ptr = &obj3; cout << "正方体的半径为" << ptr->print_ra() << endl; cout << "正方体的表面积为" << ptr->s_area() << endl; cout << "正方体的体积为" << ptr->volume() << endl; return 0; }