标签:io os ar 使用 sp 文件 数据 on art
类的声明格式:class 类名标识符 { [public:] [数据成员的声明] [成员函数的声明] [private:] [数据成员的声明] [成员函数的声明] [protected:] [数据成员的声明] [成员函数的声明] };
class CPerson { public: CPerson(); int m_iIndex; int getIndex(); }; //构造函数 CPerson::CPerson() { m_iIndex = 10; }
#include <iostream> #include <cstring> using namespace std; class Person { public: Person(int _index, char *_name); //构造函数 Person(Person & copyPerson); //复制构造函数 int index; char name[30]; }; //构造函数 Person::Person(int _index, char *_name) { index = _index; strcpy(name, _name); } //复制构造函数 Person::Person(Person & copyPerson) { index = copyPerson.index; strcpy(name, copyPerson.name); } int main(int argc, char *argv[]) { Person p(1, "lasolmi"); Person q(p); cout << q.name << endl; return 0; }
CPerson::~CPerson() { delete[] m_pMessage; }
class CBook { public: static unsigned int m_Price; }; 在定义静态数据成员时,通常需要在类体外部对静态成员进行初始化。例如: unsigned int CBook::m_Price = 10; 对于静态成员来说,不仅可以通过对象访问,还可以直接使用类名访问。例如: int main(int argc,char* argv[]) { CBook book; cout << CBook::m_Price << endl; //通过类名访问静态成员 cout << book.m_Price << endl; //通过对象访问静态成员 return 0; }
class CBook { public: static unsigned int m_Price; CBook m_book; //非法的定义,不允许在该类中定义所属类的对象 static CBook m_VCbook; //正确,静态数据成员允许定义类的所属类对象 CBook *m_pBook; //正确,允许定义类的所属类型的指针类型对象 }
#include <iostream> #include <cstring> using namespace std; #define MAXLEN 128 class CList { public: class CNode { friend class CList; private: int m_Tag; public: char m_Name[MAXLEN]; }; public: CNode m_Node; void SetNodeName(const char *pchData) { if(pchData != NULL) { strcpy(m_Node.m_Name, pchData); } } void SetNodeTag(int tag) { m_Node.m_Tag = tag; } }; int main(int argc, char* argv[]) { CList c; c.SetNodeName("lasolmi"); cout << c.m_Node.m_Name << endl; return 0; }
class B { public: friend class A; ... }
class B { friend void A::function(); ... };
class B { friend void function(); ... } void function() {...}
namespace 名称 { 常量、变量、函数等对象的定义 }
#include <iostream> using namespace std; namespace MyName1 { //定义命名空间 int iValue = 10; } namespace MyName2 { //定义命名空间 int iValue = 20; } int iValue = 30; //全局变量 int main(int argc,char* argv[]) { cout << MyName1::iValue << endl; //引用MyName1命名空间中的变量 cout << MyName2::iValue << endl; //引用MyName2命名空间中的变量 cout << iValue << endl; return 0; }
using namespace 命名空间名称;如果使用using namespace语句,则在引用空间中的成员时直接使用就可以。
#include <iostream> using namespace std; namespace Output { void show() { cout << "Output's function!" << endl; } namespace MyName { void Demo() { cout << "MyName's function!" << endl; } } } int main(int argc, char* argv[]) { Output::show(); Output::MyName::Demo(); return 0; }
class 派生类名标识符:[继承方式] 基类名标识符 { [访问控制修饰符:] [成员声明列表] }
#include <iostream> using namespace std; class CBook { public: CBook(int iPage) { m_iPage = iPage; } CBook operator+(CBook b) { return CBook(m_iPage+b.m_iPage); } void display() { cout << m_iPage <<endl; } protected: int m_iPage; }; int main(int argc,char* argv[]) { CBook bk1(10); CBook bk2(20); CBook tmp(0); tmp = bk1 + bk2; tmp.display(); return 0; }
class 派生类名标识符:[继承方式] 基类名标识符1,...,访问控制修饰符 基类名标识符n { [访问控制修饰符:] [成员声明列表] };
#include <iostream> using namespace std; class CBird { public: void FlyInSky() { cout << "鸟能够在天空飞翔" << endl; } void Breath() { cout << "鸟能够呼吸" << endl; } }; class CFish { public: void SwimInWater() { cout << "鱼能够在水里游" << endl; } void Breath() { cout << "鱼能够呼吸" << endl; } }; class CWaterBird: public CBird, public CFish { public: void Action() { cout << "水鸟能飞又能游" << endl; } }; int main(int argc, char* argv[]) { CWaterBird waterbird; waterbird.FlyInSky(); waterbird.SwimInWater(); return 0; }
#include <iostream> #include <cstring> using namespace std; class CEmployee { //定义CEmployee类 public: int m_ID; char m_Name[128]; char m_Depart[128]; CEmployee() { memset(m_Name, 0, 128); memset(m_Depart, 0, 128); } virtual void OutputName() { //定义一个虚成员函数 cout << "员工姓名:" << m_Name << endl; } }; class COperator:public CEmployee { //从CEmployee类派生一个子类 public: char m_Password[128]; void OutputName() { cout << "操作员姓名:" << m_Name << endl; } }; int main(int argc, char* argv[]) { CEmployee *pWorker = new COperator(); //定义CEmployee类型指针,调用COperator类构造函数 strcpy(pWorker->m_Name, "lasolmi"); //设置m_Name数据成员信息 pWorker->OutputName(); //调用COperator类的OutputName成员函数 delete pWorker; //释放对象 return 0; }
#include <iostream> using namespace std; class CAnimal { //定义一个动物类 public: CAnimal() {cout<<"动物类被构造"<<endl;} void move() {cout<<"动物能够移动"<<endl;} }; class CBird : virtual public CAnimal { //从CAnimal类虚继承CBird类 public: CBird() {cout<<"鸟类被构造"<<endl;} void FlyInSky() {cout<<"鸟能够在天空飞翔"<<endl;} void Breath() {cout<<"鸟能够呼吸"<<endl;} }; class CFish : virtual public CAnimal { //从CAnimal类虚继承CFish类 public: CFish() {cout<<"鱼类被构造"<<endl;} void SwimInWater() {cout<<"鱼能够在水里游"<<endl;} void Breath() {cout<<"与能够在水里游"<<endl;} }; class CWaterBird : public CBird,public CFish { public: CWaterBird() {cout<<"水鸟类被构造"<<endl;} void Action() {cout<<"水鸟既能飞又能游"<<endl;} }; int main(int argc,char* argv[]) { CWaterBird waterbird; return 0; }
#include <iostream> #include <cstring> using namespace std; class CEmployee { //定义CEmployee类 public: int m_ID; char m_Name[128]; char m_Depart[128]; virtual void OutputName()=0; //定义抽象成员函数 }; class COperator : public CEmployee { public: char m_Password[128]; void OutputName() {cout<<"操作员姓名:"<<m_Name<<endl;} COperator() {strcpy(m_Name, "lasolmi");} }; class CSystemManager : public CEmployee { //定义CSystemManager类 public: char m_Password[128]; void OutputName() {cout<<"系统管理员姓名:"<<m_Name<<endl;} CSystemManager() {strcpy(m_Name, "congli");} }; int main(int argc, char* argv[]) { CEmployee *pWorker; //定义CEmployee类型指针 pWorker = new COperator(); //调用COperator类的构造函数,为pWorker赋值 pWorker->OutputName(); //调用COperator类的OutputName成员函数 delete pWorker; //释放pWorker对象 pWorker = NULL; //将pWorker对象设置为空 pWorker = new CSystemManager(); //调CSystemManager用类的构造函数,为pWorker赋值 pWorker->OutputName(); //调用CSystenManager类的OutputName成员函数 delete pWorker; //释放pWorker对象 pWorker = NULL; //将pWorker对象设置为空 return 0; }
标签:io os ar 使用 sp 文件 数据 on art
原文地址:http://blog.csdn.net/lasolmi/article/details/40537713