开发工具及环境:PC机一套 Visual Studio 2010
实验要求:
1.硬件基本配置:Intel PentiumIII以上级别的CPU,大于64MB的内存。
2.软件要求:Window 2000操作系统,Visual Studio 6.0或更高版本开发环 境。
3.实验学时:2学时
4.实现实验内容中的题目。
5.写实验报告
实验目的:
1. 掌握类的构造函数和析构函数的概念和使用方法
2. 理解掌握构造函数的重载
3. 理解掌握 this 指针的作用和用法
4. 掌握对象数组、对象指针的定义和使用方法
5. 理解掌握拷贝构造函数的定义和使用
实验内容:
1、分析下面的程序,回答问题。
#include <iostream>
using namespace std;
class CPoint
{
public:
void Set(int x,int y);
void Print();
private:
int x;
int y;
};
void CPoint::Set(int x,int y)
{
This->x = x;
This->y = y;
}
void CPoint::Print()
{
cout<<"x="<<x<<",y="<<y<<endl;
}
void main()
{
CPoint pt;
pt.Set(10,20);
pt.Print();
}
问题一:以上程序编译能通过吗,试解释该程序?
答:在devc++不能,在vs中得到两个负数。 传入两个数,输出这两个数的值
问题二:以上程序的运行结构是否正确,如果不正确,试分析为什么,应该如何改正?
答:不正确,void->int,末尾添加return 0;。
2、阅读程序回答问题。
#include <iostream>
using namespace std;
class CPerson
{
public:
void Print();
private:
CPerson();
private:
int age;
char *name;
};
CPerson::CPerson()
{
}
void CPerson::Print()
{
cout<<"name="<<name<<",age="<<age<<endl;
}
void main()
{
CPerson ps(23,"张三");
ps.Print();
}
问题一:以上程序存在三处大错误,在不改变主函数内容的前提下,试改正该程序。
答:(1)类中CPerson的构造函数定义为public:
(2)char *name 改为 string name;
(3)构造函数加入参数:CPerson::CPerson(int a, string n):age(a), name(n) {}
3、类Person的定义如下,请实现该类,并在主函数中创建对象obj,然后使用构造函数为
obj赋予初始值(内容自定)。
class Person
{
public:
Person(char *xname,int xage,int xsalary,char *xtel);
void disp();
private:
char name[10];
int age;
int salary;
char tel[8];
};
4、分析并比较下列程序运行的结果。
程序一:
#include<iostream.h>
using namespace std;
class smallone
{
public:
smallone(int sma)
{ x=sma;
cout<<"sm constr:"<<sma<<"\n";
}
int getX()
{
return x;
}
~smallone()
{
cout<<"调用了析构函数释放对象!\n";
}
private :
int x;
};
void fn(int n)
{ smallone sm(n);
cout<<"in function fn with n="<<n<<" sm.x="<<sm.getX()<<endl;
}
int main()
{ fn(10);
fn(20);
return 0;
}
程序二:
#include<iostream.h>
using namespace std;
class smallone
{
public:
smallone(int sma)
{ x=sma;
cout<<"sm constr:"<<sma<<"\n";
}
int getX()
{
return x;
}
~smallone()
{
cout<<"调用了析构函数释放对象!\n";
}
private :
int x;
};
void fn(int n)
{ static smallone sm(n);
cout<<"in function fn with n="<<n<<" sm.x="<<sm.getX()<<endl;
}
int main()
{ fn(10);
fn(20);
return 0;
}
5、建立一个对象数组,内放5个学生的数据(学号、成绩),定义一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。其中将max函数作为类的友元函数。
#include <iostream>
using namespace std;
class Student
{
public:
int num;
int score;
Student(int n, int s):num(n), score(s) {}
friend void max(Student *);
};
void max (Student *p)
{
for(int i = 0; i< 5; i++){
if(p->score > (p+i)->score){
p = p + 1;
}
}
cout << "最高成绩:" << p->score << endl;
cout << "ta的学号是:" << p->num << endl;
}
int main ()
{
Student a[5] = {
Student(10010,95),
Student(10011,91),
Student(10012,90),
Student(10013,99),
Student(10014,89)
};
Student *p = a;
max(p);
return 0;
}
6、设计实现一个CPoint类,满足以下要求:
a. 该类包含两个整型成员变量x(横坐标)和y(纵坐标),以及一个输出函数Print()用来输出横坐标和纵坐标,要求不可以在类的外部直接访问成员变量;
b.可以采用没有参数的构造函数初始化对象,此时的成员变量采用默认值0;
c.可以采用直接输入参数的方式来初始化该类的成员变量;
d.可以采用其它的CPoint对象来初始化该类的成员变量;
e.设计一个主函数来测试以上功能。
#include <iostream>
using namespace std;
class CPoint
{
private:
int x;
int y;
public:
CPrint(){
x = 0;
y = 0;
}
void set();
void Print();
};
void CPoint::set()
{
cout << "please input x and y:" << endl;
cin >> x >> y;
}
void CPoint::Print()
{
cout << "(" << x << "," << y << ")" << endl;
}
int main ()
{
CPoint point;
point.set();
point.Print();
return 0;
}
7、声明一个类模板,利用它分别实现两个整数、浮点数和字符的比较,求出大数和小数。
#include <iostream>
using namespace std;
template <class T>
class Compare
{
public:
Compare(T a, T b):x(a), y(b) {}
T f_compare()
{
if(x > y){
cout << "the max is : " << x << endl;
cout << "the min is : " << y << endl;
cout << endl;
}
else{
cout << "the max is : " << y << endl;
cout << "the min is : " << x << endl;
cout << endl;
}
}
private:
T x, y;
};
int main ()
{
Compare<int> com1(1,2);
com1.f_compare();
Compare<float> com2(2.2, 4.3);
com2.f_compare();
Compare<char> com3('a', 'v');
com3.f_compare();
return 0;
}
8、实现下列类模板
template <typename T>
class MyStack
{
public:
MyStack(int size);
~MyStack();
bool stackEmpty();//判空
bool stackFull();//判满
void clearStack();//清空
int stackLength();//长度
bool push(T elem);//压栈
bool pop(T &elem);//出栈
bool stackTop(T &elem);//返回栈顶
void stackTranverse();//遍历栈
private:
T *m_pStack;//栈指针
int m_iSize;//栈容量
int m_iTop;//栈顶
};
template <typename T>
MyStack<T>::MyStack(int size)
{
m_iSize = size;
m_pStack = new T[m_iSize];
m_iTop = 0;
}
template <typename T>
MyStack<T>::~MyStack()
{
delete m_pStack;
m_pStack = NULL;
}
template <typename T>
bool MyStack<T>::stackEmpty() //判空
{
return m_iTop == 0 ? true : false;
}
template <typename T>
bool MyStack<T>::stackFull()//判满
{
return m_iTop == m_iSize ? true : false;
}
template <typename T>
int MyStack<T>::stackLength()//栈长度
{
return m_iTop;
}
template <typename T>
void MyStack<T>::clearStack()//清空
{
m_iTop = 0;
}
template <typename T>
bool MyStack<T>::push(T elem) //压栈
{
if(stackFull())
{
return false;
}
else{
m_pStack[m_iTop++]=elem;
return true;
}
}
template <typename T>
bool MyStack<T>::pop(T & elem) //出栈
{
// if(!stackEmpty()){
// elem=m_pStack[--m_iTop];
// return true;
// }else{
// return false;
// }
if(stackEmpty())
{
return false;
}
else {
elem = m_pStack[m_iTop - 1];
return true;
}
}
template <typename T>
bool MyStack<T>::stackTop(T &elem) //返回栈顶元素
{
if(stackEmpty())
{
return false;
}
else {
elem = m_pStack[m_iTop - 1];
return true;
}
}
template <typename T>
void MyStack<T>::stackTranverse() //遍历栈
{
int i = 0;
for(i = 0; i < m_iTop; i++){
cout << m_pStack[i] << endl;
}
}
并在main()中进行测试
int main() {
MyStack<int> *pStack = new MyStack<int>(5);
pStack->push(1);//坐标点入栈
pStack->push(2);
pStack->push(3);
pStack->push(4);
pStack->stackTranverse();//遍历栈
int t;
pStack->pop(t);//出栈
cout <<"弹出的t为:"<< t ;
cout << "长度:" << pStack->stackLength()<<endl;
pStack->clearStack();//清空栈
pStack->stackTranverse();
delete pStack;
pStack = NULL;
return 0;
}