ouc专硕上级题目
2016-03-19 00:00
225 查看
摘要: 环境:vs2010;直接复制到vc6可能报错,请自行修改。
######2015-1 任意输入若干整数,输出这些数的均值和方差
######2014-2 行程编码
######2014-3 访问一个ftp服务器的完整链接字符串
######2015-2 IPV4地址分析类
2012-3 求一个整数的三件事
模拟定义存折类,并派生出信用卡类
对象成员、友元、常类型的应用
运算符重载
[b]虚函数与多态性
专硕真题
######2014-1 任意输入三个整数,输出三个值的均值和方差#include<iostream> using namespace std; int main() { int n1,n2,n3,sum; double ave,std; cout<<"请输入三个整数:"<<endl; cin>>n1>>n2>>n3; sum = n1+n2+n3; ave = (double)sum/3; //强制类型转换将sum从int型转换成double类型 #include<cmath>//或者 #include<math.h> std = ((n1-ave)*(n1-ave)+(n2-ave)*(n2-ave)+(n3-ave)*(n3-ave))/3; //或者std = (pow((n1-ave),2)+pow((n2-ave),2)+pow((n3-ave),2))/3; //pow(x,y)计算x的y次方,返回值为浮点型 cout<<n1<<","<<n2<<","<<n3<<"的均值为:"<<ave<<endl; cout<<n1<<","<<n2<<","<<n3<<"的方差为:"<<std<<endl; system("pause");//请按任意键继续... return 0; }
请输入三个整数: 1 2 3 1,2,3的均值为:2 1,2,3的方差为:0.666667 请按任意键继续. . .
######2015-1 任意输入若干整数,输出这些数的均值和方差
#include<iostream> using namespace std; int main() { int n,*p,i; double ave,std,sum=0,ssum=0; cout<<"请输入整数个数:"; cin>>n; p = new int ; cout<<"请输入"<<n<<"个整数:"<<endl; for(i=0;i<n;i++) { cin>> p[i]; sum += p[i]; } ave = sum/n; #include<cmath> for(i=0;i<n;i++) { ssum += pow((p[i]-ave),2); } std = sqrt(ssum/n); //sqrt(x)是开平方函数, //pow(x,y)计算x的y次方, //他们返回值均为浮点型; delete []p;//释放动态内存空间 cout<<"他们的平均值为:"<<ave<<endl; cout<<"他们的均方差为:"<<std<<endl; return 0; }
请输入整数个数:3 请输入3个整数: 1 2 3 他们的平均值为:2 他们的均方差为:0.816497 请按任意键继续. . .
######2014-2 行程编码
#include<iostream> #include<string> using namespace std; /*2014-2 行程编码是数据压缩的一种编码方式 *其基本原理:用一个符号值和串长代替具有相同值的连续符号, *如:输入:AAAADDEEEEEEGGFFFFFFF * 输出:A4,D2,E6,G2,F7 */ int main() { string str,s=""; char c;//标志位 int i,j; cout<<"请输入字符串:"; cin>>str; cout<<"压缩后字符串:"; for(i=0;i<str.length();i++) { c = str[i]; j=0; //计数 while(i<str.length()&&c==str[i]) { j++; i++;//和外层for循环共用一个变量 } i--;//while循环条件为false时多走了一步 cout<<c<<j<<','; } cout<<endl; return 0; }
请输入字符串:AAAADDEEEEEEGGFFFFFFF 压缩后字符串:A4,D2,E6,G2,F7, 请按任意键继续. . .
######2014-3 访问一个ftp服务器的完整链接字符串
#include <iostream> #include <string> using namespace std; /* 2014-3 访问一个ftp服务器的完整链接字符串为 FTP://test:12345@192.168.1.1:2121 设计一个字符串分析类,功能包括: 1.构造函数:参数包含ftp链接字符串。 2.获取ftp服务器IP的接口。 3.获取登录服务器ftp服务器的用户名及密码的接口。 4.获取ftp服务器的服务端口。 编写主程序测试类的有效性。 */ class FTPString { public: FTPString(char *s); ~FTPString(){delete ch;}; string getIp(); string getInfo(); string getPort(); private: int findChar(char c);//正向查找字符 int rfindChar(char );//反向查找字符 string ftpstr; int len; char *ch; }; FTPString::FTPString(char *s) { ftpstr = s; len = strlen(s);//字符串长度(带结尾) } int FTPString::findChar(char c) { int i= 0; while(ftpstr[i]!=c && i>=0) { i++; } return i; } int FTPString::rfindChar(char c) { int i= len-1; //不带结尾的长度 while(ftpstr[i]!=c && i>=0) { i--; } return len-i; } string FTPString::getIp() { int i,l,l2,l3; l2 = findChar('@')+1; l3 = rfindChar(':'); l = len-l2-l3;//ip的长度 ch =new char[l+1]; ch[l]='\0';//结尾1个字符 for(i=0;i<l;i++) { ch[i]=ftpstr[l2+i]; } return ch; } string FTPString::getInfo() { int i,l,l2,l3; l2 = rfindChar('/')-1; l3 = rfindChar('@'); l = l2-l3; //用户名及密码的长度 ch =new char[l+1]; ch[l]='\0'; for(i=0;i<l;i++) { ch[i]=ftpstr[len-l2+i]; } return ch; } string FTPString::getPort() { int i,l; l = rfindChar(':')-1; //端口号的长度 ch =new char[l+1]; ch[l]='\0'; for(i=0;i<l;i++) { ch[i]=ftpstr[len-l+i]; } return ch; } int main( ) { char str[] = "FTP://test:12345@192.168.1.1:2121"; FTPString fs(str); //声明并初始化对象 cout << "ftp服务器字符串: " << str <<endl; cout << "ftp服务器IP地址: " << fs.getIp() <<endl; cout << "ftp服务器的服务端口: " << fs.getPort() <<endl; cout << "ftp服务器用户名密码: " << fs.getInfo() <<endl; return 0; }
ftp服务器字符串: FTP://test:12345@192.168.1.1:2121 ftp服务器IP地址: 192.168.1.1 ftp服务器的服务端口: 2121 ftp服务器用户名密码: test:12345 请按任意键继续. . .
######2015-2 IPV4地址分析类
#include <iostream> #include <string> using namespace std; /* 2015-2 写一个IPV4的地址如"202.112.17.33","192.168.1.1"等, 设计一个IPV4地址分析类,功能包括: 1)构造函数:参数包含IPV4地址字符串。 2)判断是否是合法主机IP地址的接口。 3)判断IP地址是A类,B类,C类或者其他的接口。 */ class IP { public: IP(char *ip); ~IP(){delete []iar;} bool isIp(); char ipType(); private: bool toIpArr(); //字符串ip转数组ip int toInt(int s,int e); //字符串转整数 string ipstr; int ip[4]; //4个字段的数字 int len; char *iar; }; IP::IP(char *ip) { ipstr = ip; len = ipstr.length(); //字符串长度(不带结尾) iar=NULL;//初始化指针 } bool IP::isIp() { int i,j=0; if(len>15)//(点分十进制)ip最大长度15字符 return false; //字符->ASCII值: . -> 46 0~9 -> 48~57 for(i=0; i<len-1; i++) { if(ipstr[i]!=46 && (ipstr[i]<48 || ipstr[i]>57)) return false; else { //格式正确,判断值的合法性 if(toIpArr()) return true; else return false; } } } bool IP::toIpArr() { int i,j,count; j = 0; //'.'的标志位 count =0; //'.'的个数(3个) int arr[4]; //记录点的下标位置和个数 for(i=0; i<len-1; i++) { if(ipstr[i]==46) { arr[count] = i; count++;//cout可能大于3,故至少arr[4], if(count>3)//否则会越界 return false; } } //首尾一定没有'.',‘.’也不会相邻,'.'只能是3个;如:.2.1.、192.168..11、1.2.3 if(arr[1]==0||arr[2]==len-1||arr[1]==arr[0]+1||arr[1]==arr[2]-1||count!=3) return false; else { //根据起始位置截取字符并转整数 ip[0] = toInt(0,arr[0]); ip[1] = toInt(arr[0]+1,arr[1]); ip[2] = toInt(arr[1]+1,arr[2]); ip[3] = toInt(arr[2]+1,len); //每个字段的值范围:0--255 for(i=0; i<4; i++) { if(ip[i]>=255 || ip[i]<=0) return false; } return true; } } int IP::toInt(int s,int e) { int i,l; l = e-s; //每段数字的长度 iar = new char[l+1]; iar[l]='\0'; for(i=0;i<l;i++) { iar[i]=ipstr[s+i]; } //atoi是将将字符串转为整数的函数 return atoi(iar); } char IP::ipType() { /*ip地址包括网络号和主机号,分类: A类:1.0.0.0 ---126.255.255.255 B类:128.0.0.0--191.255.255.255 C类:192.0.0.0--223.255.255.255 D类:224.0.0.0--239.255.255.255 E类:240.0.0.0--247.255.255.255 */ if(ip[0]>=0 && ip[0]<=126) return 'A'; else if(ip[0]>=128 && ip[0]<=191) return 'B'; else if(ip[0]>=192 && ip[0]<=223) return 'C'; else if(ip[0]>=224 && ip[0]<=239) return 'D'; else if(ip[0]>=240 && ip[0]<=247) return 'E'; else return '*'; } int main( ) { char str[] = "192.168.1.1"; IP ip(str); //声明并初始化对象 cout << "ipv4地址: " << str; if(ip.isIp()) { cout<<" 合法"<<endl; cout<<"该ip地址为 "<<ip.ipType()<<"类 地址"<<endl; } else cout<<" 不合法"<<endl; return 0; }
ipv4地址: 192.168.1.1 合法 该ip地址为 C类 地址 请按任意键继续. . .
2012-3 求一个整数的三件事
#include <iostream> #include <cmath> using namespace std; /* 2012-3 对于一个给定的,不超出long范围的正数,只使用一次循环, 计算以下三件事并输出结果: 1)求出它是几位数 2)将它每一位数字分别输出,数字间用连字符分隔 3)最后按照逆序输出每一位数字 */ int main( ) { int i=1; //位数 int j=1; //控制char[] long num; //除数 char str[21]={0},sno[20]={0};//正向存储 char arr[10]={0};//存储各位数 long m0,m1; cout << "请输入一个整数:"; cin>> num; do { m0 = pow(10.0,i); // 模数 m1 = pow(10.0,i-1); // 被除数 itoa (num%m0 / m1,&arr[i-1],10); //itoa()整数转换成字符函数 //从右至左依次取各位数并存在数组 str[20-j] = arr[i-1]; str[20-j-1] = '-'; //倒着存储字符 i++; j += 2;//步长为2 } while(num%m0 != num); strcpy(sno,str+(20-(j-2))); //拷贝字符串 sno[j-2] = '\0'; //加上结束符 cout << strlen(arr) <<endl; //几位数 cout << sno << endl; cout << arr <<endl;//这个数逆序 return 0; }
请输入一个整数:36987 5 3-6-9-8-7 78963 请按任意键继续. . .
练习(仅此学习c++,非海大,投机者勿看)
######求500以内的素数#include <iostream> #include <cmath> using namespace std; bool primeNumber(int x) { int t; for(t=2;t<=sqrt((double)x);t++) { if(x%t==0) return false; } return true; } /* 质数(prime number)又称素数,有无限个。 除了1和它本身以外不再有其他的除数整除。 最小的质数是2。 */ int main() { int i,c=0; for(i=2;i<=500;i++) { if(primeNumber (i)) { c++; cout<<c<<": "<<i<<"\t\t"; if(c%5==0) cout<<endl; } } return 0; }
1: 2 2: 3 3: 5 4: 7 5: 11 6: 13 7: 17 8: 19 9: 23 10: 29 11: 31 12: 37 13: 41 14: 43 15: 47 16: 53 17: 59 18: 61 19: 67 20: 71 21: 73 22: 79 23: 83 24: 89 25: 97 26: 101 27: 103 28: 107 29: 109 30: 113 31: 127 32: 131 33: 137 34: 139 35: 149 36: 151 37: 157 38: 163 39: 167 40: 173 41: 179 42: 181 43: 191 44: 193 45: 197 46: 199 47: 211 48: 223 49: 227 50: 229 51: 233 52: 239 53: 241 54: 251 55: 257 56: 263 57: 269 58: 271 59: 277 60: 281 61: 283 62: 293 63: 307 64: 311 65: 313 66: 317 67: 331 68: 337 69: 347 70: 349 71: 353 72: 359 73: 367 74: 373 75: 379 76: 383 77: 389 78: 397 79: 401 80: 409 81: 419 82: 421 83: 431 84: 433 85: 439 86: 443 87: 449 88: 457 89: 461 90: 463 91: 467 92: 479 93: 487 94: 491 95: 499 请按任意键继续. . .
模拟定义存折类,并派生出信用卡类
/* 模拟定义存折类,并派生出信用卡类; 在存折类中提供可以实现开户,存款、取款和查询余额的函数,要求取款金额必须小于余额; 信用卡类对取款操作进行修改,允许透支一定金额; 在main()函数中定义对象,并进行存取操作。 */ #include <iostream> #include <string> using namespace std; class BankBook { public: BankBook(string a,string n,string t)//开户 { account=a; name=n; typeCade=t; } void deposit(int in)//存款 { remain+=in; cout<<"存款"<<in<<Currency<<"成功!\n"; } void withdraw(int out)//取款 { //cout<<"取款"<<out<<Currency<<"\n"; if(remain>=out) {remain-=out;cout<<"\n取款"<<out<<Currency<<"成功!"<<endl; cout<<"您的"<<typeCade<<"余额为:"<<remain<<Currency<<endl<<endl;} else { cout<<"取款"<<out<<Currency<<"失败!\n取钱不能超过余额!"<<endl; cout<<"您的"<<typeCade<<"余额为:"<<remain<<Currency<<endl; } } void enquire()//查询余额 { cout<<"您的"<<typeCade<<"余额为:"<<remain<<Currency<<endl<<endl; } void showAccount(string Cur)//显示帐户信息 { Currency=Cur; cout<<"您的"<<typeCade<<"号 : "<<account<<endl; cout<<"您的"<<typeCade<<"开户姓名:"<<name<<endl; cout<<"您的"<<typeCade<<"结算币种:"<<Currency<<endl; } double remain;//余额 string typeCade; string Currency;//币种 private: string account;//帐号 string name;//储户姓名 }; class Credit_Cards:public BankBook { public: Credit_Cards(string a,string n,string t):BankBook(a,n,t) {} void withdraw(int out)//取款 { if((remain-out)>=0) {remain-=out;cout<<"\n取款"<<out<<Currency<<"成功!"<<endl; cout<<"您的余额为:"<<remain<<Currency<<endl;} else if((out-remain)>=2000) {cout<<"\n取款"<<out<<Currency<<"失败!"<<endl; cout<<"您的"<<typeCade<<"最多透支2000"<<Currency<<endl; cout<<"您的"<<typeCade<<"余额为:"<<remain<<Currency<<endl;} else {cout<<"\n取款"<<out<<Currency<<"成功!"<<endl; cout<<"您透支了"<<out-remain<<Currency<<endl; remain-=out; cout<<"您的余额为:"<<remain<<Currency<<endl;} } }; int main() { BankBook b("6226622200003600","保罗","借记卡"); b.remain=0; cout<<"您的借记卡开户成功! \n"; b.showAccount(" RMB"); b.deposit(800); b.withdraw(600); b.withdraw(300); b.withdraw(100); Credit_Cards c("6226622200003877","保罗","信用卡"); c.remain=0; cout<<"\n您的信用卡开户成功! \n"; c.showAccount(" $$"); c.deposit(6666); c.withdraw(8888); c.withdraw(666); c.withdraw(6600); }
您的借记卡开户成功! 您的借记卡号 : 6226622200003600 您的借记卡开户姓名:保罗 您的借记卡结算币种: RMB 存款800 RMB成功! 取款600 RMB成功! 您的借记卡余额为:200 RMB 取款300 RMB失败! 取钱不能超过余额! 您的借记卡余额为:200 RMB 取款100 RMB成功! 您的借记卡余额为:100 RMB 您的信用卡开户成功! 您的信用卡号 : 6226622200003877 您的信用卡开户姓名:保罗 您的信用卡结算币种: $$ 存款6666 $$成功! 取款8888 $$失败! 您的信用卡最多透支2000 $$ 您的信用卡余额为:6666 $$ 取款666 $$成功! 您的余额为:6000 $$ 取款6600 $$成功! 您透支了600 $$ 您的余额为:-600 $$ 请按任意键继续. . .
对象成员、友元、常类型的应用
/* 1.定义学生类student,和成绩类score。 要求: (1)score包含有数学成绩、英语成绩、计算机基础成绩等数据成员,成员函数自己发挥想象; (2)学生包含有:姓名、学号、成绩对象成员;定义学生类的构造函数、析构函数、设定值的函数等; (3)在类中加入静态成员count统计学生人数; (4)定义声明任意友元函数,访问学生类的私有成员 (5)定义日期类,扩充学生类的数据成员,加入学生出生日期成员。完成相关的应用 (6)发挥想象,分别加入对象数组和对象指针的应用。 */ #include<iostream> #include<string> using namespace std; class Score//成绩类 { public: Score(float c,float e,float m);//声明带参构造函数 Score(); //声明无参构造函数 void show() //显示函数 { cout<<" 电脑分数:"<<computer; cout<<" 英语分数:"<<english; cout<<" 计算机基础分数:"<<mathematics; } void modify(float c,float e,float m);//声明修改函数 private: float computer; //电脑成绩 float english; //英语成绩 float mathematics; //计算机基础成绩 }; //类外赋值构造函数 Score::Score(float c,float e,float m) {computer=c;english=e;mathematics=m;} //类外无参构造函数初始化成绩为0 Score::Score() {computer=english=mathematics=0;} //类外修改函数 void Score::modify(float c,float e,float m) {computer=c;english=e;mathematics=m;} class Student//学生类 { private: char *name;//姓名指针变量 char *stu_no;//学号指针变量 Score score1;//成绩对象 static int count;//静态变量 public: Student(char *name1,char *stu_no1,float s1,float s2,float s3); ~Student(); friend void Print(Student &);//声明友元函数 void show(); void modify(char *name1,char *stu_no1,float s1,float s2,float s3); void getNum(){cout<<"\n\n学生个数:"<<count;}//统计学生个数函数 }; //类外构造函数 Student::Student(char *name1,char *stu_no1,float s1,float s2,float s3):score1(s1,s2,s3) { name=new char[strlen(name1)+1]; //计算指针字符串长度,并开辟内存空间 strcpy(name,name1); //拷贝赋值 stu_no=new char[strlen(stu_no1)+1]; strcpy(stu_no,stu_no1); count++; } //类外构造函数 Student::~Student() { //释放内存 delete []name; delete []stu_no; } //类外构造函数 void Student::modify(char *name1,char *stu_no1,float s1,float s2,float s3) { delete[]name; //释放name地址内存 name=new char[strlen(name1)+1]; //重新分配内存空间 strcpy(name,name1); //重新拷贝赋值 delete[]stu_no; stu_no=new char[strlen(stu_no1)+1]; strcpy(stu_no,stu_no1); //调用字符串复制函数 score1.modify(s1,s2,s3); //调用成绩对象修改函数 } //类外构造函数 void Student::show() { cout<<"姓名:"<<name; cout<<" 学号:"<<stu_no; score1.show(); } //初始化静态成员count int Student::count = 0; void Print(Student &obj)//student友元函数 { cout<<"\n这是学生对象的友元函数"; cout<<"\n访问私有成员 姓名:"<<obj.name; cout<<"\n访问私有成员 学号:"<<obj.stu_no; } class Date:public Student //类Date以公有方式继承Student类 { private: int year,month,day; public: Date(int y,int m,int d):Student("王俊凯","20130110",9,7,8) {year=y;month=m;day=d;} ~Date(){} void printDate() { cout<<"\n\n入学日期:"; cout<<year<<"年"<<month<<"月"<<day<<"日 "; show(); } }; void main() { Student stu[6]= { Student("毕加猪","20130101",90,80,59), Student("朱丽叶","20130102",72,80,69), Student("周杰伦","20130103",70,69,58), Student("王力宏","20130107",52,89,75), Student("范玮琪","20130108",99,77,98), Student("王若琳","20130109",82,69,96) }; for(int i=0;i<6;i++)//遍历数组对象 {cout<<endl;stu[i].show();} stu[0].getNum();//调用静态变量count cout<<endl; Print(stu[5]);//调用友元函数 Date d2(2011,9,1); d2.printDate(); cout<<endl; }
姓名:毕加猪 学号:20130101 电脑分数:90 英语分数:80 计算机基础分数:59 姓名:朱丽叶 学号:20130102 电脑分数:72 英语分数:80 计算机基础分数:69 姓名:周杰伦 学号:20130103 电脑分数:70 英语分数:69 计算机基础分数:58 姓名:王力宏 学号:20130107 电脑分数:52 英语分数:89 计算机基础分数:75 姓名:范玮琪 学号:20130108 电脑分数:99 英语分数:77 计算机基础分数:98 姓名:王若琳 学号:20130109 电脑分数:82 英语分数:69 计算机基础分数:96 学生个数:6 这是学生对象的友元函数 访问私有成员 姓名:王若琳 访问私有成员 学号:20130109 入学日期:2011年9月1日 姓名:王俊凯 学号:20130110 电脑分数:9 英语分数:7 计算机基础分数:8 请按任意键继续. . .
运算符重载
#include<iostream> using namespace std; #include<stdlib.h> #include<time.h> #define random(a,b) (rand()%((b)-(a)) + (a)) //编写一个程序,用成员运算符函数重载+、-、将两个二维数组相加和相减 class Add { public: Add(int x = 0) { n = x; } int getn() { return n; } //定义重载成员运算符函数 Add operator+(Add); Add operator-(Add); private: int n; }; Add Add::operator+(Add a) { Add temp; temp.n = n + a.n; return temp; } Add Add::operator-(Add a) { Add temp; temp.n = n - a.n; return temp; } int main() { //用当前时间初始化随机数种子 srand((unsigned) time(NULL)); int index = 0; //控制 列下标 int IntArray0[7][7];//原理:用一维数组为二维数组赋值 int IntArray1[7][7]; int arr0[7 * 7], arr1[7 * 7],w = 0,w1=0; for (int a = 0; a < 7 * 7; a++) { arr0[a] = random(0, 10); arr1[a] = random(20, 80); } cout << "\t数组a1\t\t\t数组a2\n"; while (w < 7) { for (int c = 0; c < 7; c++) { cout << arr0[w * 7 + c] << " "; } cout << "\t"; for (int b = 0; b < 7; b++) { cout << " " << arr1[w * 7 + b]; } w++; cout << endl; } Add add[7 * 7 * 2], totle[7 * 7 * 2]; //为数组对象Add add[49*2] 赋值 for (int a = 0; a < 7 * 7; a++) { add[a] = arr0[a]; add[a + 7 * 7] = arr1[a]; } for (int a = 0; a < 7 * 7; a++) { totle[a] = add[a] + add[a + 7 * 7]; //cout<<"int(a/7)="<<int(a/7)<<" "<<index<<"\n"; IntArray0[int(a/7)][index] = totle[a].getn(); index++; if (index == 7) { index = 0; } } for (int a = 0; a < 7 * 7; a++) { totle[a] = add[a + 7 * 7] - add[a]; IntArray1[a/7][index] = totle[a].getn(); index++; if (index == 7) { index = 0; } } cout << "\n\ta1 + a2\t\t\ta2 - a1\n"; while (w1 < 7) { for (int j = 0; j < 7; j++) { //printf("%3d", arrOne[i][j]); cout << IntArray0[w1][j] << " "; } cout <<"\t "; for (int b = 0; b < 7; b++) { cout << IntArray1[w1] << " "; } w1++; cout << endl; } return 0; }
数组a1 数组a2 9 6 4 4 5 9 5 52 78 49 53 73 42 65 8 4 3 8 9 3 1 37 47 37 26 47 49 46 8 9 9 0 0 3 0 51 58 39 34 66 46 77 8 3 5 5 0 4 7 21 45 62 48 49 22 40 9 2 1 7 0 8 2 20 38 47 32 76 39 47 1 5 6 4 5 2 2 69 57 35 47 42 75 45 7 4 0 1 2 3 8 53 59 59 61 45 22 71 a1 + a2 a2 - a1 61 84 53 57 78 51 70 43 72 45 49 68 33 60 45 51 40 34 56 52 47 29 43 34 18 38 46 45 59 67 48 34 66 49 77 43 49 30 34 66 43 77 29 48 67 53 49 26 47 13 42 57 43 49 18 33 29 40 48 39 76 47 49 11 36 46 25 76 31 45 70 62 41 51 47 77 47 68 52 29 43 37 73 43 60 63 59 62 47 25 79 46 55 59 60 43 19 63 请按任意键继续. . .
[b]虚函数与多态性
/* 写一个程序,定义抽象基类container,由它派生出3个派生类,sphere(球体),cylinder(圆柱体),cube(正方体),用虚函数分别计算几种图形的表面积和体积。 */ #include<iostream> using namespace std; class Container { virtual double Volume() = 0; virtual double Space() = 0; }; class Sphere: public Container { protected: double radius; public: Sphere(double r) { radius = r; } virtual double Volume() { return (4 / 3) * radius * radius * radius * 3.1416; } virtual double Space() { return 4 * 3.1416 * radius * radius; } }; class Cylinder: public Container { private: double radiu; double height; public: Cylinder(double r, double h) : radiu (r), height(h){} virtual double Volume() { return radiu * radiu * 3.1416 * height; } virtual double Space() { return (radiu * radiu * 3.1416 * 2 + radiu * 3.1416 * 2 * height); } }; class Cube: public Container { double length; public: Cube(double x) :length(x) {} virtual double Volume() { return length * length * length; } virtual double Space() { return 6 * length * length; } }; int main() { Sphere s(1.2); Cylinder c(1.2,3); Cube cu(2); cout<<"已知球体的半径为:1.2;圆柱的底面半径为1.2,高为3;正方体的边长为2;\n"; cout<<"球 体表面积为: "<<s.Space() <<" 球体体积为: "<<s.Volume(); cout<<"\n圆柱体表面积为: "<<c.Space() <<" 圆柱体积为: "<<c.Volume(); cout<<"\n正方体表面积为: "<<cu.Space() <<"\t正方体体积为: "<<cu.Volume()<<endl; return 0; }
已知球体的半径为:1.2;圆柱的底面半径为1.2,高为3;正方体的边长为2; 球 体表面积为: 18.0956 球体体积为: 5.42868 圆柱体表面积为: 31.6673 圆柱体积为: 13.5717 正方体表面积为: 24 正方体体积为: 8 请按任意键继续. . .
相关文章推荐
- 16位图像Alpha混合的实现(用汇编写的,比MMX还要快)
- 矩阵快速幂
- 百度地图---之---桩号转坐标
- Java中回调机制
- JAVA通过访问页面中的URL实现Http文件下载到本地
- 微信支付企业打款,获取用户信息
- 微信公众平台开发(111) 现金红包、裂变红包、企业付款
- 使用Simditor和七牛上传图片
- 蜂窝教育iOS培训 让学员学有所用
- python 基础二、列表【list】
- 最近的一些记录
- 2016SDAU课程练习一1005
- 如何使用JDBC的PreparedStatement类的setClob()方法
- 重写hashcode
- 让我们一起Go(八)
- 让我们一起Go(十)
- 使用模板创建RadioButton-List控件
- 让我们一起Go(七)
- 新手silverlight练习--五子棋( 二 )
- 找出系统中的段地址