zoukankan      html  css  js  c++  java
  • c++知识学习 (7)

    静态成员函数(static)

    静态成员函数的声明是在类体中的函数声明前加上关键字static

    例如

    static Person * create(const string name);

    *在静态函数中不能使用this指针。

    不需实例化调用。可以直接用类名调用。

     

     1 //
     2 //  Person.h
     3 //  ArrayTest
     4 //
     5 //  Created by 张学院 on 14-1-8.
     6 //  Copyright (c) 2014年 com.mix. All rights reserved.
     7 //
     8 
     9 //防止重复引用
    10 #ifndef __ArrayTest__Person__
    11 #define __ArrayTest__Person__
    12 
    13 #include <iostream>
    14 using namespace std;
    15 class Person{
    16 //---------成员变量--------
    17 public :
    18     
    19     int age;
    20     
    21 private :
    22     int weight;
    23     char  * name;
    24     char sex;
    25 //---------成员方法--------
    26 public:
    27     //----构造函数-------
    28     Person();
    29     //-----构造函数重载-----
    30     Person(int age);
    31     
    32     //------拷贝构造函数-------
    33     Person(const Person & per);
    34     
    35     //----------操作符重载----------
    36     Person & operator=(const Person &right);
    37     
    38     void setWeight(int weight);
    39     int getWeight() const;
    40     //char * getName() const;
    41     //const 指针,保证指针不被修改
    42     const char * getName() const;
    43     void setName(const char * name);
    44     //-----虚函数:子类的可能会重写父类的的方法,当父类的指针指向子类的对象,子类对象调用的是子类自己的方法--------
    45     virtual void info() const;
    46     //-----纯虚函数--------
    47     //virtual void info() const=0;
    48     ~Person();
    49     static void print();
    50     //---类方法重载----
    51     static Person * Create();
    52     static Person * Create(const char * name);
    53 };
    54 
    55 class Student:public Person{
    56 private:
    57     float score;
    58 public:
    59    // void info() const;
    60     Student();
    61     ~Student();
    62 };
    63 
    64 class Worker:public Person{
    65 public :
    66     void info() const;
    67 };
    68 #endif /* defined(__ArrayTest__Person__) */
      1 //
      2 //  Person.cpp
      3 //  ArrayTest
      4 //
      5 //  Created by 张学院 on 14-1-8.
      6 //  Copyright (c) 2014年 com.mix. All rights reserved.
      7 //
      8 
      9 #include "Person.h"
     10 //------方法实现格式------
     11 //----返回值 类::方法名 (参数)------
     12 void Person::setWeight(int weight){
     13     //-----this 代表
     14     this->weight=weight;
     15 }
     16 //--------const 编译限制-------
     17 int  Person::getWeight() const{
     18     //weight++;报错
     19     return weight;
     20 }
     21 const char * Person::getName() const{
     22     
     23     return name;
     24     
     25 }
     26 void Person::setName(const char * name){
     27 
     28     strcpy(this->name, name);
     29     
     30 }
     31 void Person::info() const{
     32 
     33     //printf("%s
    %d
    %c
    %d
    ",name,age,sex,weight);
     34     printf(" this is a person
    ");
     35 
     36 }
     37 //--------构造函数:初始化成员变量------------
     38 Person::Person(){
     39     printf("call the functon Person()
    ");
     40     //name 改成指针的时候 name 没有有效地址 。strcpy 报错
     41     //strcpy(name, "a man");
     42     //在堆里分配内存,返回首地址,在堆里面分配的空间一定要记得释放内存!!!!!
     43     name = new char[255];
     44     strcpy(name, "a man");
     45     weight=60;
     46     age=20;
     47     sex='m';
     48 }
     49 //--------构造函数:重载------------
     50 Person::Person(int age){
     51     printf("call the functon Person(int age)
    ");
     52     //name 改成指针的时候 name 没有有效地址 。strcpy 报错
     53     //strcpy(name, "a man");
     54     name = new char[255];
     55     strcpy(name, "a man");
     56     weight=60;
     57     this->age=age;
     58     sex='m';
     59     
     60 }
     61 
     62 //------拷贝构造函数-------
     63 Person::Person(const Person & person){
     64 /* 自己不实现的拷贝构造函数的话,系统生成的拷贝构造函数
     65     this->name=person.name;
     66     this->age=person.age;
     67     this->weight=person.weight;
     68     this->sex=person.sex;
     69 */
     70  // 自己实现的拷贝构造函数的话
     71     //重新分配内存
     72     this->name= new char[255];
     73     strcpy(this->name, person.name);
     74     this->age=person.age;
     75     this->weight=person.weight;
     76     this->sex=person.sex;
     77     
     78 }
     79 
     80 //-----------析构函数---------
     81 Person::~Person(){
     82     //在析构函数里面释放内存
     83     printf("call the functon ~Person()析构函数 
    ");
     84     delete [] name;
     85    
     86 }
     87 
     88 //----------操作符重载----------
     89 Person & Person::operator=(const Person &right){
     90     /* 自己不实现的操作符重载的话,系统生成的拷贝构造函数
     91     
     92     this->name=right.name;
     93     this->age=right.age;
     94     this->sex=right.sex;
     95     this->weight=right.weight;
     96       */
     97     //---------不需要再次分配内存-------
     98    // this->name= new char[255];
     99     strcpy(this->name, right.name);
    100     this->age=right.age;
    101     this->sex=right.sex;
    102     this->weight=right.weight;
    103     return *this;
    104 }
    105 //-------不用在加static--------
    106 void Person::print(){
    107     printf("Person::print()
    ");
    108 
    109 }
    110 
    111 Person * Person::Create(){
    112 
    113     Person * per= new Person();
    114     return per;
    115 }
    116 Person * Person::Create(const char * name){
    117     Person * per=Person::Create();
    118     per->setName(name);
    119     return per;
    120 }
    121 //--------------student----------
    122 //-------------函数名字一样:函数覆盖----------------
    123 //void Student::info() const{
    124 //   // printf("%s
    %d
    %c
    %d %.2f
    ",name,age,sex,weight,score);
    125 //   // Person::info();
    126 //    //printf("%.2f
    ",score);
    127 //    printf(" this is a student
    ");
    128 //
    129 //}
    130 //---------子类的构造函数:不需要再调用父类的构造函数------
    131 Student::Student(){
    132     score=1.00f;
    133 
    134 }
    135 Student::~Student(){
    136     printf(" call the functon ~Student()析构函数 
    ");
    137 
    138 }
    139 
    140 void Worker::info() const{
    141     printf(" this is a worker
    ");
    142 
    143 }
     1 //
     2 //  main.cpp
     3 //  ArrayTest
     4 //
     5 //  Created by 张学院 on 14-1-6.
     6 //  Copyright (c) 2014年 com.mix. All rights reserved.
     7 //
     8 
     9 #include <iostream>
    10 
    11 #include <string>
    12 #include "Person.h"
    13 
    14 int main()
    15 {
    16    
    17     Person::print();
    18     Student::print();
    19     
    20     Person *per=Person::Create();
    21     per->info();
    22     
    23     Person * per1= Person ::Create("child");
    24     return 0;
    25 }

    输出:

    Person::print()

    Person::print()

    call the functon Person()

     this is a person

    call the functon Person()

  • 相关阅读:
    git学习02
    每日一记8.12
    git学习01
    每日一记8.7
    每日一记8.6
    spring boot使用tomcat启动
    每日一记8.1
    【学习笔记】HTML5 WebGL游戏引擎开发
    【转】使用 WebGL 进行 3D 开发,第 3 部分: 添加用户交互
    【转】使用 WebGL 进行 3D 开发,第 2 部分: 使用 WebGL 库以更少的编码做更多的事情
  • 原文地址:https://www.cnblogs.com/aosting/p/3513780.html
Copyright © 2011-2022 走看看