标签:des cWeb style blog http io color os ar
1、C++语言支持的新特性
注:1983年夏,“带类的C”被正式命名为“C++”
2、常量
不同类型数据间的转换(赋值兼容):
#include<iostream> using namespace std; #define PI 3.1415926 int main() { const int NUM=6; enum color{red,yellow,green,black,white}; cout<<green<<endl; int a;//未初始化,值不确定 cout<<"a="<<a<<endl; float b=1.23456789; cout<<b<<endl; long double c=1.23456789; cout<<c<<endl; cout<<sizeof(short int)<<"Byte"<<‘\t‘ <<sizeof(int)<<"Byte"<<‘\t‘ <<sizeof(float)<<"Byte"<<‘\t‘ <<sizeof(double)<<"Byte"<<‘\t‘ <<sizeof(long double)<<"Byte"<<endl; double d=1.23f; cout<<d<<endl; char ch=6; cout<<ch<<endl; char ch1=0141; //97 cout<<"ab\\\"cd\"\t\101\x41\0cd"<<‘\0‘<<ch1<<endl; return 0; }
5、指针
优点
缺点
#include<iostream> using namespace std; int main(int argc, char *argv[]) { while(argc > 1) { ++argv; cout<<*argv<<endl; --argc; } return 0; }
动态存储分配:
指针、引用、字符串测试源代码:
#include<iostream> using namespace std; int main() { char str[] = "Hello"; string m_str = "Hello"; string name[5] = {"zhang","li","gao"}; char *pstr = "Hello"; while(*pstr!=‘\0‘) { cout<<*pstr; pstr++; } cout<<endl; cout<<"字符数组的长度:" <<sizeof(str) <<"Byte"<<endl; cout<<"字符串的长度: " <<strlen("Hello")<<"Byte"<<endl; //每一个字符串变量被编译系统分配固定长度的存储单元,VC++为16个字节 cout<<"字符串变量的长度:"<<sizeof(m_str) <<"Byte"<<endl; cout<<sizeof(string)<<‘\t‘<<sizeof(name)<<endl; //一般的C++编译系统为每一个指针变量分配4个字节的存储单元,用来存放变量的地址 cout<<sizeof(pstr)<<endl; cout<<"**************************************************"<<endl<<endl; int arr[10]={1,2,3,4,5,6,7,8,9,10}; int *p=arr; for(int i=0;i<10;i++) { // cout<<arr[i]<<‘\0‘; cout<<*(p+i)<<‘ ‘; } cout<<endl; int *p1 = new int(10); cout<<*p1<<endl; delete p1; return 0; }
8、函数
enum weekday{sun, mon, tue, wed, thu, fri, sat};//声明 weekday workday, week_end; //定义枚举变量
enum weekday{sun=7, mon=1, tue, wed, thu, fri, sat};
编译系统自动按顺序加1,则sat为6
#include<iostream> using namespace std; //声明枚举类型 enum weekday{sun=7, mon=1, tue, wed, thu, fri, sat}; struct Student //结构体 { int age; char name[20]; union P //共用体 { int grade; char position[10]; }category; unsigned a:2;//位段 unsigned b:3; }; int main() { weekday workday;//定义枚举变量 workday=weekday(1);//C++风格的强制类型转换格式 cout<<workday<<endl <<wed<<endl; Student person[2];//定义共用体变量 person[0].category.grade=20; cout<<person[0].category.grade<<endl; person[1].a = 3; //给位段赋值 cout<<"a="<<person[1].a<<endl; person[1].b = 12; //十进制12-->二进制1100--取其低3位-->二进制100-->十进制4 cout<<"b="<<person[1].b<<endl; return 0; }
#include<iostream> using namespace std; class Box { int length, width, height;//私有成员 public: Box(); //用参数初始化表对数据成员初始化 Box(int len, int w, int h):length(len),width(w),height(h) { } ~Box() { } int volume(); };//分号不可少 Box::Box() { length = 5; width = 5; height = 5; } int Box::volume() { //显示的使用this指针 return this->width * this->height * this->length; } int main() { Box b1; Box *p = NULL;//指向对象的指针 //定义对象数组 Box b[3] = {Box(),Box(10,10,10),Box(20,20,20)}; p = &b[2]; cout<<b1.volume() <<endl; cout<<b[0].volume()<<‘\t‘ <<b[1].volume()<<endl; cout<<p->volume() <<‘\t‘ <<(*p).volume()<<endl; return 0; }
共用数据的保护:
静态成员:
对象的赋值与复制:
友元:
类和对象源代码(二):
#include<iostream> using namespace std; class Date;//对Date类的提前引用声明 class Time { public: Time(int, int, int); void display(Date &);//形参为对象的引用 private: int hour,minute,second; }; class Date { public: Date(); Date(int, int, int); //声明Time类中的display函数为Date类的友元成员函数 friend void Time::display(Date &); private: int year,month,day; }; Time::Time(int h, int m, int s):hour(h),minute(m),second(s) { } void Time::display(Date &d) { cout<<"d.day="<<d.day<<‘\t‘ <<"t.hour="<<hour<<endl; } Date::Date() { year = 1000; month = 12; day = 20; } Date::Date(int y, int m, int d) { year = y; month = m; day = d; } int main() { Time t(5,5,5); Time t1(t); //对象的复制 Date d(2000,10,30), d1; d1 = d; //对象的赋值 t.display(d); t1.display(d1); return 0; }
14、运算符重载
15、继承和派生
#include<iostream> #include<string> using namespace std; class Person { public: Person(string nam,int a,char s):name(nam),age(a),sex(s) { } void show() { cout<<"name:" <<‘\t‘<<name <<endl <<"age:" <<‘\t‘<<age <<endl <<"sex:" <<‘\t‘<<sex <<endl; } protected: string name; int age; char sex; }; class Teacher:virtual public Person//声明Person为公用继承的虚基类 { public: //构造函数 Teacher(string nam,int a,char s,string t):Person(nam, a,s),title(t) { } void display() { show(); cout<<"title:"<<‘\t‘<<title<<‘\n‘; } protected: string title; }; class Student:virtual public Person//声明Person为公用继承的虚基类 { public: //构造函数 Student(string nam, int a,char s,float sco):Person(nam,a,s),score(sco) { } void display() { show(); cout<<"score:"<<‘\t‘<<score<<‘\n‘; } protected: float score; }; class Graduate:public Teacher,public Student//声明多重继承的派生类Graduate { public: //不仅要对直接基类初始化,还要对虚基类初始化 Graduate(string nam,int a,char s,string t,float sco,float w): Person(nam,a,s),Teacher(nam,a,s,t),Student(nam,a,s,sco),wage(w) { } void present() { cout<<"name:" <<‘\t‘<<name <<‘\n‘ <<"age:" <<‘\t‘<<age <<‘\n‘ <<"sex:" <<‘\t‘<<sex <<‘\n‘ <<"title:"<<‘\t‘<<title<<‘\n‘ <<"score:"<<‘\t‘<<score<<‘\n‘ <<"wage:" <<‘\t‘<<wage <<‘\n‘; } private: float wage; }; int main() { Graduate grad1("WangLi",24,‘f‘,"assistant",99.5,1325.5); grad1.present(); return 0; }
16、多态性和虚函数
虚函数:
纯虚函数:virtual 函数类型 函数名(参数表列)=0;//声明语句
抽象类:不用来定义对象,只作为一种基本类型用作继承的类
#include<iostream> #include<string> using namespace std; class Student { public: Student(string nam,int a,char s,float sco):name(nam),age(a),sex(s),score(sco) { } // virtual void display() = 0;//纯虚函数 virtual void display()//虚函数 { cout<<"name:" <<‘\t‘<<name <<‘\n‘ <<"age:" <<‘\t‘<<age <<‘\n‘ <<"sex:" <<‘\t‘<<sex <<‘\n‘ <<"score:"<<‘\t‘<<score<<‘\n‘; } protected: string name; int age; char sex; float score; }; class Graduate:public Student { public: Graduate(string nam,int a,char s,float sco,float w): Student(nam,a,s,sco),wage(w) { } void display()//虚函数 { cout<<"name:" <<‘\t‘<<name <<‘\n‘ <<"age:" <<‘\t‘<<age <<‘\n‘ <<"sex:" <<‘\t‘<<sex <<‘\n‘ <<"score:"<<‘\t‘<<score<<‘\n‘ <<"wage:" <<‘\t‘<<wage <<‘\n‘; } private: float wage; }; int main() { Student stud("LiGang",18,‘m‘,95); Graduate grad("WangLi",24,‘f‘,99.5,1325.5); grad.display();//静态关联 cout<<"******************************************\n"; Student *pt = &stud; pt->display();//动态关联 cout<<"******************************************\n"; pt = &grad; pt->display(); return 0; }
17、输入输出流
18、用typedef声明类型
19、预处理命令
typedef与预处理命令源代码:
#include<iostream> using namespace std; typedef int ARR[2]; //用typedef声明数组类型 #define MAX(x,y) x>y?x:y //带参数的宏定义 int main() { int max; ARR a; a[0]=1; a[1]=2; max=MAX(a[0],a[1]); #ifdef MAX cout<<"max="<<max<<endl; #else cout<<"MAX(x,y) is not defined! "<<endl; #endif return 0; }
20、模板
模板源代码:
#include<iostream> using namespace std; //声明类模板,Box为类模板名 template<typename TYPE> //<>中的为模板形参表 class Box//类声明 { TYPE length, width, height; public: Box(); Box(TYPE len, TYPE w, TYPE h):length(len),width(w),height(h) { } ~Box() { } TYPE volume(); }; //类外定义成员函数 template<typename TYPE> Box<TYPE>::Box() { length = 5; width = 5; height = 5; } //类外定义成员函数 template<typename TYPE> TYPE Box<TYPE>::volume() { return width * height * length; } //声明定义函数模板 template<typename TYPE> TYPE max(TYPE a,TYPE b) { return a>b?a:b; } int main() { Box<int> b1;//定义类对象 Box<int> b2(10,10,10); cout<<b1.volume()<<endl <<b2.volume()<<endl; cout<<"max="<<max(3.3,6.24)<<endl; return 0; }
标签:des cWeb style blog http io color os ar
原文地址:http://www.cnblogs.com/gaohongchen01/p/4065872.html