赞
踩
掌握const对象的使用,类的继承与派生的使用方法。
代码:
#include<iostream> #include<string> using namespace std; class Student { public: Student(string na,int nu):num(nu) { name=na; } void Show() { cout<<"name:"<<name<<"num:"<<num<<endl; } private: string name; const int num; }; int main() { Student s1("张三",1); Student s2("李四",2); s1.Show(); s2.Show(); return 0; }
结果:
代码:
#include<iostream> using namespace std; class Date { public: Date(int y,int m,int d) { year=y; month=m; day=d; } void Display() { cout<<"year:"<<year<<"month:"<<month<<"day:"<<day<<endl; } int year,month,day; }; void setDate(Date * const p) { p->year=2021; p->month=5; p->day=20; } int main() { Date d1(2021,5,18),d2(0,0,0); d1.Display(); Date * const p=&d2; setDate(p); d2.Display(); Date * const p1=&d1; p1->year=2021; p1->month=5; p1->day=21; p1->Display(); return 0; }
结果:
代码:
#include<iostream> using namespace std; class X { public: int a; X() { a=0;b=1;c=2; } int display() { return c; } protected: int b; private: int c; }; class Y:public X { public: void fun() { int c; cout<<a<<b<<endl; c=display(); cout<<c<<endl; } }; int main() { Y y; cout<<y.a<<endl; y.fun(); return 0; }
结果:
代码:
#include<iostream> using namespace std; class X { public: int a; X() { a=0;b=1;c=2; } int display() { return c; } protected: int b; private: int c; }; class Y:protected X { public: void display2() { cout<<a; } void fun() { int c; cout<<a<<b<<endl; c=display(); cout<<c<<endl; } }; int main() { Y y; y.display2(); cout<<endl; y.fun(); return 0; }
结果:
代码:
#include<iostream> using namespace std; class X { public: int a; X() { a=0;b=1;c=2; } void displayC() { cout<<"c="<<c<<endl; } protected: int b; private: int c; }; class Y:private X { public: void displayaa() { cout<<"a="<<a<<endl; } void displayAA() { cout<<"a="<<a<<endl; } void funY() { cout<<"a="<<a<<" b="<<b<<endl; displayC(); } }; class Z:private Y { public: void funZ() { funY(); } void displayaaa() { displayaa(); } }; int main() { Y y; Z z; cout<<"y:------------------------"<<endl; y.displayAA(); y.funY(); cout<<"z:------------------------"<<endl; z.displayaaa(); z.funZ(); return 0; }
结果:
(1)声明圆类:Circle。
(2)声明基类Circle的派生类:Sphere(球)、Cylinder(圆柱)。
(3)根据题目下面两条要求定义相应的成员变量。
1)定义派生类Sphere、Cylinder的求表面积和体积的成员函数和输出函数。
2)定义Circle类、Sphere类和Cylinder类的成员变量,且均为私有的成员变量。
3)在主函数中分别输入相应的值计算Sphere、Cylinder的表面积和体积并输出结果。
代码:
#include<iostream> using namespace std; #define PI 3.14 class Circle { public: Circle(int r1,int h1) { r=r1; h=h1; } int ret() { return r; } int rett() { return h; } private: int r; int h; }; class Sphere: public Circle { public: Sphere(int r,int h):Circle(r,h){} void ss() { R=ret(); s=4*PI*R*R; cout<<"表面积为:"<<s<<endl; } void tt() { t=(4*PI*R*R*R)/3; cout<<"体积为:"<<t<<endl; } private: int R; double s; double t; }; class Cylinder: public Circle { public: Cylinder(int r,int h):Circle(r,h){} void sss() { R=ret(); H=rett(); s1=(2*PI*R*R)+(2*PI*R*H); cout<<"表面积为:"<<s1<<endl; } void ttt() { t1=PI*R*R*H; cout<<"体积为:"<<t1<<endl; } private: int H; int R; double s1; double t1; }; int main() { int a,b; cout<<"请输入圆体的半径:"<<endl; cin>>a; Sphere s(a,0); s.ss(); s.tt(); cout<<"请输入圆柱体底的的半径和高:"<<endl; cin>>a>>b; Cylinder c(a,b); c.sss(); c.ttt(); return 0; }
结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。