标签:iostream bsp 基本用法 end 子类 对象 方法 gety 知识
1 C++面向对象
1 // 2 // Created by Mr.Z on 2016/3/2 0002. 3 // 4 5 #ifndef L01OOP_PEOPLE_H 6 #define L01OOP_PEOPLE_H 7 8 #include <iostream> 9 class People { 10 public: 11 void sayHello(); 12 }; 13 14 15 #endif //L01OOP_PEOPLE_H
1 // 2 // Created by Mr.Z on 2016/3/2 0002. 3 // 4 5 #include "People.h" 6 7 void People::sayHello() { 8 printf("Hello CPP\n"); 9 }
1 #include <iostream> 2 #include "People.h" 3 4 class P { 5 public: 6 void sayHello() { 7 printf("Hello C++\n"); 8 } 9 }; 10 11 int main() { 12 13 P *p = new P(); 14 p->sayHello(); 15 delete p; 16 17 People *people = new People(); 18 people->sayHello(); 19 delete people; 20 21 return 0; 22 }
2 C++命名空间
1 // 2 // Created by Mr.Z on 2016/3/2 0002. 3 // 4 5 #ifndef L01OOP_PEOPLE_H 6 #define L01OOP_PEOPLE_H 7 8 #include <iostream> 9 10 namespace CPP { 11 class People { 12 public: 13 void sayHello(); 14 }; 15 } 16 17 18 #endif //L01OOP_PEOPLE_H
1 // 2 // Created by Mr.Z on 2016/3/2 0002. 3 // 4 5 #include "People.h" 6 7 namespace CPP { 8 void People::sayHello() { 9 printf("Hello CPP\n"); 10 } 11 }
1 #include "People.h" 2 3 using namespace CPP; 4 5 int main() { 6 7 People *p = new People(); 8 p->sayHello(); 9 delete (p); 10 11 return 0; 12 }
3 C++类的继承
1 // 2 // Created by Mr.Z on 2016/3/2 0002. 3 // 4 5 #ifndef L01OOP_PEOPLE_H 6 #define L01OOP_PEOPLE_H 7 8 #include <iostream> 9 10 class People { 11 private: 12 int age; 13 int sex; 14 15 public: 16 People(); 17 18 People(int age, int sex); 19 20 int getAge(); 21 22 int getSex(); 23 24 void sayHello(); 25 }; 26 27 28 #endif //L01OOP_PEOPLE_H
1 // 2 // Created by Mr.Z on 2016/3/2 0002. 3 // 4 5 #include "People.h" 6 7 People::People() { 8 this->age = 10; 9 this->sex = 1; 10 } 11 12 People::People(int age, int sex) { 13 this->age = age; 14 this->sex = sex; 15 } 16 17 18 int People::getAge() { 19 return this->age; 20 } 21 22 int People::getSex() { 23 return this->sex; 24 } 25 26 27 void People::sayHello() { 28 printf("Hello CPP\n"); 29 }
1 // 2 // Created by Mr.Z on 2016/3/2 0002. 3 // 4 5 #ifndef L03CLASSES_MAN_H 6 #define L03CLASSES_MAN_H 7 8 #include <iostream> 9 #include "People.h" 10 11 class Man : public People { 12 13 }; 14 15 16 #endif //L03CLASSES_MAN_H
1 // 2 // Created by Mr.Z on 2016/3/2 0002. 3 // 4 5 #include "Man.h"
1 #include "Man.h" 2 3 4 int main() { 5 Man *m = new Man(); 6 m->sayHello(); 7 return 0; 8 }
4 C++构造方法和析构方法
1 #include <iostream> 2 3 class Object { 4 public: 5 //构造方法 6 Object() { 7 printf("Create Object\n"); 8 } 9 10 //析构方法 11 ~Object() { 12 printf("Delete Object\n"); 13 } 14 }; 15 16 void runObject() { 17 { //没有大括号 Create Object runObject end Delete Object 18 19 Object obj; //有大括号 Create Object Delete Object runObject end 20 } 21 printf("runObject end\n"); 22 } 23 24 25 int main() { 26 27 // Object *obj = new Object();//创建时使用构造方法 28 // delete(obj); //销毁时使用析构方法 29 30 //-------------------------------------------------------------- 31 // Object object; //创建值对象 同时使用 构造和析构 32 33 //-------------------------------------------------------------- 34 runObject(); 35 printf("end\n"); 36 return 0; 37 }
5 C++执行父类的构造方法
1 // 2 // Created by Mr.Z on 2016/3/2 0002. 3 // 4 5 #ifndef L01OOP_PEOPLE_H 6 #define L01OOP_PEOPLE_H 7 8 #include <iostream> 9 10 class People { 11 private: 12 int age; 13 int sex; 14 15 public: 16 People(); 17 18 People(int age, int sex); 19 20 int getAge(); 21 22 int getSex(); 23 24 void sayHello(); 25 }; 26 27 28 #endif //L01OOP_PEOPLE_H
1 // 2 // Created by Mr.Z on 2016/3/2 0002. 3 // 4 5 #include "People.h" 6 7 People::People() { 8 this->age = 10; 9 this->sex = 1; 10 } 11 12 People::People(int age, int sex) { 13 this->age = age; 14 this->sex = sex; 15 } 16 17 18 int People::getAge() { 19 return this->age; 20 } 21 22 int People::getSex() { 23 return this->sex; 24 } 25 26 27 void People::sayHello() { 28 printf("Hello CPP\n"); 29 }
1 // 2 // Created by Mr.Z on 2016/3/2 0002. 3 // 4 5 #ifndef L03CLASSES_MAN_H 6 #define L03CLASSES_MAN_H 7 8 #include <iostream> 9 #include "People.h" 10 11 class Man : public People { 12 public: 13 Man(int age); 14 }; 15 16 17 #endif //L03CLASSES_MAN_H
1 // 2 // Created by Mr.Z on 2016/3/2 0002. 3 // 4 5 #include "Man.h" 6 7 //: People(age, 1) 执行父类的构造方法 8 Man::Man(int age) : People(age, 1) { 9 10 }
1 #include <iostream> 2 #include "Man.h" 3 4 5 int main() { 6 Man *m = new Man(20); 7 printf("age: %d\n", m->getAge()); 8 return 0; 9 }
6 C++执行父类的方法
People.h/cpp同上
1 // 2 // Created by Mr.Z on 2016/3/2 0002. 3 // 4 5 #ifndef L03CLASSES_MAN_H 6 #define L03CLASSES_MAN_H 7 8 #include <iostream> 9 #include "People.h" 10 11 class Man : public People { 12 public: 13 Man(int age); 14 void sayHello(); 15 }; 16 17 18 #endif //L03CLASSES_MAN_H
1 // 2 // Created by Mr.Z on 2016/3/2 0002. 3 // 4 5 #include "Man.h" 6 7 Man::Man(int age) : People(age, 1) { 8 // People::sayHello();//也可以执行父类 9 } 10 11 void Man::sayHello() { 12 People::sayHello();//执行父类方法 13 printf("Man say : Hello CPP\n");//重构的方法 14 }
7 C++实函数、虚函数、纯虚函数、函数重写
1 // 2 // Created by Mr.Z on 2016/3/2 0002. 3 // 4 5 #ifndef L01OOP_PEOPLE_H 6 #define L01OOP_PEOPLE_H 7 8 #include <iostream> 9 10 class People { 11 private: 12 int age; 13 int sex; 14 15 public: 16 People(); 17 18 People(int age, int sex); 19 20 int getAge(); 21 22 int getSex(); 23 24 virtual void sayHello();//虚函数 便于子类方法重写 25 26 virtual void eat() = 0;//纯虚函数 类似于Java的抽象方法 27 }; 28 29 30 #endif //L01OOP_PEOPLE_H
1 // 2 // Created by Mr.Z on 2016/3/2 0002. 3 // 4 5 #include "People.h" 6 7 People::People() { 8 this->age = 10; 9 this->sex = 1; 10 } 11 12 People::People(int age, int sex) { 13 this->age = age; 14 this->sex = sex; 15 } 16 17 18 int People::getAge() { 19 return this->age; 20 } 21 22 int People::getSex() { 23 return this->sex; 24 } 25 26 27 void People::sayHello() { 28 printf("People say : Hello CPP\n"); 29 }
1 // 2 // Created by Mr.Z on 2016/3/2 0002. 3 // 4 5 #ifndef L03CLASSES_MAN_H 6 #define L03CLASSES_MAN_H 7 8 #include <iostream> 9 #include "People.h" 10 11 class Man : public People { 12 public: 13 Man(int age); 14 15 virtual void sayHello();//虚函数 便于子类方法重写 主函数中 p->sayHello();定位到Man sayHello 16 17 virtual void eat(){ //实现纯虚函数 18 printf("Man eat\n"); 19 } 20 }; 21 22 23 #endif //L03CLASSES_MAN_H
1 // 2 // Created by Mr.Z on 2016/3/2 0002. 3 // 4 5 #include "Man.h" 6 7 Man::Man(int age) : People(age, 1) { 8 9 } 10 11 void Man::sayHello() { 12 printf("Man say : Hello CPP\n"); 13 }
1 #include <iostream> 2 #include "Man.h" 3 4 int main() { 5 6 People *p = new Man(20); 7 p->sayHello(); 8 p->eat(); 9 return 0; 10 }
只要类中有纯虚函数就是一个抽象类,全部都是纯虚函数就是纯虚类。类似Java接口。
8 C++函数重载
1 #include <iostream> 2 3 class Hello { 4 public: 5 void sayHello() { 6 printf("Hello Clion\n"); 7 } 8 9 void sayHello(std::string name) { 10 std::string str = "Hello "; 11 str += name; 12 std::cout << str << std::endl; 13 } 14 }; 15 16 int main() { 17 Hello *h = new Hello(); 18 h->sayHello(); 19 h->sayHello("Clion"); 20 return 0; 21 }
9 C++运算符重载
1 #include <iostream> 2 3 using namespace std; 4 5 6 class Point { 7 private: 8 int x, y; 9 public: 10 Point(int x, int y) { 11 this->x = x; 12 this->y = y; 13 } 14 15 int getX() { 16 return this->x; 17 } 18 19 int getY() { 20 return this->y; 21 } 22 23 void add(Point p) { 24 add(p.getX(), p.getY()); 25 } 26 27 void add(int x, int y) { 28 this->x += x; 29 this->y += y; 30 } 31 32 //运算符重载 33 void operator+=(Point p) { 34 add(p); 35 } 36 }; 37 38 int main() { 39 40 Point p(10, 10); 41 p += Point(13, 13); 42 cout << p.getX() << "\t" << p.getY() << endl; 43 44 Point *point = new Point(3, 3); 45 (*point) += Point(2, 2); 46 cout << point->getX() << "\t" << point->getY() << endl; 47 return 0; 48 }
10 C++伪函数
1 #include <iostream> 2 3 4 void hello() { 5 printf("hello\n"); 6 } 7 8 class Hello { //类 -----> 函数 伪函数 重载小括号operator() 9 public: 10 void operator()() { 11 printf("Hello Clion\n"); 12 } 13 }; 14 15 16 int main() { 17 18 hello(); 19 20 //伪函数 21 Hello h; 22 h(); 23 24 return 0; 25 }
11 C++函数指针
1 #include <stdio.h> 2 #include <thread> 3 #include <unistd.h> 4 #include <iostream> 5 6 class Object; 7 8 typedef void (Object::*SayHi)(); 9 10 typedef void (Object::*CallLaterHandler)(); 11 12 void threadFunc(Object *target, CallLaterHandler handler, int delay) { 13 sleep(delay); 14 (target->*handler)(); 15 } 16 17 void callLater(Object *target, CallLaterHandler handler, int delay) { 18 std::thread t(threadFunc, target, handler, delay); 19 t.join(); 20 } 21 22 #define CH(fp)(CallLaterHandler)(&fp) 23 24 class Object { 25 public: 26 SayHi sayHi; 27 }; 28 29 class Hello : public Object { 30 public: 31 Hello() { 32 callLater(this, CH(Hello::HelloSayHi), 3); 33 } 34 35 void HelloSayHi() { 36 printf("Hello Clion\n"); 37 } 38 }; 39 40 41 int main() { 42 Hello *h = new Hello(); 43 delete h; 44 return 0; 45 }
12 C++引用
1 #include <iostream> 2 3 4 class Point { 5 private: 6 int x, y; 7 public: 8 Point(int x, int y) { 9 this->x = x; 10 this->y = y; 11 } 12 13 int getX() { 14 return x; 15 } 16 17 int getY() { 18 return y; 19 } 20 21 //引用 &p 避免内存拷贝 22 void add(Point &p) { 23 this->x += p.x; 24 this->y += p.y; 25 } 26 }; 27 28 int main() { 29 Point p(1, 1); 30 Point p1(2, 3); 31 p.add(p1); 32 return 0; 33 }
13 C++友元类
1 #include <iostream> 2 3 class A { 4 friend class B; 5 6 friend class C; 7 8 private: 9 int num; 10 public: 11 A() { 12 num = 10; 13 } 14 }; 15 16 class B : public A { 17 public: 18 B() { 19 printf("%d\n", num); 20 } 21 }; 22 23 class C { 24 public: 25 C() { 26 A a; 27 printf("%d\n", a.num); 28 } 29 }; 30 31 int main() { 32 B b; 33 C c; 34 return 0; 35 }
14 C++标准库容器的基本用法
1 #include <iostream> 2 #include <list> 3 #include <map> 4 5 using namespace std; //std::list std::string std::cout std::map std::pair 6 7 int main() { 8 list<string> l; 9 l.push_back("Hello"); 10 l.push_back("Clion"); 11 for (list<string>::iterator it = l.begin(); it != l.end(); it++) { 12 cout << *it << endl; 13 } 14 15 map<string, string> m; 16 m.insert(pair<string, string>("hello", "Hello Clion")); 17 m.insert(pair<string, string>("name", "C++")); 18 cout << m.at("hello") << "\t" << m.at("name") << endl; 19 20 21 m["name"] = "C"; 22 cout << m["name"] << endl; 23 return 0; 24 }
15 C++字符串常用操作
1 #include <iostream> 2 #include <sstream> 3 4 using namespace std; 5 6 int main() { 7 string str; 8 str += "Hello "; 9 str += "Clion"; 10 cout << str << endl; 11 12 13 stringstream ss; 14 ss << "Hello "; 15 ss << 2016; 16 ss << " "; 17 ss << 3.4 << " "; 18 ss << "Hello " << "C++ " << 30; 19 cout << ss.str() << endl; 20 return 0; 21 }
16 C++文件操作
1 #include <iostream> 2 #include <fstream> 3 #include <sstream> 4 5 using namespace std; 6 7 int main() { 8 9 //Write 10 // ofstream of("data.txt"); 11 // of << "Hello Clion\n"; 12 // of.close(); 13 14 15 //Read 16 ifstream inf("data.txt"); 17 stringbuf sb; 18 inf >> &sb; 19 cout << sb.str() << endl; 20 return 0; 21 }
标签:iostream bsp 基本用法 end 子类 对象 方法 gety 知识
原文地址:http://www.cnblogs.com/changchou/p/6204679.html