• c++ 类和对象


    一、类是对应于自然界中的实体,包含属性和操作。比如:以人为例,一个人具有身高、体重、性别等属性,一个人的性格,做事的方法就是操作。

     1.  3种成员访问限定符(访问权限):public,protected,private.

          private私有只允许类内访问,类外不可访问。

          protected保护类型,允许类内和子类访问。

          public 共有都可以访问。

     **1)const static的数据是在声明类的时候直接初始化的,非const的static的数据必须在类外面。

        2)禁止在类中实例化非const static member.

        3)在类中不使用任何访问权限的时候,默认为private.

      代码示例:

    #include <stdio.h>
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Car{
        public:
       int GetType(){
          return type;
       }
       string GetName(){
         return name;
       }
       static void setType(int type2){
           type = type2;
       }
       static string name;
       //static string name="car1";
       static int type;
    
        private:
            int price;
    };
    /*这两个变量在这个文件定义了,如果还在其他别的文件里定义,就会出错误,multiple define多重定义!!*/
    //int Car::type=1;
    string Car::name="car2";
    int main(){
      Car::setType(5);
      cout<<"car1的name: "<<Car::name<<endl;
      cout<<"car1的type:"<<Car::type<<endl; //undefined reference to 'Car::type'
    
      return 0;
    }
    

      

     2.可以将类的声明和实现分离:

       例如:

       XX.h: class Test{

       public:int GetNum();

       int num;

      };

      XX.cpp:

       int Test::GetNum(){

        return num;

       }

       说这样分开定义看着方便,不过我没感觉出来,还是习惯把定义声明写在一块。

       test.h:

    #include <iostream>
    #ifndef TEST_H_INCLUDED
    #define TEST_H_INCLUDED
    using namespace std;
    class CTest{
      public:
          int m_num;
          int Getnum();
          void setNum(int a);
    };
    #endif // TEST_H_INCLUDED
    

      main.cpp:

    #include <iostream>
    #include "test.h"
    
    /*普通对象传递是值传递,不会改变原来对象的值,用指针对象才可以。
    */
    //外联函数
    int CTest::Getnum(){
        return m_num;
    }
    void CTest::setNum(int a){
       m_num=a;
    }
    
    void ShowNum(CTest test){    //值传递,改变了它的值,跟原来的没有关系
      cout<<"类CTest中的成员变量num:"<<test.m_num<<endl;
    
      cout<<"类CTest中的成员函数的返回值:"<<test.Getnum()<<endl;
      cout<<"ShowNum函数中类CTest的地址:"<<&test<<endl;
      cout<<"输入一个整数:"<<endl;
      cin>>test.m_num;
    }
    void ShowNum2(CTest test){
      cout<<"类CTEst中的成员变量num:"<<test.m_num<<endl;
    }
    
    void ShowNum(CTest *test){    //地址传递
      cout<<"类CTest中的成员变量num:"<<test->m_num<<endl;
    
      cout<<"类CTest中的成员函数的返回值:"<<test->Getnum()<<endl;
    
      cout<<"指针对象输入一个整数:"<<endl;
      cin >> test->m_num;
    }
    
    void Ctestprint(){
     cout <<"类t1"<<endl;
     CTest t1;
     cout<<"类t1的地址:"<<&t1<<endl;
     cout<<"输入一个整数:";
     cin >> t1.m_num;
     cout << "调用ShowNum函数:"<<endl;
     ShowNum(t1);
     ShowNum2(t1);
     cout <<"类t2"<<endl;
     CTest t2;
     cout<<"输入一个整数:";
     cin >> t2.m_num;
     cout << "调用ShowNum函数:"<<endl;
     ShowNum(t2);
     ShowNum2(t2);
    }
    int main()
    {
        cout << "Hello world!" << endl;
        Ctestprint();
    
        cout<<"使用类的指针对象"<<endl;
        CTest *ct= new CTest();
        ct->setNum(5);
        ShowNum(ct);
        cout<<"指针对象的num:"<<ct->m_num<<endl;
        return 0;
    }
    

     3.类成员

     static关键字:由于静态成员必须有确定的值,但由于在类的定义中不能对成员数据直接进行初始化,故必须在类定义的外部对静态数据成员再声明一次,并进行初始化,此时前边不需要加关键字static。

     在程序开始运行时就为静态数据成员分配存储空间,但它只有类的作用域。即在main()之前,首先对静态成员数据和全局变量分配存储空间并进行初始化,当整个程序结束时才撤销静态成员数据和全局变量。因此,在创建任何对象之前,类的静态成员数据已经存在  并可以引用。

     格式:<类名>::<静态成员数据>

       例:Car::name;

            Car::Add();

     4.友元函数:

      一个普通函数或一个类中的成员函数可以访问其他类中的死有和保护成员。

     例如:

     class Y{

     friend void X::disp(Y y);//X类的disp()函数为本类的友元函数。

     。。。。。

     };

     class X{

      void disp(Y y){

        ........

        }

     };

    还有一个友元类,一个类是另一个类的友元,一个类中的任意一个成员函数都可以访问另一个类的私有成员。不过感觉这么设计不怎么好,有想仔细了解的可以研究下,这里就不上代码啦。。

    还是上吧:

    #include <iostream>
    
    using namespace std;
    
    class YourClass{
      friend class YourOtherClass;//指定YourOtherClass是该类的友元类。
      private:
          int num;
      public:
        YourClass(int n){
          num=n;
        }
        void display(char *YCname){
            cout<<YCname<<".num"<<endl;
            cout<<num<<endl;
    
        }
     };
    
    class YourOtherClass{
       public:
           void display1(YourClass yc,char *YCname){
              cout<<YCname<<".num:"<<endl;
              cout<<yc.num<<endl;
              cout<<"通过public函数调用:"<<endl;
              yc.display(YCname);
           }
    
    };
    
    int main()
    {
        cout << "Hello world!" << endl;
        YourClass a(5),b(10);
        YourOtherClass yoc;
    
        a.display("a");
        yoc.display1(a,"a");
    
    
        return 0;
    }
    

      

  • 相关阅读:
    Beyond_Compare 4.2.3中文版下载及密钥(亲测可用)
    ORA-0131:Insufficient privileges(Oracle授予用户DEBUG权限)
    常见正则表达式
    MySQL导入导出SQL文件(txt文件)
    存储过程(总结)
    怎样优化数据更新、访问量大的数据库(总结)
    索引原理(平衡树数据结构)
    Oracle面试题
    Linux系统没有IP地址
    iwlist/iwconfig/iw命令
  • 原文地址:https://www.cnblogs.com/jycboy/p/5188842.html
Copyright © 2020-2023  润新知