程序清单2-1: 加注释的C++程序
|
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return TRUE;
}
|
程序清单2-2:C与C++的输入输出比较
|
(1)#include “stdio.h”
(2)#include “iostream.h”
void main() void main()
{ int a,b,sum; { int a,b,sum;
printf(“input a b:”); cout<<“input a b:”;
scanf(“%d%d” ,&a,&b);
cin>>a>>b;
sum=a+b;
sum=a+b;
printf(“sum is %d/n”,sum);
cout<<”sum is ”<
} }
|
程序清单2-3:引用参数举例
|
#include "iostream.h"
void AutoInc1(int*x);
void AutoInc2(int &y);
void main()
{int a(10);
AutoInc1(&a);
|
程序清单2-4:函数参数默认值举例
|
#include “iostream.h”
void delay(int t)
{
if(t==0) return;
for(int i=0; i
cout<<”i=”<
}
void main()
{
void delay(int=1000); //声明默认值参数
delay(0);
delay(50);
delay (500);
delay();
delay(5000);
}
运行结果为:
i=50
i=500
i=1000
i=5000
|
程序清单2-5:内联函数举例
|
#include "iostream.h"
void main()
{
int a(8),b(16),c;
inline int max(int x,int y);
c=max(a,b);
cout<<"c="<
}
int max(int x,int y)
{
return(x>y?x:y);
}
|
程序清单2-6:Point类的设计(一)
|
class Point
{
int x;
int y;
public:
void set_x(int k){x=k;}
void set_y(int k){y=k;}
int get_x(){return x;}
int get_y(){return y;}
};
|
程序清单2-7:Point类的设计(二)
|
class Point
{
int x;
int y;
public:
void set_x(int k);
void set_y(int k);
int get_x();
int get_y();
};
void Point::set_y(int k)
{
y=k;
}
void Point::set_x(int k)
{
x=k;
}
int Point::get_x()
{
return x;
}
int Point::get_y()
{
return y;
}
|
程序清单2-8:实现Point类应用的main函数
|
#include “iostream.h”
void main()
{
Point p1,*p;
int x,y;
p1.set_x(100);
p1.set_y(100);
p=&p1;
x=p->get_x();
y=p->get_y();
cout<<”x=”<
}
运行结果为:
x=100
y=100
|
析构函数
|
构造函数
|
程序清单2-9:使用拷贝构造函数
|
#include "iostream.h"
class Point{
int x,y;
public:
Point(){};
Point(int a ,int b);
Point(const Point& pa);
int get_x(){return x;}
int get_y(){return y;}
};
Point::Point(int a,int b):x(a),y(b)//实现成员变量赋值
{}
Point::Point(const Point& pa)// 实现对象拷贝
{x=pa.x;
y=pa.y;
}
void main()
{Point p1(100,150); //调用带参构造函数初始化p1
Point p2(p1);
//以p1的值初始化p2
cout<<"x="<
cout<<"y="<
}
运行结果:
x=100
y=150
|
程序清单2-10:实现正方形类及应用
|
#include "iostream.h"
class Square
{
protected:
int len;
public:
Square(){}
Square(int x){len=x;}
int SetLen(int x);
int GetLen(){return len;}
void DrawSquare();
};
int Square::SetLen(int x)
{
if(x<1 ||x>30 )
//检查x的值在1-30之间
return 0;
len=x;
//设置边长
return 1;
}
void Square::DrawSquare()
{ int i,j;
for(i=0;i
{for(j=0;j
cout<<'*';
cout<
}
}
void main()
{ Square s(5);
cout<<"draw the square,len="<
s.DrawSquare();
cout<<"input the new side length:";
int x;
cin>>x;
if (!s.SetLen(x))
cout<<"error, length should be between 1 and 30! ";
else
{cout<<"draw the square,len="<
s.DrawSquare();
}
}
|
继承方式
|
基类成员访问权限
|
派生类访问权限
|
public
(公有继承)
|
public
|
public
|
protected
|
protected
|
|
private
|
不可访问
|
|
protected
(保护继承)
|
public
|
protected
|
protected
|
protected
|
|
private
|
不可访问
|
|
private
(私有继承)
|
public
|
private
|
protected
|
private
|
|
private
|
不可访问
|
程序清单2-11:派生类的实现和应用
|
#include "iostream.h"
class Point
// 定义基类
{
int m_x, m_y;
public:
Point(int x,int y){ m_x=x; m_y=y; }
int get_x(){ return m_x; }
int get_y() { return m_y;}
};
class Circle : public Point
//定义派生类,公有继承
{
double radius;
public:
Circle(int x,int y,double r) : Point(x,y)//派生类没有继承基类的构造函数,而是
{ radius=r;} //通过访问基类的构造函数初始化基类的数据成员。
double Area(void) {return 3.14*radius*radius; } //计算圆面积
int get_centreX() {return get_x();} //调用基类中的成员函数访问基类数据,获得
int get_centreY() {return get_y();} //圆心坐标
};
void main()
{
int x,y;
double r;
cout<<"x="; cin>>x;
cout<<"y="; cin>>y;
cout<<"r="; cin>>r;
Circle c(x,y,r);
cout<<"the centre of the Circle is "<
cout<<"the Area of the Circle is "<
}
运行结果:
x=100
y=100
r=50
the centre of the Circle is 100 and 100
the Area of the Circle is 7850
|
圆类
矩形类
图形类
图2-3 图形类的构成
|
程序清单2-12:多继承示例
|
#include "iostream.h"
const double PI =3.14;
class Circle
//定义基类
{
double radius;
public:
Circle(double r)
{ radius=r;}
double CircleArea(void) {return PI*radius*radius; } //求圆面积
};
class Rectangle //定义基类
{
double length,width;
public:
Rectangle(double x,double y)
{length=x,width=y;}
double RecArea(void){return length*width;} //求矩形面积
};
class Graph:public Circle,public Rectangle //定义多继承派生类
{
public:
Graph(double r,double x,double y):Circle(r),Rectangle(x,y)
{}
void ShowArea(void) //求图形面积,调用基类成员函数
{double TotalArea;
TotalArea= CircleArea()+RecArea();
cout<<"the Area of Graph is "<
}
};
void main()
{
double x,y,r;
cout<<"r="; cin>>r;
cout<<"x="; cin>>x;
cout<<"y="; cin>>y;
Graph g(r,x,y);
g.ShowArea ();
}
运行结果:
r=10
x=20
y=50
the Area of Graph is 1314
|
CA
类公有继承部分
|
CB
类新增加的成员
|
程序清单2-13:派生类与基类的转换
|
#include "iostream.h"
class Sharp
{double x,y;
public :
void SetValue(double i,double j){x=i,y=j;}
double GetX(){return x;}
double GetY(){return y;}
};
class Rectangle:public Sharp
{
public:
void Area(){cout<<"the area of rectangle is "<
};
void main()
{
Sharp *bs;
Rectangle rec;
rec.SetValue(3.0,4.0);
bs=&rec;
//通过地址赋值,使基类指针指向派生类对象
((Rectangle*)bs)->Area(); //基类指针调用派生类的成员函数
}
|
程序清单2-14:虚函数的实例
|
#include "iostream.h"
const double PI =3.1415926;
class Sharp
{protected:
double x,y;
public:
void set_value(double i,double j=0)
{x=i;y=j;}
virtual void Area(){cout<<" this is a sample of virtual fuction/n";}//设置虚函数
};
class Circle:public Sharp
{
public:
void Area(void) {
cout<<"the area of Circle is "<
};
class Rectangle:public Sharp
{
public:
void Area(void){cout<<"the area of Rectanle is "<
};
void main()
{
Sharp *bs;Circle cir;Rectangle rec;
bs=○ bs->set_value(10);bs->Area(); //基类指针访问Circle类成员函数
bs=&rec; bs->set_value(3.5,5.0);bs->Area();//基类指针访问Rectangle类成员函数
}
运行结果:
the area of Circle is 314.159
the area of Rectanle is 17.5
|
程序清单2-15:虚函数的实例
|
class Circle:public Sharp
{
public:
void Area(void) {
Sharp::Area();
cout<<"the area of Circle is "<
};
再次运行这个程序,运行结果为:
this is a sample of virtual fuction
the area of Circle is 314.159
the area of Rectanle is 17.5
|
程序清单2-16:纯虚函数的实例
|
#include
class Point
{
public:
Point(int i=0, int j=0)
{
a0=i; b0=j;
}
virtual void set() = 0;
virtual void draw() = 0;
protected:
int a0, b0;
};
class Line : public Point
{
public:
Line(int i=0, int j=0, int m=0, int n=0):Point(i, j)
{
a1=m; b1=n;
}
void set() { cout<<"Line::set() be called./n"; }
void draw() { cout<<"Line::draw() be called./n"; }
protected:
int a1, b1;
};
class Circle : public Point
{
public:
Circle(int i=0, int j=0, int p=0, int q=0):Point(i, j)
{
a2=p; b2=q;
}
void set() { cout<<"Circle::set() be called./n"; }
void draw() { cout<<"Circle::draw() be called./n"; }
protected:
int a2, b2;
};
void setobj(Point *p)
{
p->set();
}
void drawobj(Point *p)
{
p->draw();
}
void main()
{
Line *line1 = new Line;
Circle *circle1 = new Circle;
drawobj(line1);
drawobj(circle1);
cout<
setobj(line1);
setobj(circle1);
cout<<"/nRedraw the object.../n";
drawobj(line1);
drawobj(circle1);
}
运行结果:
Line::draw( ) be called.
Circle::draw( ) be called.
Line::set( ) be called.
Circle::set( ) be called.
Redraw the object…
Line::draw( ) be called.
Circle::draw( ) be called
|