推荐一个很好的编程学习平台---菜鸟教程,基本把编程语言的相应知识点都涉及到了,对于想快速入门某一门语言的朋友都可以去看看,下面是我个人的一些学习笔记,只是为了方便以后用到某个基础知识点的时候方便翻阅,如果能帮助到他人不胜荣幸。 #include <iostream> #include <time h=""> using namespace std; /*结构类型 tm 把日期和时间以 C 结构的形式保存 struct tm { int tm_sec; // 秒,正常范围从 0 到 59,但允许至 61 int tm_min; // 分,范围从 0 到 59 int tm_hour; // 小时,范围从 0 到 23 int tm_mday; // 一月中的第几天,范围从 1 到 31 int tm_mon; // 月,范围从 0 到 11 int tm_year; // 自 1900 年起的年数 int tm_wday; // 一周中的第几天,范围从 0 到 6,从星期日算起 int tm_yday; // 一年中的第几天,范围从 0 到 365,从 1 月 1 日算起 int tm_isdst; // 夏令时 } */ int main1() { cout << "Hello World!\n"<<endl; //**********time_t time(time_t *t)**************** time_t seconds;//seconds -- 这是指向类型为 time_t 的对象的指针,用来存储 seconds 的值。 seconds = time(NULL); printf("自 1970-01-01 起的小时数 = %ld\n", seconds/3600); //**********char *ctime(const time_t *timer)**************** // timer:这是指向 time_t 对象的指针,该对象包含了一个日历时间。 time_t curtime; time(&curtime); char* dt = ctime(&curtime); printf("now:%s",dt); tm *gmtm = gmtime(&curtime); dt = asctime(gmtm); cout << "utc time:" << dt << endl; return 0; } //time test int main2() { // 基于当前系统的当前日期/时间 time_t now = time(0); cout << "nuber of sec sine january 1,1970:" << now << endl; tm* ltm = localtime(&now); //输出 tm 结构的各个组成部分 cout << "year:" << 1900 + ltm->tm_year << endl; cout << "month:" << 1 + ltm->tm_mon << endl; cout << "Day: "<< ltm->tm_mday << endl; cout << "Time: "<< ltm->tm_hour << ":"; cout << ltm->tm_min << ":"; cout << ltm->tm_sec << endl; return 0 ; } //io test /* 使用 cerr 流来显示错误消息,而其他的日志消息则使用 clog 流来输出。 */ int main3() { char* inp; char name[50]; cout << "please input your name:" << endl; cin >> name; cout << "your name is :" << name << endl; char str[] = "unable to read..."; cerr << "error message(cerr):" << str << endl; clog << "error message(clog):" << str << endl; return 0; } /* struct type_name { member_type1 member_name1; member_type2 member_name2; member_type3 member_name3; . . } object_names; */ //CString 是一种很有用的数据类型。它们很大程度上简化了MFC中的许多操作, //使得MFC在做字符串操作的时候方便了很多 #include <cstring> void printBook(struct Books *book); //给结构体取别名用typedef typedef struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }Books; int main4() { Books Book1; //book book2; Books Book2; //strcpy(a,b) -- 将b中的字符串内容复制到a中 // Book1 详述 //Book1.title = "C++ 教程"--->wrong--不能直接赋值 strcpy( Book1.title, "C++ 教程"); strcpy( Book1.author, "Runoob"); strcpy( Book1.subject, "编程语言"); Book1.book_id = 12345; // Book2 详述 strcpy( Book2.title, "CSS 教程"); strcpy( Book2.author, "Runoob"); strcpy( Book2.subject, "前端技术"); Book2.book_id = 12346; /* // 输出 Book1 信息 cout << "第一本书标题 : " << Book1.title <<endl; cout << "第一本书作者 : " << Book1.author <<endl; cout << "第一本书类目 : " << Book1.subject <<endl; cout << "第一本书 ID : " << Book1.book_id <<endl; // 输出 Book2 信息 cout << "第二本书标题 : " << Book2.title <<endl; cout << "第二本书作者 : " << Book2.author <<endl; cout << "第二本书类目 : " << Book2.subject <<endl; cout << "第二本书 ID : " << Book2.book_id <<endl struct="" books="" struct_pointer="" struct_pointer="&Book1;" struct_pointer-="">title;//为了使用指向该结构的指针访问结构的成员必须使用 -> 运算符 ******/ //指针方式输出 printBook(&Book1); printBook(&Book2); return 0; } void printBook(Books *book) { cout << "结构体方式输出" << endl; cout << "书标题 : " << book->title <<endl; cout << "书作者 : " << book->author <<endl; cout << "书类目 : " << book->subject <<endl; cout << "书 ID : " << book->book_id <<endl; } /**********C++类************/ class Box { public: //公有访问属性 double length; double breadth; double height; //内部定义类成员函数 double getVolume(void) { return length*breadth*height; } double getVolume1(void); //外部定义类成员函数时,内部需要对其进行声明 }; //可以在类的外部使用范围解析运算符 :: 定义该函数 //外部定义类成员函数 double Box::getVolume1(void) { return length*breadth*height; } int main5() { Box box1; Box box2; double volume = 0.0; box1.height = 5.0; box1.length = 6; box1.breadth = 7; box2.height = 10.0; box2.length = 12.0; box2.breadth = 13.0; volume = box1.height * box1.length * box1.breadth; cout << "box1's volume is :" << volume << endl; volume = box2.height * box2.length * box2.breadth; cout << "box2's volume is :" << volume << endl; return 0; } /*******C++ 类访问修饰符---继承*******/ /* public、private、protected:一个类可以有多个 public、protected 或 private 标记区域。 每个标记区域在下一个标记区域开始之前或者在遇到类主体结束右括号之前都是有效的。 成员和类的默认访问修饰符是 private。 class Base { public: //公有成员在程序中类的外部是可访问的 // 公有成员 protected://保护成员变量或函数与私有成员十分相似,但有一点不同,保护成员在派生类(即子类)中是可访问的 // 受保护成员 private: //私有成员变量或函数在类的外部是不可访问的,甚至是不可查看的 // 私有成员 }; 1.public 继承:基类 public 成员,protected 成员,private 成员的访问属性在派生类中分别变成:public, protected, private 2.protected 继承:基类 public 成员,protected 成员,private 成员的访问属性在派生类中分别变成:protected, protected, private 3.private 继承:基类 public 成员,protected 成员,private 成员的访问属性在派生类中分别变成:private, private, private 但无论哪种继承方式,上面两点都没有改变: 1.private 成员只能被本类成员(类内)和友元访问,不能被派生类访问; 2.protected 成员可以被派生类访问。 */ /*******C++多态*******/ /* 多态按字面的意思就是多种形态。当类之间存在层次结构,并且类之间是通过继承关联时,就会用到多态。 C++ 多态意味着调用成员函数时,会根据调用函数的对象的类型来执行不同的函数。 */ class Shape{ protected: int width,height; public: Shape(int a=0, int b=0) { width = a; height = b; } //int area() //静态多态--早绑定 virtual int area() //动态绑定---后期绑定 { cout << "parent class area:" << endl; return 0; } }; class Rectangle: public Shape{ public: Rectangle(int a = 0, int b = 0):Shape(a,b){} int area() { cout << "Rectangle class area : " << endl; return (width * height); } }; class Triangle:public Shape { public: Triangle(int a= 0, int b = 0):Shape(a,b){} int area() { cout << "Tiangle class area:" << endl; return (width * height / 2); } }; int main6() { Shape * shape; Rectangle rec(10,7); Triangle tri(10,5); // 存储矩形的地址 shape = &rec; // 调用矩形的求面积函数 area shape->area(); // 存储三角形的地址 shape = &tri; // 调用三角形的求面积函数 area shape->area(); return 0 ; } /* C++ 接口是使用抽象类来实现的,抽象类与数据抽象互不混淆,数据抽象是一个把实现细节与相关的数据分离开的概念。 如果类中至少有一个函数被声明为纯虚函数,则这个类就是抽象类。纯虚函数是通过在声明中使用 "= 0" 来指定的,如下 class Box { public: // 纯虚函数 virtual double getVolume() = 0; private: double length; // 长度 double breadth; // 宽度 double height; // 高度 }; 抽象类时为了给其他类提供一个可以继承的适当的基类。抽象类不能被用于实例化对象, 它只能作为接口使用。如果试图实例化一个抽象类的对象,会导致编译错误 可用于实例化对象的类被称为具体类 面向对象的系统可能会使用一个抽象基类为所有的外部应用程序提供一个适当的、通用的、标准化的接口。然后,派生类通过继承抽象基类,就把所有类似的操作都继承下来。 外部应用程序提供的功能(即公有函数)在抽象基类中是以纯虚函数的形式存在的。这些纯虚函数在相应的派生类中被实现。 */ // 基类 class Shape { public: // 提供接口框架的纯虚函数 virtual int getArea() = 0; void setWidth(int w) { width = w; } void setHeight(int h) { height = h; } protected: int width; int height; }; // 派生类 class Rectangle: public Shape { public: int getArea() { return (width * height); } }; class Triangle: public Shape { public: int getArea() { return (width * height)/2; } }; int main(void) { Rectangle Rect; Triangle Tri; Rect.setWidth(5); Rect.setHeight(7); // 输出对象的面积 cout << "Total Rectangle area: " << Rect.getArea() << endl; Tri.setWidth(5); Tri.setHeight(7); // 输出对象的面积 cout << "Total Triangle area: " << Tri.getArea() << endl; return 0; } </endl></cstring></time></iostream>