c++知识学习 (7)
2014-01-10 15:15
302 查看
静态成员函数(static)
静态成员函数的声明是在类体中的函数声明前加上关键字static
例如
static Person * create(const string name);
*在静态函数中不能使用this指针。
不需实例化调用。可以直接用类名调用。
输出:
Person::print()
Person::print()
call the functon Person()
this is a person
call the functon Person()
静态成员函数的声明是在类体中的函数声明前加上关键字static
例如
static Person * create(const string name);
*在静态函数中不能使用this指针。
不需实例化调用。可以直接用类名调用。
// // Person.h // ArrayTest // // Created by 张学院 on 14-1-8. // Copyright (c) 2014年 com.mix. All rights reserved. // //防止重复引用 #ifndef __ArrayTest__Person__ #define __ArrayTest__Person__ #include <iostream> using namespace std; class Person{ //---------成员变量-------- public : int age; private : int weight; char * name; char sex; //---------成员方法-------- public: //----构造函数------- Person(); //-----构造函数重载----- Person(int age); //------拷贝构造函数------- Person(const Person & per); //----------操作符重载---------- Person & operator=(const Person &right); void setWeight(int weight); int getWeight() const; //char * getName() const; //const 指针,保证指针不被修改 const char * getName() const; void setName(const char * name); //-----虚函数:子类的可能会重写父类的的方法,当父类的指针指向子类的对象,子类对象调用的是子类自己的方法-------- virtual void info() const; //-----纯虚函数-------- //virtual void info() const=0; ~Person(); static void print(); //---类方法重载---- static Person * Create(); static Person * Create(const char * name); }; class Student:public Person{ private: float score; public: // void info() const; Student(); ~Student(); }; class Worker:public Person{ public : void info() const; }; #endif /* defined(__ArrayTest__Person__) */
// // Person.cpp // ArrayTest // // Created by 张学院 on 14-1-8. // Copyright (c) 2014年 com.mix. All rights reserved. // #include "Person.h" //------方法实现格式------ //----返回值 类::方法名 (参数)------ void Person::setWeight(int weight){ //-----this 代表 this->weight=weight; } //--------const 编译限制------- int Person::getWeight() const{ //weight++;报错 return weight; } const char * Person::getName() const{ return name; } void Person::setName(const char * name){ strcpy(this->name, name); } void Person::info() const{ //printf("%s\n%d\n%c\n%d\n",name,age,sex,weight); printf(" this is a person\n"); } //--------构造函数:初始化成员变量------------ Person::Person(){ printf("call the functon Person()\n"); //name 改成指针的时候 name 没有有效地址 。strcpy 报错 //strcpy(name, "a man"); //在堆里分配内存,返回首地址,在堆里面分配的空间一定要记得释放内存!!!!! name = new char[255]; strcpy(name, "a man"); weight=60; age=20; sex='m'; } //--------构造函数:重载------------ Person::Person(int age){ printf("call the functon Person(int age)\n"); //name 改成指针的时候 name 没有有效地址 。strcpy 报错 //strcpy(name, "a man"); name = new char[255]; strcpy(name, "a man"); weight=60; this->age=age; sex='m'; } //------拷贝构造函数------- Person::Person(const Person & person){ /* 自己不实现的拷贝构造函数的话,系统生成的拷贝构造函数 this->name=person.name; this->age=person.age; this->weight=person.weight; this->sex=person.sex; */ // 自己实现的拷贝构造函数的话 //重新分配内存 this->name= new char[255]; strcpy(this->name, person.name); this->age=person.age; this->weight=person.weight; this->sex=person.sex; } //-----------析构函数--------- Person::~Person(){ //在析构函数里面释放内存 printf("call the functon ~Person()析构函数 \n"); delete [] name; } //----------操作符重载---------- Person & Person::operator=(const Person &right){ /* 自己不实现的操作符重载的话,系统生成的拷贝构造函数 this->name=right.name; this->age=right.age; this->sex=right.sex; this->weight=right.weight; */ //---------不需要再次分配内存------- // this->name= new char[255]; strcpy(this->name, right.name); this->age=right.age; this->sex=right.sex; this->weight=right.weight; return *this; } //-------不用在加static-------- void Person::print(){ printf("Person::print()\n"); } Person * Person::Create(){ Person * per= new Person(); return per; } Person * Person::Create(const char * name){ Person * per=Person::Create(); per->setName(name); return per; } //--------------student---------- //-------------函数名字一样:函数覆盖---------------- //void Student::info() const{ // // printf("%s\n%d\n%c\n%d %.2f\n",name,age,sex,weight,score); // // Person::info(); // //printf("%.2f\n",score); // printf(" this is a student\n"); // //} //---------子类的构造函数:不需要再调用父类的构造函数------ Student::Student(){ score=1.00f; } Student::~Student(){ printf(" call the functon ~Student()析构函数 \n"); } void Worker::info() const{ printf(" this is a worker\n"); }
// // main.cpp // ArrayTest // // Created by 张学院 on 14-1-6. // Copyright (c) 2014年 com.mix. All rights reserved. // #include <iostream> #include <string> #include "Person.h" int main() { Person::print(); Student::print(); Person *per=Person::Create(); per->info(); Person * per1= Person ::Create("child"); return 0; }
输出:
Person::print()
Person::print()
call the functon Person()
this is a person
call the functon Person()
相关文章推荐
- C++学习笔记1--基础知识
- 从Delphi转向VC系列学习一:C++基础知识
- Symbian C++学习笔记连载一:Symbian基础知识
- C/C++学习笔记:基础知识2
- 学习C++需要注意的十八个基础知识
- C++学习笔记1 - 预备知识
- 【C++】学习、复习知识框架
- C++学习2--坦克大战编写-前置知识
- C++ Primer 学习笔记-基础知识(二)
- C++学习笔记---------基础知识sizeof用法
- C++基础知识学习整理一
- C++学习笔记1--基础知识
- c++知识学习 (4)
- C++学习笔记——基础知识
- C++ primer plus学习:第一章 预备知识
- [C++学习]基本知识(1)
- 感觉还有好多的C++基础知识要去学习
- C++基础知识学习笔记(二)
- JNI学习之:C/C++基础知识学习
- 学习C++动态链接库基础 4000 知识 (主要关于QT项目)