zoukankan      html  css  js  c++  java
  • c++ basic 整理1

     //c++类 初始化 与 初始赋值
     //C++规定 对象的 成员变量 初始化 动作发生 在进入 构造函数本体 之前 。 在构造函数内 成员变量赋值都属于 赋值 。
     class People {
         public:
         People( std::string name, int age, int height );
         private:
         std::string m_sName;
         int m_iAge;
         int m_iHeight;
     }
     /*
     * 赋值
     * 赋值时首先调用默认构造函数为m_sName,m_iAge,m_iHeight赋初始值,然后在立刻调用赋值操作符进行赋新值。
     */
     People::People( std::string name, int age, int height )
     {
         m_sName = name;
         m_iAge = age;
         m_iHeight = height;
     }
    
    
      /*
      * 初始化列表
      * 成员初始列表是将各个成员变量实参都作为复制构造函数的实参
      */
     People::People( std::string name, int age, int height )
         : m_sName( name ), m_iAge( age ), m_iHeight( height )
     {
     } /* 所以看出赋值相对于初始化,多了一步就是使用赋值操作符进行赋值。所以初始化的效率比赋值的效率高多了。但是对于内置类型,它们效率是一样的。 */
    
    
    /* 多态调用 */
    #include <iostream>
     using namespace std;
    
     class A
     {
         public:
         void foo()
         {
             printf( "1
    " );
         }
         virtual void fun()
         {
             printf( "2
    " );
         }
     };
     class B : public A
     {
         public:
         void foo()
         {
             printf( "3
    " );
         }
         void fun()
         {
             printf( "4
    " );
         }
     };
     int main( void )
     {
         A a;
         B b;
         A *p = &a;
    
         p->foo();  /* 1 */
         p->fun();  /* 2 */
         p = &b;
         p->foo();  /* 1//非虚方法和默认参数都是静态绑定,在继承关系中只跟指针类型有关 */
         p->fun();  /* 4//虚函数动态绑定, 跟指针类型相关 */
    
         B *q = (B *) &a;
         q->foo();  /* 3 */
         q->fun();  /* 2 */
    
         system( "PAUSE" );
         return(0);
     }
    
    /*
    * 友元函数
    * 定义:友元函数是指某些虽然不是类成员却能够访问类的所有成员的函数。
    */
    #include <iostream>
     using namespace std;
    
     class Point;
    
     class Test
     {
         public:
         int priority( Point & p ); /* 因为先声明Point,所以这里不出错 */
     };
     class Point
     {
         public:
         Point( double xx, double yy, int pri )
         {
             x = xx; y = yy; priority = pri;
         }
         void Getxy();
         friend double Distance( Point & a, Point & b );    /* 申明全局函数作友元,全局函数的定义必须放在ca这个类的后面 */
         friend int Test::priority( Point & p );            /* 申明申明Test类的priority()函数作友元作友元,全局函数的定义必须放在ca这个类的后面 */
         private:
         double x, y;
         int priority;
     };
    
    
     void Point::Getxy()
     {
         cout << "(" << x << "," << y << ")" << endl;
     }
     double Distance( Point & a, Point & b )    /* 全局函数 */
     {
         double dx = a.x - b.x;
         double dy = a.y - b.y;
         return(sqrt( dx * dx + dy * dy ) );
     }
     int Test::priority( Point & p )            /* //作友元函数必须在Point类的后面实现,否则会因为Point成员就未定义而出错; */
     {
         return(p.priority);
     }
    
     int main()
     {
         Point p1( 3.0, 4.0, 1 ), p2( 6.0, 8.0, 2 );
         p1.Getxy();
         p2.Getxy();
         double d = Distance( p1, p2 );
         Test t;
         int pri = t.priority( p1 );
         cout << "Distance is " << d << endl;
         cout << "p1 priority is " << pri << endl;
         system( "PAUSE" );
         return(0);
     }
    
    
    /*
    * (1)友元关系不可以继承,但对已有的方法来说访问权限不改变。
    *
    * (2)如果改写基类的方法则访问权限改变
    *
    * (3)友元关系不具有传递性
    *
    * 若类B是类A的友元,类C是B的友元,类C不一定是类A的友元。
    */
    给笨笨的自己提个醒>_<~
  • 相关阅读:
    php 面向对象编程实例 __construct 和 __destruct 区别
    php 数组 类对象 值传递 引用传递 区别
    WebHttpBinding.ReaderQuotas 无法设置或者无法点出来
    XP IE8 安装失败
    把XML保存为ANSI编码
    更新RDL文件中的数据集(DataSets)
    真实赛车3,FERRARI之魂不买FERRARI 599 GTO可以解锁顶点系列。
    [转]一大波“关于BUG的类型”的图片 + 一小波笑话
    sdk manager 代理,解决下载速度慢的问题
    错误 1 缺少编译器要求的成员“System.Runtime.CompilerServices.ExtensionAttrib
  • 原文地址:https://www.cnblogs.com/ephuizi/p/4371463.html
Copyright © 2011-2022 走看看