c++
2010年04月14日
http://blog.sina.com.cn/wangzhaofei6666
书面作业:题目1和题目5
上机作业:题目1、2、3、4、5
题目1:自定义一个简单的复数类complx,如下:
class complex
{
double real;//复数的
double imag;//复数的虚部
public:
complex(double r=0,double i=0);
void inCom();
complex addCom(complex c2);//调用者对象与对象c2相加
complex subCom(complex c2);
complex mulCom(complex c2);
void outCom();//输出调用者的有关数据(各分量)
};
请完成各个成员函数,并编制主函数,说明complex类对象,
对定义的各个成员函数进行调用,
以验证它们的正确性。
提示:(a+bi)(c+di)=ac+adi+bci+bdi2=(ac-bd)+(ad+bc)i
题目2 改错题目:
#include
class sam
{
int a;
int b;
public:
sam()
{
a=1;
b=1;
}
sam(int a,int b)
{
a=a;
b=b;
}
sam(int a,b=1)
{
a=a;
}
~sam()
{
}
void plus(sam p)
{
sam c;
c.a=a+p.a;
c.b=b+p.b;
return c;
}
void print()
{
cout
class mycla
{
int a,b;
public:
void set(int x,int y)
{
a=x;
b=y;
}
mycla()
{
coutnew mycla[3];
for(int i=0;inew mycla;
p1->set(90,88);
for(i=0;idelete p1;
for( i=0;idelete []pa;
}
题目4:阅读程序,写出运行结果:
#include
class sam{
public:
static int m;
sam(){m++;}
sam(int n){m=n;}
static void testm(){m++;}
};
int sam::m=0;
int main()
{
sam A;
sam B(3);
A.testm();
sam::testm();
cout
class mycla
{
int arr[10];//存放数据
public:
void myIn();//输入数据的成员函数
void myOut();//输出数据的成员函数
};
实现成员函数,编制主程序,调用成员函数,实现输入10个整数,而后反序输出。
补充:分数类参考答案:
#include
#include
class fraction
{
int above; //分子
int below; //分母
int gcd();//私有成员函数,负责求出分子、分母的最大公约数
public:
fraction(int,int); //
fraction(int);
fraction();
fraction add(fraction &);
fraction sub(fraction );
void print();
};
fraction::fraction(int a,int b)
{
above=a;
if(b!=0) //
below=b;
else
below=1;
}
fraction::fraction(int a)
{
above=a;below=1;
}
fraction::fraction()
{
above=0;
below=1;
}
int fraction::gcd()
{
if(below学习用Visual C++6.0编写标准的C++控制台程序
实验内容:
例题:1-1,1-2;
课后习题:1.4,1.6,1.7
1.4 模仿本课件中的例题,编写下列C++语句:
a)输出信息“Enter two numbers”。
b)将输入的两个整数读入变量a与b中
c)将变量a和b的乘积赋给变量c
d)打印“a*b=”,加上变量c的值。
1.6下列代码打印出什么结果?
cout
main()
{
int a,b,c;
cout>a>>b;
c=sum(a,b);
cout
int &f(int &i)
{
i+=10;
return i;
}
void main()
{
int k=0;
int &m=f(k);
cout总结经验和体会。
3、(2题和3题的源程序、测试数据和实验结果)写到实验报告上。
实验4名称:类和对象的使用
实验目的:
理解类的复合的含义,掌握子对象的初始化方式;
理解this指针的实质,掌握this指针的用法;
学习友元函数和友元类的使用;
掌握const特性,理解常对象和常成员函数的作用;
掌握自定义头文件的方法,学会建立和调试多文件程序。
实验内容:
1、定义复数类Complex,并使用友元函数实现复数的加法、减法、乘法,所有函数都返回Complex对象。
2、对如下圆柱类Column的定义:
class Column{
public:
Column(double x,double y,double r,double h);
~Column();
double area();
double volume();
private:
double height;
double radius;
double x;
double y;
};
要求:
a)实现类Column中的四个成员函数;
b)增加一个友元函数,实现从键盘读取4个double类型的数据对类Column的对象进行赋值的功能;
c)增加一个友元函数,实现将类Column的对象输出到屏幕的功能,输出信息包括对象的
基本信息以及对对象的面积和体积。
3、对于下面定义的类Test:
class Test
{
private:
int a;
double b;
};
要求:
a)为类Test的每个数据成员增加一个set函数,并使这些set函数都可以被连续调用;
b)为类Test的每个数据成员增加一个get函数。
4、阅读程序,写出执行结果:
#include
class A
{
const int const_v;
static const int static_const_v;
public:
A(int i);
void print();
};
const int A::static_const_v=8;
A::A(int i):const_v(i)
{}
void A::print()
{
cout总结经验和体会。
3、(2题和3题的源程序、测试数据和实验结果)写到实验报告上。
实验名称:运算符重载
实验目的:
掌握运算符重载的概念;
掌握使用friend重载运算符的方法;
掌握使用成员函数重载运算符的方法。
实验内容:
1、分别用成员函数和友元函数重载运算符,使适用于整型的运算符+、-、*、/ 也适用于分数运算。
要求:
(1)输出结果是最简分数(可以是带分数);
(2)分母为1时,只输出分子。
2、利用运算符重载定义集合类,达到使集合的运算符合人们的习惯的目的。
“借用”一批运算符,(如 *,+, -,&, operator &(int m,set s); //&:判断元素m是否为集合s的成员
friend bool operator==(set s1,set s2); //==:判断两个集合是否相等
friend set operator+(set s,int elem); //+:将某元素elem添加到某集合s中
friend set operator+(set s1,set s2); //+:求两个集合的并集合
friend set operator-(set s, int elem ); //-:从某集合s中删除元素s
friend set operator*(set s1, set s2 ); //*:求两个集合的交集合
friend bool operator总结经验和体会。
3.完成实验报告。
2010年04月14日
http://blog.sina.com.cn/wangzhaofei6666
书面作业:题目1和题目5
上机作业:题目1、2、3、4、5
题目1:自定义一个简单的复数类complx,如下:
class complex
{
double real;//复数的
double imag;//复数的虚部
public:
complex(double r=0,double i=0);
void inCom();
complex addCom(complex c2);//调用者对象与对象c2相加
complex subCom(complex c2);
complex mulCom(complex c2);
void outCom();//输出调用者的有关数据(各分量)
};
请完成各个成员函数,并编制主函数,说明complex类对象,
对定义的各个成员函数进行调用,
以验证它们的正确性。
提示:(a+bi)(c+di)=ac+adi+bci+bdi2=(ac-bd)+(ad+bc)i
题目2 改错题目:
#include
class sam
{
int a;
int b;
public:
sam()
{
a=1;
b=1;
}
sam(int a,int b)
{
a=a;
b=b;
}
sam(int a,b=1)
{
a=a;
}
~sam()
{
}
void plus(sam p)
{
sam c;
c.a=a+p.a;
c.b=b+p.b;
return c;
}
void print()
{
cout
class mycla
{
int a,b;
public:
void set(int x,int y)
{
a=x;
b=y;
}
mycla()
{
coutnew mycla[3];
for(int i=0;inew mycla;
p1->set(90,88);
for(i=0;idelete p1;
for( i=0;idelete []pa;
}
题目4:阅读程序,写出运行结果:
#include
class sam{
public:
static int m;
sam(){m++;}
sam(int n){m=n;}
static void testm(){m++;}
};
int sam::m=0;
int main()
{
sam A;
sam B(3);
A.testm();
sam::testm();
cout
class mycla
{
int arr[10];//存放数据
public:
void myIn();//输入数据的成员函数
void myOut();//输出数据的成员函数
};
实现成员函数,编制主程序,调用成员函数,实现输入10个整数,而后反序输出。
补充:分数类参考答案:
#include
#include
class fraction
{
int above; //分子
int below; //分母
int gcd();//私有成员函数,负责求出分子、分母的最大公约数
public:
fraction(int,int); //
fraction(int);
fraction();
fraction add(fraction &);
fraction sub(fraction );
void print();
};
fraction::fraction(int a,int b)
{
above=a;
if(b!=0) //
below=b;
else
below=1;
}
fraction::fraction(int a)
{
above=a;below=1;
}
fraction::fraction()
{
above=0;
below=1;
}
int fraction::gcd()
{
if(below学习用Visual C++6.0编写标准的C++控制台程序
实验内容:
例题:1-1,1-2;
课后习题:1.4,1.6,1.7
1.4 模仿本课件中的例题,编写下列C++语句:
a)输出信息“Enter two numbers”。
b)将输入的两个整数读入变量a与b中
c)将变量a和b的乘积赋给变量c
d)打印“a*b=”,加上变量c的值。
1.6下列代码打印出什么结果?
cout
main()
{
int a,b,c;
cout>a>>b;
c=sum(a,b);
cout
int &f(int &i)
{
i+=10;
return i;
}
void main()
{
int k=0;
int &m=f(k);
cout总结经验和体会。
3、(2题和3题的源程序、测试数据和实验结果)写到实验报告上。
实验4名称:类和对象的使用
实验目的:
理解类的复合的含义,掌握子对象的初始化方式;
理解this指针的实质,掌握this指针的用法;
学习友元函数和友元类的使用;
掌握const特性,理解常对象和常成员函数的作用;
掌握自定义头文件的方法,学会建立和调试多文件程序。
实验内容:
1、定义复数类Complex,并使用友元函数实现复数的加法、减法、乘法,所有函数都返回Complex对象。
2、对如下圆柱类Column的定义:
class Column{
public:
Column(double x,double y,double r,double h);
~Column();
double area();
double volume();
private:
double height;
double radius;
double x;
double y;
};
要求:
a)实现类Column中的四个成员函数;
b)增加一个友元函数,实现从键盘读取4个double类型的数据对类Column的对象进行赋值的功能;
c)增加一个友元函数,实现将类Column的对象输出到屏幕的功能,输出信息包括对象的
基本信息以及对对象的面积和体积。
3、对于下面定义的类Test:
class Test
{
private:
int a;
double b;
};
要求:
a)为类Test的每个数据成员增加一个set函数,并使这些set函数都可以被连续调用;
b)为类Test的每个数据成员增加一个get函数。
4、阅读程序,写出执行结果:
#include
class A
{
const int const_v;
static const int static_const_v;
public:
A(int i);
void print();
};
const int A::static_const_v=8;
A::A(int i):const_v(i)
{}
void A::print()
{
cout总结经验和体会。
3、(2题和3题的源程序、测试数据和实验结果)写到实验报告上。
实验名称:运算符重载
实验目的:
掌握运算符重载的概念;
掌握使用friend重载运算符的方法;
掌握使用成员函数重载运算符的方法。
实验内容:
1、分别用成员函数和友元函数重载运算符,使适用于整型的运算符+、-、*、/ 也适用于分数运算。
要求:
(1)输出结果是最简分数(可以是带分数);
(2)分母为1时,只输出分子。
2、利用运算符重载定义集合类,达到使集合的运算符合人们的习惯的目的。
“借用”一批运算符,(如 *,+, -,&, operator &(int m,set s); //&:判断元素m是否为集合s的成员
friend bool operator==(set s1,set s2); //==:判断两个集合是否相等
friend set operator+(set s,int elem); //+:将某元素elem添加到某集合s中
friend set operator+(set s1,set s2); //+:求两个集合的并集合
friend set operator-(set s, int elem ); //-:从某集合s中删除元素s
friend set operator*(set s1, set s2 ); //*:求两个集合的交集合
friend bool operator总结经验和体会。
3.完成实验报告。