1、WIN32 控制台的若干程序:1. 源文件中:这个例子着重注意枚举类型的联合类型的应用。枚举和联合都是类的成员变量。#include using namespace std;const float PI=3.1416;#include class datatypeprivate:enumcharacter,interger,float_pointdtype;unionchar c;int i;float f;public:datatype(char cc)c=cc;dtype=character;datatype(int ii)i=ii;dtype=interger;datatype(floa
2、t ff)f=ff;dtype=float_point;void printf();void datatype:printf()switch (dtype)case character:coutusing namespace std;void swap(int temp=x;x=y;y=temp;void main()int a=10,b=20;coutusing namespace std;void swap(int x,int y)int temp;temp=x;x=y;y=temp;void main()int a=10,b=20;coutusing namespace std;void
3、 swap(int x,int y)coutusing namespace std;class boat;class carprivate: int weight;public:car(int j) weight=j;friend int totalweight(car ;class boatprivate: int weight;public: boat(int j)weight=j;friend int totalweight(car ;int totalweight(car void main()car c1(4);boat b1(5);coutusing namespace std;c
4、lass Pointpublic:Point();Point(int x,int y);Point();void move(int newx,int newy);int getx() const return x;int gety() const return y;static void showcount();private:int x,y;Point:Point()x=y=0;coutusing namespace std;void main()int *p; 应该为: int *p=new int;错误原因:指针没有初始化,也就是没有指向某个确定的内存单元,它指向内存中的一个随机地址,给
5、这个随机地址赋值时是非常危险的。 (也就在在使用指针的时候,必须要初始化。 )*p=9;cout#include using namespace std;class Employeepublic:Employee(char *n,char *s,char *c,char *y)strcpy(name,n);strcpy(shreet,s);strcpy(city,c);strcpy(youbian,y);void change_name(char *newname)strcpy(name,newname);void display()coutusing namespace std;class
6、Shapepublic:Shape()Shape()virtual float GetArea()return -1;class Circle: public Shape public:Circle(float radius):itsRadius(radius)Circle()float GetArea()return 3.14*itsRadius*itsRadius;private:float itsRadius;class Rectangle: public Shape public:Rectangle(float len,float width):itsLength(len),itsWi
7、dth(width)Rectangle()virtual float GetArea()return itsWidth*itsLength; private:float itsWidth;float itsLength;class Square: public Rectangle public:Square(float len);Square();Square:Square(float len):Rectangle(len,len)int main()Shape *sp;sp=new Circle(5);coutGetArea()GetArea()GetArea()using namespac
8、e std;class objectprivate:int Weight;public:object()coutusing namespace std;class BaseClasspublic:void fn1();void fn2();void BaseClass:fn1()coutfn1();pBaseClass-fn2();pDerivedClass-fn1();pDerivedClass-fn2();return 0;9.#include #include using namespace std;class Documentpublic:Document();Document(cha
9、r *name);char *Name;void PrintNameOf();Document:Document(char *name)Name=new charstrlen(name)+1;strcpy(Name,name);void Document:PrintNameOf()coutNameendl;class Book:public Document/补充程序 要在子类中向基类传递参数啊!public:Book(char *name, int pageCnt):Document(name),pageCount(pageCnt)private:int pageCount;void main()Document a(“Document1“);Book b(“Book1“,100);b.PrintNameOf();