资源描述
实验一 熟悉VC++IDE开发环境
一、实验目的
1、熟悉VC++6.0集成开发环境,熟练掌握VC++6.0项目工作区、各种编辑器、菜单栏和工具栏的使用。
2、掌握如何编辑、编译、连接和运行一个C++程序。
3、通过运行简单的C++程序,初步了解C++源程序的结构和特点。
二、实验要求
1、分析下列程序运行的结果。
程序一:
#include <iostream.h>
int add(int x,int y=8);
void main()
{ int x=4;
cout<<add(x)<<",";
cout<<add(x,add(add(x,add(x))))<<endl;
}
int add(int x,int y)
{ return x+y;
}
//12,28
程序二:
#include <iostream.h>
void main()
{ int *p,i;
i=5;
p=&i;
i=*p+10;
cout<<"i="<<i<<endl;
}
//i=15
程序三:
#include <iostream.h>
void main(void)
{ int i=10;
int &r=i;
r++;
cout<<"i="<<i<<", r="<<r<<'\n';
i=88;
cout<<"i="<<i<<", r="<<r<<'\n';
}
//i=11,r=11
i=88,r=88
程序四:
#include <iostream.h>
int f(int i)
{ static int k=1;
for(;i>0;i--)
k +=i;
return k;
}
void main()
{ int i;
for(i=0;i<5;i++)
cout<<f(i)<<" ";
}
// 1 2 5 11 21
程序五:
#include <iostream.h>
void func();
int n=1;
void main()
{ static int a;
int b= -9;
cout <<"a:"<<a<<" b:"<<b<<" n:" <<n<<endl;
b+=4;
func();
cout <<"a:"<<a<<" b:"<<b<<" n:"<<n<<endl;
n+=10;
func();
}
void func()
{ static int a=2; int b=5;
a+=2;
n+=12;
b+=5;
cout <<"a:" <<a<<" b:" <<b<<" n:" <<n <<endl;
}
// a:0 b:-9 n:1
a:4 b:10 n:13
a:0 b:-5 n:13
a:6 b:10 n:35
实验二 C++对C的扩充
一、实验目的
1、了解在面向对象程序设计过程中C++对C功能的扩充与增强,并善于在编写程序的过程中应用这些新功能。
2、进一步熟悉编辑、编译、连接和运行C++程序的方法。
3、进一步熟悉C++程序的结构和编程方法。
二、实验要求
1、分析下列程序运行的结果。
#include <iostream.h>
int amount=123;
void main()
{ int amount=456;
cout<<::amount<<',';
cout<<amount<<',';
::amount=789;
cout<<::amount<<',';
cout<<amount<<'\n';
}
// 123,456,789,456
2、编写一个程序,用来求2个或3个正整数中的最大数。
①用不带默认参数的函数实现。
include <iostream>
using namespace std;
int max(int a,int b,int c) //求3个整数中的最大者
{if (b>a) a=b;
if (c>a) a=c;
return a;
}
int max(int a, int b) //求两个整数中的最大者
{if (a>b) return a;
else return b;
}
int main( )
{int a=7,b=-4,c=9;
cout<<max(a,b,c)<<endl; //输出3个整数中的最大者
cout<<max(a,b)<<endl; //输出两个整数中的最大者
return 0;
}
②用带默认参数的函数实现。
#include <iostream>
using namespace std;
int main()
{int max(int a,int b,int c=0);
int a,b,c;
cin>>a>>b>>c;
cout<<"max(a,b,c)="<<max(a,b,c)<<endl;
cout<<"max(a,b)="<<max(a,b)<<endl;
return 0;
}
int max(int a,int b,int c)
{if(b>a) a=b;
if(c>a) a=c;
return a;
}
3、有5个字符串,要求对它们按由小到大顺序排列,用string方法。
#include <iostream>
#include <string>
using namespace std;
int main()
{ int i;
string str[5]={"BASIC","C","FORTRAN","C++","PASCAL"};
void sort(string []);
sort(str);
cout<<"the sorted strings :"<<endl;
for(i=0;i<5;i++)
cout<<str[i]<<" ";
cout<<endl;
return 0;
}
void sort(string s[])
{int i,j;
string t;
for (j=0;j<5;j++)
for(i=0;i<5-j;i++)
if (s[i]>s[i+1])
{t=s[i];s[i]=s[i+1];s[i+1]=t;}
}
4、定义一个求两个数中较小值的函数模板min( ),要求在main( )函数中进行调用求两个
浮点型数据和两个整型数据中较小的数。
#include "iostream"
#include "string"
using namespace std;
template<typename T>
T min(T a,T b)
{
return a<b?a:b;
}
int main()
{
int a=1, b=9;
float c=1.23471,d=32.431564;
cout<<"The min of "<<a<<" and "<<b<<" is "<<min(a,b)<<endl
<<"The min of "<<c<<" and "<<d<<" is "<<min(c,d)<<endl;
return 0;
}
实验三 类和对象(一)
一、实验目的
1、掌握声明类的方法,类和类的成员的概念以及定义对象的方法。
2、掌握类的构造函数与析构函数的概念和使用方法。
3、初步掌握用类和对象编制基于对象的程序。
二、实验要求
1、分析下面的程序,写出其运行时的输出结果。
#include <iostream>
using namespace std;
class Date
{public:
Date(int,int,int);
Date(int,int);
Date(int);
Date( );
void display( );
private:
int month;
int day;
int year;
};
Date∷Date(int m,int d,int y):month(m),day(d),year(y){ }
Date∷Date(int m,int d):month(m),day(d) {year=2005;}
Date∷Date(int m):month(m)
{ day=1;
year=2005;
}
Date∷Date( )
{ month=1;
day=1;
year=2005;
}
void Date∷display( )
{cout<<month<<″/″<<day<<″/″<<year<<endl;}
int main( )
{ Date d1(10,13,2005);
Date d2(12,30);
Date d3(10);
Date d4;
d1.display( );
d2.display( );
d3.display( );
d4.display( );
return 0;
}
// 10/13/2005
12/30/2005
10/1/2005
1/1/2005
2、建立一个名为Student的类,该类有以下几个私有成员变量:学生姓名、学号、性别、年龄。还有以下两个成员变量:一个用于初始化学生姓名、学号、性别和年龄的构造函数,一个用于输出学生信息的函数。编写一个主函数,声明一个学生对象,然后调用成员函数在屏幕输出学生信息。
#include "iostream"
#include "string"
using namespace std;
class student
{
public:
student();
void display();
private:
string sName,sNum;
char chSex;
int iAge;
};
student::student(string na,string num,char s,int a):sName(na),sNum(num),
chSex(s),iAge(a){}
void student::display()
{
cout << "----------THE INFORMATION OF STUDENT----------\n";
cout << "name: "<< sName << endl << "number: "<< sNum<<endl
<< "sex: "<< chSex << endl << "age: "<< iAge <<endl;
}
int main()
{
student s("WangFang","0811045263",'w',20);
s.display();
return 0;
}
3、类Person的定义如下,请实现该类,并在主函数中创建对象obj,然后使用构造函数为obj赋予初始值(内容自定)。
class Person
{ private:
char name[10];
int age;
int salary;
char tel[8];
public:
Person(char *xname,int xage,int xsalary,char *xtel);
void disp();
};
解:
#include <iostream.h>
#include <string.h>
Person::person(char *Xname,int Xage,int Xsalary,char *Xtel)
{ strcpy ( name, xname);
age=xage;
salary=xsalary;
strcpy (tel,xtel);}
void Person::disp()
{ cout<<“ 姓名:”<<name<<endl;
cout<<“ 年龄”:<<age<<endl;
cout<<“ 工资”:<<salary<<endl:
cout<<“ 电话”:<<tel<<endl;
}
void main()
{ Person obj (“张三”,25,850,“45678912”);
obj.disp()
}
实验四 类和对象(二)
一、实验目的
1、进一步加深对类和对象的理解。
2、掌握对类的对象数组、对象的指针及其使用方法。
3、掌握友元的概念和使用。
4、了解类模板的使用方法。
二、实验要求
1、分析并比较下列程序运行的结果。
程序一:
#include<iostream.h>
#include<iostream.h>
class smallone
{public:
smallone(int sma)
{ cout<<"sm constr:"<<sma<<"\n";}
};
void fn(int n)
{ smallone sm(n);
cout<<"in function fn with n="<<n<<endl;
}
int main()
{ fn(10);
fn(20);
return 0;
}
//sm constr: 10
in function fn with n=10
sm constr: 20
in function fn with n=20
程序二:
#include<iostream.h>
#include<iostream.h>
class smallone
{public:
smallone(int sma)
{ cout<<"sm constr:"<<sma<<"\n";}
};
void fn(int n)
{ static smallone sm(n);
cout<<"in function fn with n="<<n<<endl;
}
int main()
{ fn(10);
fn(20);
return 0;
}
//sm constr:10
in function fn with n=10
in function fn with n=20
2、建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。
#include <iostream>
using namespace std;
class Student
{public:
Student(int n,float s):num(n),score(s){}
int num;
float score;
};
void main()
{Student stud[5]={
Student(101,78.5),Student(102,85.5),Student(103,98.5),
Student(104,100.0),Student(105,95.5)};
void max(Student* );
Student *p=&stud[0];
max(p);
reyurn 0;
}
void max(Student *arr)
{float max_score=arr[0].score;
int k=0;
for(int i=1;i<5;i++)
if(arr[i].score>max_score) {max_score=arr[i].score;k=i;}
cout<<arr[k].num<<" "<<max_score<<endl;
}
3、声明一个类模板,利用它分别实现两个整数、浮点数和字符的比较,求出大数和小数。
#include <iostream>
using namespace std;
template<class numtype>
class Compare
{public:
Compare(numtype a,numtype b)
{x=a;y=b;}
numtype max()
{return (x>y)?x:y;}
numtype min()
{return (x<y)?x:y;}
private:
numtype x,y;
};
int main()
{Compare<int> cmp1(3,7);
cout<<cmp1.max()<<" is the Maximum of two inteder numbers."<<endl;
cout<<cmp1.min()<<" is the Minimum of two inteder numbers."<<endl<<endl;
Compare<float> cmp2(45.78,93.6);
cout<<cmp2.max()<<" is the Maximum of two float numbers."<<endl;
cout<<cmp2.min()<<" is the Minimum of two float numbers."<<endl<<endl;
Compare<char> cmp3('a','A');
cout<<cmp3.max()<<" is the Maximum of two characters."<<endl;
cout<<cmp3.min()<<" is the Minimum of two characters."<<endl;
return 0;
}
实验五 运算符重载
一、实验目的
1、进一步了解运算符重载的概念和使用方法。
2、掌握几种常用的运算符重载的方法。
二、实验要求
1、定义一个复数类Complex,重载运算法“+”,使之能用于复数的加法运算。将运算符重载为普通函数(非成员、非友元)、成员函数、友元函数。根据要求修改通过函数来实现复数相加的示例,分别编写程序,求两个复数之和。
#include <iostream>
using namespace std;
class Complex //定义Complex类
{public:
Complex(float x=0,float y=0){real=x;imag=y;} //构造函数
Complex complex_add(Complex &c2); //声明复数相加函数
void display() { cout<<real<<'+'<<imag<<'i'<<endl; };
private:
float real; //实部
float imag; //虚部
};
Complex Complex::complex_add(Complex &c2)
{ Complex c;
c.real = real +c2.real;
c.imag=imag+c2.imag;
return c;
}
int main()
{ Complex complex1(3.34f, 4.8f), complex2(12.8f, 5.2f);
Complex complex; //定义3个复数对象
complex=plex_add(complex2); // 进行两个复数的加运算
complex.display( );
return 0;
}
//16.14+10i
//普通函数(非成员、非友元)
#include <iostream>
using namespace std;
class Complex
{public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
double get_real();
double get_imag();
void display();
private:
double real;
double imag;
};
double Complex::get_real()
{return real;}
double Complex::get_imag()
{return imag;}
void Complex::display()
{cout<<"("<<real<<","<<imag<<"i)"<<endl;}
Complex operator + (Complex &c1,Complex &c2)
{
return Complex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag());
}
int main()
{Complex c1(3,4),c2(5,-10),c3;
c3=c1+c2;
cout<<"c3=";
c3.display();
return 0;
}
//运算符重载为成员函数
#include <iostream>
using namespace std;
class Complex
{public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
Complex operator + (Complex &c2);
void display();
private:
double real;
double imag;
};
Complex Complex::operator + (Complex &c2)
{Complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;}
void Complex::display()
{cout<<"("<<real<<","<<imag<<"i)"<<endl;}
int main()
{Complex c1(3,4),c2(5,-10),c3;
c3=c1+c2;
cout<<"c1=";c1.display();
cout<<"c2=";c2.display();
cout<<"c1+c2=";c3.display();
return 0;
}
//将运算符重载为友元函数
#include <iostream>
using namespace std;
class Complex
{public:
Complex(){real=0;imag=0;}
Complex(double r){real=r;imag=0;}
Complex(double r,double i){real=r;imag=i;}
friend Complex operator+ (Complex &c1,Complex &c2);
void display();
private:
double real;
double imag;
};
Complex operator+ (Complex &c1,Complex &c2)
{return Complex(c1.real+c2.real, c1.imag+c2.imag);}
void Complex::display()
{cout<<"("<<real<<","<<imag<<"i)"<<endl;}
int main()
{Complex c1(3,4),c2(5,-10),c3;
c3=c1+c2;
cout<<"c1="; c1.display();
cout<<"c2="; c2.display();
cout<<"c1+c2="; c3.display();
return 0;
}
实验六 继承和派生
一、实验目的
1、了解继承在面向对象程序设计中的重要作用。
2、进一步理解继承与派生的概念。
3、掌握通过继承派生出一个新的类的方法。
4、了解虚基类的作用和用法。
二、实验要求
1、运行程序,分析构造函数与析构函数的调用顺序。
程序一:
#include <iostream.h>
class A
{ public:
A(){cout<<"A:Constructor"<<endl;}
~A(){cout<<"A:Destructor" <<endl;}
};
class B:public A
{ public:
B(){cout<<"B:Constructor" <<endl;}
~B(){cout<<"B:Destructor"<<endl;}
};
void main()
{ B b;
}
//A:Constructor
B:Constructor
B:Destructor
A:Destructor
程序二:
#include <iostream.h>
class A
{ int a;
public :
A(int aa=0) { a=aa; }
~A() { cout<<”Destructor A!”<<a<<endl; }
};
class B: public A
{ int b;
public:
B(int aa=0, int bb=0) : A(aa) { b=bb; }
~B() { cout<<”Destructor B!”<<b<<endl; }
};
void main()
{ B x(5),y(6,7);
}
//Destructor B!7
Destructor A!6
Destructor B!0
Destructor A!5
调用顺序:构造x.A a=5 构造x.B a=5 b=0 构造y.A // 不匹配,不调用A() 构造y.B a=6 b=7 析构y.B B!7 析构y.A A!6 析构x.B B!0 析构x.A A!5
2、分别声明Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cader类。要求:
①在两个基类种豆包含姓名、年龄、性别、地址、电话等数据成员。
②在Teacher类中还包含数据成员title(职称),在Cader 类中还包含数据成员post(职务)。在Teacher_Cader类中还包含数据成员wages(工资)。
③在对两个基类中的姓名、年龄、性别、地址、电话等数据成员用相同的名字,在引用这些数据成员时,指定作用域。
④在类体中声明成员函数,在类外定义数据成员。
⑤在派生类Teacher_Cader的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、职称、地址、电话,然后再调用cout语句输出职务和工资。
#include<string>
#include <iostream>
using namespace std;
class Teacher
{public:
Teacher(string nam,int a,char s,string tit,string ad,string t);
void display();
protected:
string name;
int age;
char sex;
string title;
string addr;
string tel;
};
Teacher::Teacher(string nam,int a,char s,string tit,string ad,string t):
name(nam),age(a),sex(s),title(tit),addr(ad),tel(t){ }
void Teacher::display()
{cout<<"name:"<<name<<endl;
cout<<"age"<<age<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"title:"<<title<<endl;
cout<<"address:"<<addr<<endl;
cout<<"tel:"<<tel<<endl;
}
class Cadre
{public:
Cadre(string nam,int a,char s,string p,string ad,string t);
void display();
protected:
string name;
int age;
char sex;
string post;
string addr;
string tel;
};
Cadre::Cadre(string nam,int a,char s,string p,string ad,string t):
name(nam),age(a),sex(s),post(p),addr(ad),tel(t){}
void Cadre::display()
{cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"post:"<<post<<endl;
cout<<"address:"<<addr<<endl;
cout<<"tel:"<<tel<<endl;
}
class Teacher_Cadre:public Teacher,public Cadre
{public:
Teacher_Cadre(string nam,int a,char s,string tit,string p,string ad,string t,float w);
void show( );
private:
float wage;
};
Teacher_Cadre::Teacher_Cadre(string nam,int a,char s,string t,string p,string ad,string tel,float w):
Teacher(nam,a,s,t,ad,tel),Cadre(nam,a,s,p,ad,tel),wage(w) {}
void Teacher_Cadre::show( )
{Teacher::display();
cout<<"post:"<<Cadre::post<<endl;
cout<<"wages:"<<wage<<endl;
}
int main( )
{Teacher_Cadre te_ca("Wang-li",50,'f',"prof.","president","135 Beijing Road,Shanghai","(021)61234567",1534.5);
te_ca.show( );
return 0;
}
实验七 多态性和虚函数
一、实验目的
1、了解多态性的概念。
2、了解虚函数的作用及其使用方法。
3、了解静态关联和动态关联的概念和用法。
4、了解纯虚函数和抽象类的概念和用法。
二、实验要求
1、分析程序运行结果,掌握虚函数的使用。
程序一:
#include <iostream.h>
class ONE
{ public:
virtual void f(){cout<<"l"<<endl;}
};
class TWO:public ONE
{ public:
TWO(){cout<<"2"<<endl;}
};
class THREE:public TWO
{ public:
virtual void f(){TWO::f(); cout<<"3";}
};
void main()
{ ONE aa, *p;
TWO bb;
THREE cc;
p = &cc;
p->f();
}
//2
2
1
3
程序二:
#include<iostream.h>
class Base
{ public:
virtual void fn() { cout <<"In Base Class\n";}
};
class SubClass :public Base
{ public:
virtual void fn(){ cout <<"In Sub Class\n"; }
};
void main()
{ Base bc,*p;
SubClass sc;
p=&bc; p->fn();
p=≻ p->fn();
}
//In Base Class
In Sub Class
2、实现一个类A,在A中有两个私有的整型变量a和b,定义构造函数对a和b进行初始化,并实现成员函数geta()取得a的值和getb()取b的值。实现类B从A继承,覆盖geta(),使其返回a的2倍。主函数中声明类B对象,调用
展开阅读全文