当前位置:文档之家› 实验报告3 继承与多态

实验报告3 继承与多态

};
class Crectangle: public Cshape
{
public:
void virtual Display( void)
{
cout<<"Crectangle"<<endl;
}
};
class Ctriangle: public Cshape
{
void virtual Display( void)
3、理解多态的概念、运算符重载的概念、虚函数的作用;
4、学习实现多态,掌握几种常用运算符的重载;;
5、掌握虚函数的定义和使用方法;
6、简单面向对象程序的编写
实验内容及具体步骤:
内容1:函数重载
(1)构造函数重载,运行以下程序,
#include <iostream.h>
class COMPLEX
{ public:
1.Chin a china
2.National Computer
3.Examination Rank
4.swust swust
#include<string.h>
class CString
{
public:
friend int main();
CString();
CString(CString &s);
void display();
~CString();
private:
char *str;
};
CString::CString()
{
str=NULL;
}
CString::CString(CString &s)
{
str=new char[strlen(s.str)+1];
if(str!=NULL)
{
strcpy(str,s.str);
this是自身的地址,但是*this就是自身了.是按值回返了.如果函数的回返值是带有&号的,那么回返值就是引用了
构造出五个公共类函数,此函数在运行时没有出现问题,但是我经过仔细阅读理解程序的过程中,主要对这个this的用途和用法在网上查阅学习了下,一种情况就是,在类的非静态成员函数中返回类对象本身的时候,直接使用return *this;另外一种情况是当参数与成员变量名相同时,如this->n = n(不能写成n = n)。
{OperClass A; x ++; A.x=x; return A;}
void OperClass::display ( )
{ cout <<“x=“<<x<<endl; }
void main ( )
{
OperClass X,Y ;
X.display( );
++X;
Y=++X;
Y++.display( );
CString(char *s);
friend bool operator >(CString s1,CString s2);
friend bool operator <(CString s1,CString s2);
friend bool operator ==(CString s1,CString s2);
cout<<"color="<<d.getColor( )<<endl;
}
此程序中父类Mammal有两个私有类变量,三个公共类函数,Dog类是Mammal类的子类,从而调用子类实现了函数的继承;此程序代码在运行的过程中没有遇到任何的错误;从函数中我更深的了解到继承的概念以及如何使用,继承是存在于面向对象程序设计中的两个类之间的一种关系,是面向对象程序设计方法的一个重要手段,通过继承可以更有效地组织程序结构,明确类间的关系,充分利用已有的类来完成更复杂、更深入的开发。
计算机课程实验报告
2012—2013学年度第一学期
系别:数学与计算机科学学院
实验课程
C++面向对象程序设计
班级
级计算机科学与技术
学号
11
姓名
蔡兴明
指导教师
马学梅
实验题目
继承与多态(一)
日期
2012-9-29
实验目的
及要求
1、理解继承在面向对象程序中的重要作用、继承和派生的概念;
2、掌握通过继承派生一个新类的方法;
(2)运算符重载:以下程序要求实现运算符++的前置和后置重载,但实际上并没有实现。试分析原因,并作出修改,以实现此功能。
#include <iostream.h>
class OperClass
{ int x;
public:
OperClass( );
OperClass operator ++( );
OperClass operator ++(int );
return *this;
}
int main ( )
{
COMPLEX c1(1,2) ;
COMPLEX c2(2) ;
COMPLEX c3(c1) ;
c3.print ( );
c2.add(c1);
c3. subs(c2);
c3.print( );
return 0;
}
给出输出结果,分析this的用途.在此程序的基础上实现运算符”+”和”-“的重载
double real, image;
};
COMPLEX:: COMPLEX ( double r, double i)
{ real = r; image =i; return; }
COMPLEX:: COMPLEX( const COMPLEX & other )
{ real = other.real; image =other.image; return;}
{
if(strcmp(s1.str,s2.str)==0)
return true;
else
return false;
}
int main()
{
void Update1(CString &s1,CString &s2);
CString s1("Chin a"),s2("National"),s3("Examination");
{
this->age=age;
this->weight=weight;
}
int Mammal::getAge( )
{
return age;
}
int Mammal::getWeight( )
{
return weight;
}
class Dog : public Mammal //class Dog is a derived class of Mammal
COMPLEX (double r=0,double i=0);
COMPLEX(const COMPLEX & other);
void print( );
COMPLEX add(const COMPLEX & other);
COMPLEX subs(const COMPLEX & other);
protected:
}
内容2:重载<,>,==:
设计字符串类String,用来存放不定长的字符串,重载运算符“==”,“>”,“<”,用于两个字符串的大于、小于和等于的比较运算。
提示:(1)属于双目运算符,在重载双目运算符时,函数中应该有两个参数。
(2)String类成员变量应为字符型指针;
(3)多个构造函数的定义;
测试数据:
{
cout<<"Ctiangle"<<endl;
}
};
class Cellipse :public Cshape
{
public: void virtual Display(void)
{
cout<<"Cellipse"<<endl;
}
};
void main()
{
Cshape obshape;
Cellipse obEllipse;
{
hairColor=color;
}
int Dog::getColor( )
{
return hairColor;
}
void main( )
{
Dog d(8, 20, 1);
cout<<"age="<<d.getAge( )<<endl;
cout<<"weight="<<d.getWeight( )<<endl;
}
}
CString::CString(char *s)
{
str=new char[strlen(s)+1];
if(str!=NULL)
{
strcpy(str,s);
}
}
bool operator >(CString s1,CString s2)
{
if(strcmp(s1.str,s2.str)>0)
}
else
{
cout<<"s1等于s2";
cout<<endl;
相关主题