cin ,cout分别对应C语言中的scanf,printf,但是比scanf,printf灵活,可以自动的去匹配输出格式。
cin要与>> 一起用,cout要与<< 一起用。
endl 相当于C中的"/n"
cerr是C++ 的标准错误输出函数。
#include "stdafx.h" #include <iostream> using namespace std;
struct Point { int x; int y;
};
void main() { Point pt; pt.x = 5; pt.y = 5; cout<< pt.x << endl<< pt.y<<endl; } |
#include "stdafx.h" #include <iostream> using namespace std;
struct Point { int x; int y; void output() { cout<< x << endl<< y<<endl; } };
void main() { Point pt; pt.x = 5; pt.y = 5; pt.output(); } |
调用结构体本身的函数去打印结构体的成员, 在C语言中结构体中不允许有函数,C++中是可以的。
#include "stdafx.h" #include <iostream> using namespace std;
//struct Point class Point { public: int x; int y; void output() { cout<< x << endl<< y<<endl; } }; //这里要结束符
void main() { Point pt; pt.x = 5; pt.y = 5; pt.output(); //cout<< pt.x << endl<< pt.y<<endl; } |
C++中的结构体跟类是可以通用的,区别就是访问的修饰符上。在结构体中所有的成员的默认修饰符是public ,但是类的成员默认修饰符是private.
public:所有的成员在外部可以访问。
private:类内部的成员才可以访问。
protected:子类,父类之间的访问。
#include "stdafx.h" #include <iostream> using namespace std;
//struct Point class Point { public: int x; int y; void init() { x = 0; y = 0; } void output() { cout<< x << endl<< y<<endl; } };
void main() { Point pt; pt.init() ; pt.output(); }
|
构造函数的引入:
1. 构造函数最重要的作用是创建对象本身。
2.C++ 规定,每个类都必须有一个构造函数,没有构造函数,就不能创建任何对象。
3.只要类定义了一个构造函数,不管是否带参数,C++就不在提供默认的构造函数了。还想引用无参的构造函数要自己定义。
#include "stdafx.h" #include <iostream> using namespace std;
class Point { public: int x; int y; /*void init() { x = 0; y = 0; }*/ //构1造¨¬函¡¥数ºy,ê?类¤¨¤名?为a函¡¥数ºy名?,ê?无T返¤¦Ì回?值¦Ì Point(){ x = 0; y = 0; } void output() { cout<< x << endl<< y<<endl; } };
void main() { Point pt; //pt.init() ; //pt.x = 5; //pt.y = 5; //pt.output(); cin >> pt.x; //cout<< pt.x << endl<< pt.y<<endl; } |
析构函数的引入:当一个对象的生命周期结束时,调用析构函数来释放对象的内存。
1.当一个对象生命周期结束时,其所占有的内存空间就要被回收,这个工作由析构函数来完成。
2.析构函数是“反向”的构造函数,析构函数不允许有返回值。且不允许带参数。并且一个类中只能有一个析构函数。
3. 根据析构函数的这种特点,我们可以在构造函数中初始化对象的某些成员变量,给其分配内存空间(堆内存),在析构函数中释放对象运行期间所申请的资源。
#include "stdafx.h" #include <iostream> using namespace std;
//struct Point class Point { public: int x; int y; /*void init() { x = 0; y = 0; }*/ //构1造¨¬函¡¥数ºy,ê?类¤¨¤名?为a函¡¥数ºy名?,ê?无T返¤¦Ì回?值¦Ì Point(){ x = 0; y = 0; } ~Point() { } void output() { cout<< x << endl<< y<<endl; } };
void main() { Point pt; //pt.init() ; //pt.x = 5; //pt.y = 5; pt.output(); //cout<< pt.x << endl<< pt.y<<endl; } |
函数的重载:
重载的说明:
(1) void output();
(2) int output();
不能构成重载,原因:编译器无法确定去调用那个函数。
(1) void output(int a, int b = 5);
(2) void output(int a );
不能构成重载,原因:编译器无法确定去调用那个函数。
#include "stdafx.h" #include <iostream> using namespace std;
//struct Point class Point { public: int x; int y; /*void init() { x = 0; y = 0; }*/ //构1造¨¬函¡¥数ºy,ê?类¤¨¤名?为a函¡¥数ºy名?,ê?无T返¤¦Ì回?值¦Ì Point(){ x = 0; y = 0; } Point(int a, int b){ x = a; y = b; } ~Point() { } void output() { cout<< x << endl<< y<<endl; } void output(int x ,int y){ x = x; y = y; } };
void main() { Point pt(3,3); //pt.init() ; //pt.x = 5; //pt.y = 5; pt.output(5,5);//此ä?时º¡À只?是º?给?形?参?赋3值¦Ì,ê?pt对?象¨®的Ì?成¨¦员¡Àx, y并¡é没?得Ì?到Ì?这a个?5,5. pt.output(); //cout<< pt.x << endl<< pt.y<<endl; } //打䨰印®?的Ì?结¨¢果?:êo3 3
|
this指针的引入:
1.this指针是一个隐含的指针,它指向对象本身,代表了对象的地址。
2.一个类所有的对象调用的成员函数都是同一代码段。但是在对象调用pt.output(6,6)时,成员函数除了接收到了一个对象s的地址。这个地址被一个隐含的形参this 指针所获取。它等同于执行this = &pt。所有对数据成员的访问都被隐含的加上了前缀this -> 。如:x = 0;等价于:this->x = 0;
#include "stdafx.h" #include <iostream> using namespace std;
//struct Point class Point { public: int x; int y; /*void init() { x = 0; y = 0; }*/ //构1造¨¬函¡¥数ºy,ê?类¤¨¤名?为a函¡¥数ºy名?,ê?无T返¤¦Ì回?值¦Ì Point(){ x = 0; y = 0; } Point(int a, int b){ x = a; y = b; } ~Point() { } void output() { cout<< x << endl<< y<<endl; } //不?换?形?参?时º¡À,ê?改?变À?成¨¦员¡À变À?量¢?x, y的Ì?值¦Ì void output(int x ,int y){ this->x = x; this->y = y; } };
void main() { Point pt(3,3); //pt.init() ; //pt.x = 5; //pt.y = 5; pt.output(5,5);//此ä?时º¡À只?是º?给?形?参?赋3值¦Ì,ê?pt对?象¨®的Ì?成¨¦员¡Àx, y并¡é没?得Ì?到Ì?这a个?5,5. pt.output(); //cout<< pt.x << endl<< pt.y<<endl; } //打䨰印®?的Ì?结¨¢果?:êo5 5
|