实验一 数据类型和表达式.doc_第1页
实验一 数据类型和表达式.doc_第2页
实验一 数据类型和表达式.doc_第3页
实验一 数据类型和表达式.doc_第4页
实验一 数据类型和表达式.doc_第5页
已阅读5页,还剩18页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实验一 数据类型和表达式选课时间:星期三下午 学号14084228 姓名 徐志鹏 【实验目的】1、熟悉并学习使用C+程序编译平台VC6.0;2、掌握如何在编译平台下编辑、编译、连接和运行一个简单的C+程序;3、掌握C+语言基础数据类型,熟悉变量及常量的定义和赋值方法;4、学会使用C+算术运算符和算术表达式;5、掌握C+程序的赋值和输入输出语句的使用方法;6、掌握简单C+程序的编写和调试【源程序】1: #includeusing namespace std;int min(int a , int b)if(aab;coutmin(a,b);return 0;2:#includeusing namespace std;int main()int a;double b;cinab;if(ab)coutaendl;else coutbendl;return 0;3:#includeusing namespace std;int main()double a,b;cina;b=a*(9.00/5.00)+32;coutb;return 0;4:#include#define fhcl 1.60934using namespace std;int main()double a,b;cina;b=a/fhcl;coutb;return 0;5:#includeusing namespace std;int main()int a,i,b; cina;if(a=1000) b=a%10000; a=a-b+1111; else if(a=100&a=10&a=0&a10)a=1; couta;return 0;【错误及解决办法】【实验总结】实验一比较简单,没有出现什么错误,代码实现和c差不多。 实验四 函数和作用域选课时间:星期三下午 学号14084228 姓名 徐志鹏 【实验目的】1、掌握函数的定义和说明方法;2、掌握函数调用时的传值调用、传址调用和引用调用方法;3、掌握函数重载的方法;4、学习使用指向字符串的指针变量;5、掌握作用域的种类和范围;【源程序】 1:#include#includeusing namespace std;void conver(double *x , double *y) double a,b; a=(*x)*cos(*y); b=(*x)*sin(*y); coutanbn;void conver(double &x , double &y) double a,b; a=x*cos(y); b=x*sin(y); coutanxy;conver(&x,&y); /传值调用conver(x,y); /引用调用coutxny;return 0;2:#includeusing namespace std;typedef struct fu double zb ; double xb ;fushu;void product(double &a,double &b) /浮点数相乘 double c; c=a*b; coutcn;void product(fushu &a,fushu &b) /复数相乘 fushu c; c.zb=a.zb*b.zb-a.xb*b.xb; c.xb=a.zb*b.xb+a.xb*b.zb; coutc.zb+c.xbin;int main() fushu a,b ;double x,y;cout两个浮点数xy;product(x,y);cout两个复数a.zba.xb;cinb.zbb.xb;product(a,b);return 0;3:#include#include#define pi 3.14using namespace std;int computeday(int year , int mot , int day) int num ; num = year*365 + mot*30 +day ; return num;double compute_physiological_index(int age) double cp ; cp = sin(2*pi)/28)*age); return cp ;double compute_emotional_index(int age) double cp ; cp = sin(2*pi)/23)*age); return cp ;double compute_mentality_index(int age) double cp ; cp = sin(2*pi)/33)*age); return cp ;int main() int y,m,d,n; double p,e,cm; coutymd; n=computeday(y,m,d); p=compute_physiological_index(n); cout生理指数pn; e=compute_emotional_index(n); cout情绪指数en; cm=compute_mentality_index(n); cout智力指数cmn;【错误及解决办法】【实验总结】更加熟悉对函数的使用实验5 类与对象(一)【实验目的】1、掌握类的概念以及定义类的方法;2、学习简单面向对象程序的编写;【源程序】1:#includeusing namespace std;class Fanpublic : void set(double s , double r , bool o , string c) speed = s ; radius = r ; on = o ; color = c ; void display() coutSpeed Is : speedn; coutRadius Is : radiusn; coutColor Is : colorn; coutOn Is : onn; private:double speed , radius ;bool on ;string color ;int main() Fan fan1 , fan2 ; coutThe first fans information : n; fan1.set(3,10,true,yellow); fan1.display(); coutThe secend fans information : n; fan2.set(2,5,false,blue); fan2.display();return 0;2:#includeusing namespace std;class Accountpublic : void set(int i , double b , double a) id = i ; balance = b ; annualInterestRate = a ; double getMonthlyInsterestRate(double a) return a/12 ; double withDraw(double m) balance -= m ; return balance ; double deposit(double m) balance += m ; return balance ; void display() coutId Is : idn; coutBalance Is : balancen; coutGetMonthlyInsterestRate Is : getMonthlyInsterestRate(annualInterestRate)*100%n; private: int id ; double balance , annualInterestRate ;int main() Account user ; user.set(1122 , 20000 , 0.045); user.withDraw(2500); user.deposit(3000); user.display();【错误及解决办法】一开始字符串用数组存发生一些莫名错误后面发现可以直接用string.【实验总结】定义类时,那些成员数据和成员函数是必须的,那些不是?public、protected、private分别表示什么意思?能否对类的成员数据在定义时初始化?在声明类时,没有必须的成员函数和成员数据。public代表公用成员,可在类外进行调用;private代表私有成员,只能被本类的成员函数访问,不能被类外访问;protect代表受保护的成员,可以被本类以及派生类的成员函数调用。不能对类的成员数据在定义时进行初始化。账户余额变量balance是否必须定义为静态变量?为什么?不一定必须定义为静态变量,普通变量的加减也可以满足该题的要求。实验6 类与对象(二)【实验目的】1、掌握类的构造函数和析构函数的概念和使用方法;2、掌握对象数组、对象指针的定义和使用方法;3、掌握new和delete的使用方法;【源程序】#includeusing namespace std;class CPointpublic : CPoint( double xx = 1 , double xy = 2) x = xx ; y = xy ; void SetX( double xx ) x = xx ; void SetY( double xy ) y = xy ; double GetX () return x ; double GetY () return y ; private: double x , y ; class CRectangle public : CRectangle( const CPoint & x , const CPoint & y ) l = x ; r = y ; void SetLPoint(const CPoint & x ) l = x ; void SetRPoint(const CPoint & y ) r = y ; double GetPerimeter() double x , y ; x = 2 * ( r.GetX() - l.GetX() ) ; y = 2 * ( r.GetY() - l.GetY() ) ; return (x+y) ; double GetArea() double x , y ; x = r.GetX() - l.GetX() ; y = r.GetY() - l.GetY() ; return x*y ; private: CPoint l , r ; ;int main()CPoint x(2,5) ;CPoint y(6,8) ;double p , a ;CRectangle * a_rectagnle = new CRectangle( x , y ) ; if( a_rectagnle != NULL ) p = a_rectagnle - GetPerimeter() ; a = a_rectagnle - GetArea(); coutPerimeter is : pn; coutArea is : an; delete a_rectagnle ;实验七 继承与派生【实验目的】1、理解继承在面向对象程序设计中的重要作用;2、理解继承和派生的概念;3、掌握通过继承派生出一个新类的方法;4、进一步学习简单面向对象程序的编写;【源程序】#include using namespace std;typedef enumAssistantProfessor,AssociateProfessor,Professorrankind;class MYDatepublic: MYDate(int y=1990 , int m=1 , int d=1) year = y ; month = m ; day = d ; int diffyear(MYDate a) int y, m = 0; if(a.year year) y = a.year - year - 1; m = a.month + 12 - month + y * 12; else y = year - a.year - 1; m = month + 12 - a.month + y * 12; return m; void print() coutyear:yearmonthmonthdaydayendl; int Gety() return year; int Getm() return month; int Getd() return day; private: int year , month , day ;class Personpublic: Person(string n , string i , string p) name = n ; id = i ; phonenumber = p ; void print() coutname:nameendl; coutid:idendl; coutphonenumber:phonenumberendl; private: string name ; string id ; string phonenumber ;class Student: public Personpublic: void print() Person:print(); coutGrade:gradeendl; Student(string name,string id,string phonenumber,string grade): Person(name,id,phonenumber),grade(grade)private: const string grade;class Employee: public Personpublic: Employee(string name , string id , string phonenumber , string of , double sal , MYDate dat): Person(name , id , phonenumber) office = of ; salary = sal ; dateHired = dat ; MYDate GetDate() return dateHired; void print() Person:print(); coutoffice:officeendl; coutsalary:salaryendl; couty:dateHired.Gety()endl; coutm:dateHired.Getm()endl; coutd:dateHired.Getd()endl; private: string office ; double salary ; MYDate dateHired ;class Faculty:public Employeepublic: Faculty(string name , string id , string phonenumber , string of , double sal , MYDate dat,int rank, double Basic): Employee( name , id , phonenumber , of , sal , dat), rank(rankind(rank - 1), Basicsalary(Basic) int xinshui() return Basicsalary * (int)rank + 1); void print() Employee:print(); coutrank:rankendl; private: const rankind rank; double Basicsalary;class Staff:public Employeepublic: Staff(string name , string id , string phonenumber , string of , double sal , MYDate dat,string po,double BasicWages, double Allowance): Employee( name , id , phonenumber , of , sal , dat), BasicWages(BasicWages),Allowance(Allowance) position = po ; int xins(MYDate d) int res ; MYDate x = GetDate(); res = BasicWages + Allowance*d.diffyear(x); return res ; void print() Employee:print(); coutposition:positionendl; coutBasicWages:BasicWagesendl; coutAllowance:Allowanceendl; private: string position ; const double BasicWages ; const double Allowance ;int main() MYDate date1(1950,1,1); MYDate date2(1996,1,1); MYDate date3(2000,1,1); Person p1(pName1,10001,7059261); Student p2(pName2,10002,7059262,freshman); Employee p3(pName3,10003,7059263,officelala,1000,date1); Faculty p4(pName4,10004,7059264,officehaha,2000,date2,3,1000); Staff p5(pName5,10005,7059265,officegaga,3000,date3,zongcai,1000,200); p1.print(); p2.print(); p3.print(); p4.print(); p5.print(); MYDate now(2010,1,1); coutmoney:p4.xinshui()endl; coutmoney:p5.xins(now)endl; return 0;【错误及解决办法】1:这次代码太长了,很容易出错,调bug调了半天.根据错误提示.【实验总结】 publicprotectedprivatepublic继承publicprotected不可用protected继承protectedprotected不可用private继承privateprivate不可用实验八 多态实现【实验目的】1、 了解继承和多态的作用和实现方式,掌握动态联编方法;2、 掌握使用C+语言的抽象类和派生类实现继承性;【源程序】 #includeusing namespace std;class CStereoShapepublic:virtual double GetArea()=0;virtual double GetVolume()=0;class CCube:public CStereoShapepublic:CCube(double

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论