c++基础

来源:互联网 发布:blue ray mac 编辑:程序博客网 时间:2024/06/04 17:45

1.有关流对象cin,cout,流运算符,以及string(字符串类型)类型的定义包含在下面的头文件中,并加入命名空间std。

#include<iostream>

using namespace std;


2.cin,cout

cin>>变量名;

cout<<“字符串”<<变量<<endl(相当于c语言的\n);


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:"<<name<<",age:"<<age<<",sex:"<<age<<endl;

}

};


6.代码

cin,cout使用

#include <iostream> using namespace std; //命称空间//cout<<     cin>>int main(){//name:小明,age:100cout<<"name:"<<"小明"<<", age:"<<100<<endl; //name:小明 ,age:100cout<<123.234f<<endl;cout<<1234<<endl;int age = 2000;cout<<age<<endl;cout<<&age<<endl;return 0;}

#include <iostream>using 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;}/*输入姓名: 小明输入姓别:男输入年龄:33name:小明, sex:男, age:33*/

引用

#include <iostream>using namespace std;void f1() {int a = 20;//定义引用类型变量时就立刻初始化int &b = a;  //b 是a的引用类型变量,  也就是说: b是a的别名  ////错//int &b;错//b = a;错cout<<a<<endl;//20cout<<b<<endl;//20int r = (&a==&b);//r == 1cout<<&b<<endl;cout<<&a<<endl;cout<<"r="<<r<<endl;//r=1;/**把b代表的内存的值赋给c    不要理解成为b再一个别叫c*/int c = b;    //c==20cout<<c<<endl;//20b = 100;      // b==100  a==100cout<<a<<endl;//100cout<<b<<endl;//100cout<<c<<endl;//20}void g1(int *p) {*p = 100; //a=100}//引用 类型变量主要使用于 作为函数的形参void g2(int &r) { //int &r = a  //r是a的别名r = 200; //a=200}/** * 引用为什么是受限的指针? 指针与引用类型变量的区别?  面试题 *  引用类型变量就是一个受限的指针     (比喻成指针) *  引用能做的事,指针都能做, 而指针能做的事,引用不一定能做 *  引用不重新开辟空间,所以速度要快些 */int main() {f1();int a = 20;g1(&a);cout<<a<<endl;//100g2(a);//100cout<<a<<endl;//200return 0;}

#include <iostream>using namespace std;#include "Stu.h"#include "Teacher.h"void f1() {Stu stu1; // age name sexStu 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="<<t1.name<<"t1.age="<<t1.age<<endl;t1.teach();//引用.成员Teacher &r = t1;r.name = 20;r.age = 3;r.teach();//对象指针->成员Teacher *p = &t1;p->age = 3;p->name = "fsdf";p->teach();}int main() {//f1();f2();return 0;}
#ifndef STU_H_#define STU_H_#include <iostream>using namespace std;//定义类class Stu {public://成员变量int age;string name;string sex;//成员函数void study() {cout<<"study()..."<<endl;}void sleep();};void Stu::sleep(){cout<<"sleep()..."<<endl;}#endif /* STU_H_ */
#include "Teacher.h"//解偶#include <iostream>using namespace std;void Teacher::teach() {cout<<"Teacher::teach()"<<endl;}
#ifndef TEACHER_H_#define TEACHER_H_#include <iostream>using namespace std;class Teacher {public:string name;int age;void teach();};#endif /* TEACHER_H_ */
函数重载

#include "Stu.h"void Stu::show() {cout<<"我是学生"<<endl;}void Stu::show(int age) {cout<<"我是学生,age = "<<age<<endl;}void Stu::show(int age, string name) {cout<<"我是学生, age="<<age<<", name="<<name<<endl;}
#ifndef STU_H_#define STU_H_#include <iostream>using namespace std;class Stu {public:void show();void show(int age);void show(int age, string name);};#endif /* STU_H_ */
默认参数

#include "Stu.h"#include <iostream>using namespace std;void Stu::show(int x, int y, int z) {cout<<"x="<<x<<endl;cout<<"y="<<y<<endl;cout<<"z="<<z<<endl;}
#ifndef STU_H_#define STU_H_class Stu {public://默认参数值,只写在声明的地方,不写在定义的地方void show(int x=1, int y=2, int z=3);};#endif /* STU_H_ */
this指针

#include "Stu.h"#include <iostream>using 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="<<this->x<<"y="<<this->y<<endl;}void Stu::show2() {this->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_ */












0 0
原创粉丝点击