VC++基础知识点举例Word格式文档下载.docx
《VC++基础知识点举例Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《VC++基础知识点举例Word格式文档下载.docx(21页珍藏版)》请在冰豆网上搜索。
Tdate();
virtual~Tdate();
};
stdafx.h"
voidmain()
Tdatea,b,c;
a.set(4,12,1996);
b.set(3);
c.set(8,10);
a.print();
b.print();
c.print();
2.重载成员函数举例
cube.h"
classcube
intheight,width,depth;
intvolume(intht,intwd)
{
returnht*wd;
}
intvolume(intht,intwd,intdp)
height=ht;
width=wd;
depth=dp;
returnheight*width*depth;
cube();
virtual~cube();
//#include"
//#include<
//usingnamespacestd;
cubecube1;
cout<
cube1.volume(10,20)<
cube1.volume(10,20,30)<
3.含有对象成员的类的构造函数和析构函数的调用顺序举例
A.h"
B.h"
classB
B();
virtual~B();
B:
:
B()
DefineaB"
~B()
DestructingaB"
classA
BobjectB;
A();
virtual~A();
A:
A()
DefineaA"
~A()
DestructingaA"
AobjectA;
4.对象数组
DefineaObject"
AObject[5];
5.类的继承与派生
Person.h"
Teacher.h"
Student.h"
classPerson
protected:
charname[20],sex[20];
intage;
Person(char*na,char*se,intag)
strcpy(name,na);
strcpy(sex,se);
age=ag;
ThisistheclassofPerson"
voidShow()
Name:
name<
endl
<
Sex:
sex<
Age:
age<
virtual~Person()
DestructingtheclassofPerson"
classTeacher:
publicPerson
charsubject[20];
intsalary;
Teacher(char*na,char*se,intag,char*su,intsa):
Person(na,se,ag)
strcpy(subject,su);
salary=sa;
ThisistheclassofTeacher"
Person:
Show();
Subject:
subject<
Salary:
salary<
virtual~Teacher()
DestructingtheclassofTeacher"
classStudent:
charnumber[20],clvss[20];
Student(char*na,char*se,intag,char*nu,char*cl):
strcpy(number,nu);
strcpy(clvss,cl);
ThisistheclassofStudent"
Number:
number<
Class:
clvss<
virtual~Student()
DestructingtheclassofStudent"
PersonPer("
张三"
"
男"
26);
TeacherTea("
李四"
31,"
数学"
3000);
StudentStu("
王六"
女"
18,"
1996302856"
电子仪器
(1)班"
);
Per.Show();
Tea.Show();
Stu.Show();
6.友元函数的定义与使用
Score.h"
classStudent
charname[10],number[10];
friendvoidShow(Student&
st)
st.name<
st.number<
Student(char*na,char*nu)
virtual~Student();
classScore
intmat,phy,eng;
friendvoidShowAll(Student&
Score*);
Score(intm,intp,inte):
mat(m),phy(p),eng(e)
{}
virtual~Score();
StudentTanlin("
ScoreSscore(82,90,81);
ShowAll(Tanlin,&
Sscore);
voidShowAll(Student&
st,Score*sc)
Show(st);
Mathematics:
sc->
mat<
Phyics:
phy<
English:
eng<
7.静态数据的使用
staticinti;
A(){i++;
intlist(){returni;
intA:
i=0;
//静态成员的初始化应放在某一个源文件(.cpp)中
Aa1,a2,a3;
a1.list()<
a2.list()<
a3.list()<
8.用成员函数重载运算符
RMB.h"
classRMB
intyuan,jf;
RMB(inty,intj):
yuan(y),jf(j)
RMBoperator+(RMB&
r)
intj=jf+r.jf;
inty=yuan+r.yuan;
RMBresult(y,j);
returnresult;
RMBoperator++()
{
if(jf>
=100){
jf-=100;
yuan++;
return*this;
voidshow(){cout<
(yuan+jf/100.0)<
RMBObj1(1,60);
RMBObj2(2,50);
RMBObj3(0,0);
Obj3=Obj1+Obj2;
++Obj3;
Obj3.show();
9.引用虚函数举例
Base.h"
Derive1.h"
Derive2.h"
classBase
voidwho()
{cout<
ThisistheclassofBase"
classDerive1:
publicBase
ThisistheclassofDerive1"
classDerive2:
ThisistheclassofDerive2"
BaseObject,*p;
Derive1Object1;
Derive2Object2;
p=&
Object;
p->
who();
Object1;
((Derive1*)p)->
Object2;
((Derive2*)p)->
Object1.who();
Object2.who();
10.虚函数举例
virtualvoidwho()
注意比较实验9与实验10运行结果的不同之处
11.计算三角形的面积(使用MFC)
math.h>
voidCMFCDlg:
OnButton1()
//TODO:
Addyourcontrolnotificationhandlercodehere
UpdateData(TRUE);
floats;
s=(m_a+m_b+m_c)/2;
m_result=(float)sqrt(s*(s-m_a)*(s-m_b)*(s-m_c));
UpdateData(FALSE);
OnButton2()
CDialog:
OnOK();
具体实现过程
打开VC++6.0应用程序
1.建立该应用程序
文件→新建→工程→MFCAppWizard(exe)→输入工程名称→点击确定
选择“基本对话框”,并点击完成
2.程序界面的设计
可清除“确定”、“取消”等自动生成按钮,根据需要添加“静态文本框”、“编辑框”、“命令按钮”等
3.给成员变量添加说明
选择IDC_EDIT1,点击AddVariable
设定成员变量名,选择成员变量类型
用相同的方法给编辑框2,3,4设定成员名和类型
4.给控件添加链连接代码
在对话框中按下Ctrl键的同时双击要链接代码的控件(本题中为“计算”,“退出”按钮),并定义函数体的内容(实验代码上文已给出)
5.调试运行实现该程序