📄 ex01.cpp
字号:
//掌握c++的基本语法和定义
//结构体到类的演变
#include <iostream.h>
//**************************test1:成员的访问
/*class point
{
public: //在默认的情况下是private类型
//private: //不能够访问私有的成员变量和函数
//protected: //不能够访问保护的成员变量和函数
int a;
int b;
void output()
{
cout<<a<<endl<<b<<endl;
}
};
void main()
{
point pt; //当访问类的成员变量和函数时如果是private和protected就不能访问了
pt.a=0;
pt.b=0;
pt.output();
}*/
//****************************test2:构造函数和析构函数
/*class point
{
public: //在默认的情况下是private类型
//private: //不能够访问私有的成员变量和函数
//protected: //不能够访问保护的成员变量和函数
int a;
int b;
char *pname;
void init()
{
a=0;
b=1;
}
point() //注意构造函数不能加void,构造函数在运行时首先自动运行
{
pname=new char[20]; //构造函数里定义的空间
a=0;
b=1;
}
void output()
{
cout<<a<<endl<<b<<endl;
}
~point()
{
delete[] pname;//析构函数里释放空间
}
};
void main()
{
point pt; //当访问类的成员变量和函数时如果是private和protected就不能访问了
//pt.init();
pt.output();
}*/
//*******************************test3:函数的重载:两个构造函数
/*class point
{
public: //在默认的情况下是private类型
//private: //不能够访问私有的成员变量和函数
//protected: //不能够访问保护的成员变量和函数
int a;
int b;
char *pname;
point(int x,int y) //两个构造函数的时候其中一个要有参数
{
b=x+y;
}
point() //注意构造函数不能加void,构造函数在运行时首先自动运行
{
pname=new char[20]; //构造函数里定义的空间
a=0;
b=1;
}
void output()
{
cout<<a<<endl<<b<<endl;
}
~point()
{
delete[] pname;//析构函数里释放空间
}
};
void main()
{
point pt(1,4); //当函数重载的时候要在这给构造函数复值 //当访问类的成员变量和函数时如果是private和protected就不能访问了
//pt.init();
pt.output();
}
*/
//************************this指针
class point
{
public: //在默认的情况下是private类型
//private: //不能够访问私有的成员变量和函数
//protected: //不能够访问保护的成员变量和函数
int a;
int b;
char *pname;
point() //注意构造函数不能加void,构造函数在运行时首先自动运行//重载时不执行
{
// pname=new char[20]; //构造函数里定义的空间
a=0;
b=1;
}
point(int x,int y) //两个构造函数的时候其中一个要有参数
{
a=0;
b=x+y;
}
void init(int m,int n)
{
// this->a=m; //this=&pt
(*this).a=m;
this->b=n;
}
void output()
{
cout<<a<<endl<<b<<endl;
}
~point()
{
// delete[] pname;//析构函数里释放空间
}
};
void main()
{
point pt(1,4); //当函数重载的时候要在这给构造函数复值 //当访问类的成员变量和函数时如果是private和protected就不能访问了
pt.init(8,9);
pt.output();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -