您的位置:首页 > 编程语言 > C语言/C++

C++学习记录二

2017-02-13 15:59 141 查看
强制类型转化

C中:char *ptr;                    C++中:double num = 5;

         int *p = (int *)ptr;                      int count =static_cast<int>num;

指针类型间转化:reinterpret_cast<T>expr

Const int p = 5;

int num = const_cast<int>p;//去掉const属性

 

面向对象的优点:

       可维护,复用,扩展,灵活(活字印刷术)

 

面向对象的三大特点:封装(维护)、继承(复用)、多态(扩展)、[抽象也是特点]

 

类的声明:

class Test

{

       public:

              intx_;//能在类外访问

       protected:

              inty_;//类外不能访问

       private:

              intz_;//类外不能访问                                                                                                                                                                                                                                  
                                 } 

int main()

{

       Test t;

       t.x_= 5;//public可能在类外进行访问

       cout<< “t.x_ = ” << t.x_ << endl;

       return0;

                     =>t.x_= 5

       t.y_= 6;//成员Test::y_不可访问

       t.z_= 7;//同上

}

 

不能在类内直接初始化

命名规范:

1、 类的命名:第一个字母大写

2、 类内成员:名字_,如x_、y_、z_

3、 函数:驼峰命名,第一个单词以小写字母开始,每第二个单词的首字母大写或每个单词首字母均大写

 

类是一种用户自定义的类型,声明形式:

class 类名称

{

       public:

              公有成员(外部接口)类与外部的接口,任何外部函数都可以访问公有类型数据和函数

       private:

              私有成员 只允许本类中的函数访问

       protected:

              保护成员 与private类似,差别:继承与派生时对派生类的影响不同

};

 

*struct与class

1、用struct声明类,如果对其成员不作private或public的声明,系统将其默认为public(公用的);

2、用class定义的类,如果不作private或public声明,系统将其默认为private(私有的);

3、结构体也支持限定,不能继承,类可继承

 

#include <iostream>

using namespace std;

Class Test

{

public:

       intx_;

       voidsetX(int x)

       {

              x_= x;

}

void setY(int y)

{

       y_ = y;

}

void setZ(int z)

{

       z_ = z;

}

int getx()

{

       return x_;

}

int gety()

{

       return y_;

}

int getz()

{

       return z_;

}

}

 

int main()

{

       Testt;

       t.setX(5);

       t.setY(6);

       t.setZ(7);

       cout<< “t.x_” << t.getX() <<endl;

       cout<< “t.y_” << t.getY() << endl;

       cout<< “t.z_” << t.getZ() << endl;

       return0;

}

 

结果:

t.x_ = 5

t.y_ = 6

t.z_ = 7

 

 

类内定义的成员函数,编译器默认为inline

类内成员函数可互相调用

成员函数定义在类外

Test.h Test.cpp  Main.cpp      g++ Main.cpp  Test.cpp  -o Main

 

Test.h

#ifndef _TEST_H
#define _TEST_H_
 
class Test
{
         int x_;
         int y_;
         int z_;
public:
         voidinitXYZ(int x,
inty, int z);
         voiddisplayXYZ();
};
 
#endif
 

 

Test.cpp

#include
<iostream>
#include
"Test.h"
 using namespace std;
 void Test::initXYZ(intx,
int y, intz)
{
  x_ = x;
         y_ = y;
         z_ = z;
}
 void Test::displayXYZ()
{
         cout << "t.x_= " << x_ << endl;
         cout << "t.y_= " << y_ << endl;
         cout << "t.z_= " << z_ << endl;
}
 

Main.cpp

#include
<iostream>
#include
"Test.h"
 
int main()
{
         Test t;
         t.initXYZ(1,2,3);
         t.displayXYZ();
         return0;
}
 

 

结果:

t.x_ = 1

t.y_ = 2

t.z_ = 3

 

 

成员函数可类内实现(默认inline)也可类外实现(一般采用类外)

封装特点:保护成员属性

Test* t1 = new Test();

t1->initXYZ(1,2,4);

 

对象的存储模型

*对象的大小只与成员属性有关,与类中的方法无关       Test t1;

与结构体一样的对齐方式。对象的大小有成员决定            Test t2;

方法共享在同一段空间

 

不同的对象使用的是同一个函数代码段,利用This指针来指向不同的对象

this指针:

保留调用对象地址,自动传参

传参时间:调用方法的时候,动态的调用完消失

 

前向声明:

       当一个类中要调用另一个类时

       ①包括头文件(代价较大)

       ②前向声明不能含有对象的实体,即前向声明的类不能实例化。一定要是对象的指针或引用。

A.h

#ifndef _A_H_
#define _A_H_
 #include
"B.h"//加头文件
 class A
{
public:
       A();
       ~A();
private: //加头文件
       B b; //加头文件
};
 #endif

B.h

#ifndef _B_H_
#define _B_H_
 class A;//前向声明
 class B
{
public:
       B();
       ~B();
private: //前向声明
       A *a; //前向声明
};
 #endif

A.cpp

#include
<iostream>
#include
"A.h"
 using namespace std;
 A::A()
{
       cout << "initA!"<< endl;
}
 A::~A()
{
       cout << "destoryA!" << endl;
}
 

B.cpp

#include
<iostream>
#include
"B.h"
 using namespace std;
B::B()
{
       cout << "initB!" << endl;
}
B::~B()
{
       cout << "destoryB!" << endl;
}

 Main.cpp

#include
<iostream>
#include
"A.h"
#include
"B.h"
 int main()
{
       return 0;
}
 
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++