1.有关流对象cin,cout,流运算符,以及string(字符串类型)类型的定义包含在下面的头文件中,并加入命名空间std。
#include
using namespace std;
2.cin,cout
cin>>变量名;
cout<<“字符串”<<变量<
3.字符串
字符串比较可以直接用== , < ,> ,!=
可以直接赋值比如
string s1 = "hello";
string s2 = s1;
可以直接+,表示连接
比如
string s1 = “hello”;
string s2 = "world";
string s3 = s1 + s2;
4.引用
对一个数据可以使用引用,他的作用是为这个变量取一个别名;主要用在函数的形参。
如:int a = 3;
int &b = a;
b为a的引用,b相当与a的别名,他们指向同一片内存空间,b并不额外的分配空间,所以b就相当于a,访问b就是访问a。
注:应用声明时就必须初始化,并且以后 都不能在被重新赋值。引用时类型必须与初始化的变量类型一致。
使用限制:
不能声明数组的引用,不能声明引用的引用,不能申明引用的指针。
5.对象,类
类中有数据,和操作。
有public和private;
定义类:
class student{
public:
string name;//数据
int age;
int sex;
void show(){//操作函数
cout<<"name:"<
}
};
6.代码
cin,cout使用
#includeusing namespace std; //命称空间 //cout<< cin>> int main() { //name:小明,age:100 cout<<"name:"<<"小明"<<", age:"<<100< #includeusing namespace std; int main() { string name; char sex[16]; int age; cout << "输入姓名: " << endl; cin >> name; cout << "输入姓别:" << "\n"; cin >> sex; cout << "输入年龄:\n"; cin >> age; cout << "name:" << name << ", sex:" << sex << ", age:" << age << endl; return 0; } /* 输入姓名: 小明 输入姓别: 男 输入年龄: 33 name:小明, sex:男, age:33 */ 引用
#includeusing namespace std; void f1() { int a = 20; //定义引用类型变量时就立刻初始化 int &b = a; //b 是a的引用类型变量, 也就是说: b是a的别名 // //错 // int &b;错 // b = a;错 cout< 类
#includeusing namespace std; #include "Stu.h" #include "Teacher.h" void f1() { Stu stu1; // age name sex Stu stu2; // age name sex //访问成员 “对象.成员” stu1.age = 10; stu1.name = "小明"; stu1.sex = "男"; stu2.age = 30; stu2.name = "大明"; stu2.sex = "男"; cout << "stu1: name=" << stu1.name << ", age=" << stu1.age << ",sex=" << stu1.sex << endl; stu1.sleep(); stu1.study(); cout << "stu2: name=" << stu2.name << ", age=" << stu2.age << ",sex=" << stu2.sex << endl; stu2.sleep(); stu2.study(); } /** stu1: name=小明, age=10,sex=男 sleep()... study()... stu2: name=大明, age=30,sex=男 sleep()... study()... */ void f2() { Teacher t1; //对象.成员 t1.name = "王海宁"; t1.age = 28; cout<<"t1.name="< 成员 Teacher *p = &t1; p->age = 3; p->name = "fsdf"; p->teach(); } int main() { // f1(); f2(); return 0; } #ifndef STU_H_ #define STU_H_ #includeusing namespace std; //定义类 class Stu { public: //成员变量 int age; string name; string sex; //成员函数 void study() { cout<<"study()..."< #include "Teacher.h" //解偶 #includeusing namespace std; void Teacher::teach() { cout<<"Teacher::teach()"< #ifndef TEACHER_H_ #define TEACHER_H_ #include函数重载using namespace std; class Teacher { public: string name; int age; void teach(); }; #endif /* TEACHER_H_ */ #include "Stu.h" void Stu::show() { cout<<"我是学生"<默认参数#ifndef STU_H_ #define STU_H_ #include using namespace std; class Stu { public: void show(); void show(int age); void show(int age, string name); }; #endif /* STU_H_ */ #include "Stu.h" #includethis指针using namespace std; void Stu::show(int x, int y, int z) { cout<<"x="< #ifndef STU_H_ #define STU_H_ class Stu { public: //默认参数值,只写在声明的地方,不写在定义的地方 void show(int x=1, int y=2, int z=3); }; #endif /* STU_H_ */ #include "Stu.h" #includeusing namespace std; //this一定是指向当前类对象 //this在这里当前类是 Stu类 //this一定是一个Stu类对象的地址 //成员函数中的this 指向调用该函数的对象 void Stu::setX(int x) { this->x = x; } void Stu::setY(int y) { this->y = y; } int Stu::getX() { return this->x; } int Stu::getY() { return this->y; } void Stu::show() { cout<<"x="< x<<"y="< y< show(); } void Stu::show3() { this->show2(); } #ifndef STU_H_ #define STU_H_ class Stu { public: int x; int y; void setX(int x); void setY(int y); int getX(); int getY(); void show(); void show2(); void show3(); }; #endif /* STU_H_ */