实验一熟悉VC++IDE开发环境
一、实验目的
1、熟悉VC++6.0集成开发环境,熟练掌握VC++6.0项目工作区、各种编辑器、菜单栏和工具栏的使用。
2、掌握如何编辑、编译、连接和运行一个C++程序。
3、通过运行简单的C++程序,初步了解C++源程序的结构和特点。
二、实验要求
1、分析下列程序运行的结果。
程序一:
#include
int add(int x,int y=8);
void main()
{ int x=4;
cout< cout< } int add(int x,int y) { return x+y; } //12,28 程序二: #include void main() { int *p,i; i=5; p=&i; i=*p+10; cout<<"i="< } //i=15 程序三: #include void main(void) { int i=10; int &r=i; r++; cout<<"i="< } // 1 2 5 11 21 程序五: #include void func(); int n=1; void main() { static int a; int b= -9; cout <<"a:"< b+=4; func(); cout <<"a:"< n+=10; func(); } void func() { static int a=2; int b=5; a+=2; n+=12; b+=5; cout <<"a:" < } // 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 int amount=123; void main() { int amount=456; cout<<::amount<<','; cout< ::amount=789; cout<<::amount<<','; cout< } // 123,456,789,456 2、编写一个程序,用来求2个或3个正整数中的最大数。 ①用不带默认参数的函数实现。 include 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< cout< return 0; } ②用带默认参数的函数实现。 #include 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)="< cout<<"max(a,b)="< 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 #include using namespace std; int main() { int i; string str[5]={"BASIC","C","FORTRAN","C++","PASCAL"}; void sort(string []); sort(str); cout<<"the sorted strings :"< for(i=0;i<5;i++) cout< cout< 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 T min(T a,T b) { return a } int main() { int a=1, b=9; float c=1.23471,d=32.431564; cout<<"The min of "< return 0; } 实验三类和对象(一) 一、实验目的 1、掌握声明类的方法,类和类的成员的概念以及定义对象的方法。 2、掌握类的构造函数与析构函数的概念和使用方法。 3、初步掌握用类和对象编制基于对象的程序。 二、实验要求 1、分析下面的程序,写出其运行时的输出结果。 #include 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< 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 INFORMA TION OF STUDENT----------\n"; cout << "name: "<< sName << endl << "number: "<< sNum< << "sex: "<< chSex << endl << "age: "<< iAge < } 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 #include Person::person(char *Xname,int Xage,int Xsalary,char *Xtel) { strcpy ( name, xname); age=xage; salary=xsalary; strcpy (tel,xtel);} void Person::disp() { cout<<“姓名:”< cout<<“年龄”:< cout<<“工资”:< cout<<“电话”:< } void main() { Person obj (“张三”,25,850,“45678912”); obj.disp() } 实验四类和对象(二) 一、实验目的 1、进一步加深对类和对象的理解。 2、掌握对类的对象数组、对象的指针及其使用方法。 3、掌握友元的概念和使用。 4、了解类模板的使用方法。 二、实验要求 1、分析并比较下列程序运行的结果。 程序一: #include #include class smallone {public: smallone(int sma) { cout<<"sm constr:"< }; void fn(int n) { smallone sm(n); cout<<"in function fn with n="< } 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 #include class smallone {public: smallone(int sma) { cout<<"sm constr:"< }; void fn(int n) { static smallone sm(n); cout<<"in function fn with n="< } 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 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< } 3、声明一个类模板,利用它分别实现两个整数、浮点数和字符的比较,求出大数和小数。#include using namespace std; template class Compare {public: Compare(numtype a,numtype b) {x=a;y=b;} numtype max() {return (x>y)?x:y;} numtype min() {return (x private: numtype x,y; }; int main() {Compare cout< cout< Compare cout< cout< Compare cout< cout< return 0; } 实验五运算符重载 一、实验目的 1、进一步了解运算符重载的概念和使用方法。 2、掌握几种常用的运算符重载的方法。 二、实验要求 1、定义一个复数类Complex,重载运算法“+”,使之能用于复数的加法运算。将运算符重载为普通函数(非成员、非友元)、成员函数、友元函数。根据要求修改通过函数来实现复数相加的示例,分别编写程序,求两个复数之和。 #include 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< 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=https://www.doczj.com/doc/111281012.html,plex_add(complex2); // 进行两个复数的加运算 complex.display( ); return 0; } //16.14+10i //普通函数(非成员、非友元) #include 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<<"("< 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 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(); 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<<"("< 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 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()