码迷,mamicode.com
首页 > 编程语言 > 详细

C++面向对象

时间:2019-02-18 23:19:02      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:函数调用   uri   ios   protected   通过   纯虚函数   auth   功能   mono   

类的成员声明与定义

声明一个类,则类就是一个类型,类不能直接访问其属性,这是与Python最大区别

#include "pch.h"
#include <iostream>
using namespace std;

struct Book
{
    char title[40];
    char author[40];
    char subject[40];
    int book_id;
};

class Box
{
public:  //声明公共属性
    double length;
    double height ;
    double weight = 30;
    void setlength(double let);   //声明函数不定义

    void setheight(double het)    //声明函数并定义
    {
        height = het;
    }

    double getvolumn(void)
    {
        return length * height * weight;
    }

};

void Box::setlength(double let)  //使用 :: 范围解析符定义函数
{
    length = let;
}



int main()
{
    double v;
    Box my_first_box;
    my_first_box.setheight(10);
    my_first_box.setlength(10);
    v = my_first_box.getvolumn();
    cout << v << endl;
}

 

类的访问修饰符

class Base {
 
   public:
 
  // 公有成员 类与实例、继承类都能访问
 
   protected:
 
  // 受保护成员 实例不能直接访问,可通过公有函数调用,但是派生类能访问
 
   private:
 
  // 私有成员 实例不能直接访问,但能通过公有函数调用,派生类不能访问
 
};

继承类型

派生类可以访问基类中所有的非私有成员。因此基类成员如果不想被派生类的成员函数访问,则应在基类中声明为 private。

我们可以根据访问权限总结出不同的访问类型,如下所示:

访问publicprotectedprivate
同一个类 yes yes yes
派生类 yes yes no
外部的类 yes no no

当一个类派生自基类,该基类可以被继承为 public、protected 或 private 几种类型。继承类型是通过上面讲解的访问修饰符 access-specifier 来指定的。

我们几乎不使用 protected 或 private 继承,通常使用 public 继承。当使用不同类型的继承时,遵循以下几个规则:

  • 公有继承(public):当一个类派生自公有基类时,基类的公有成员也是派生类的公有成员,基类的保护成员也是派生类的保护成员,基类的私有成员不能直接被派生类访问,但是可以通过调用基类的公有保护成员来访问。
  • 保护继承(protected): 当一个类派生自保护基类时,基类的公有保护成员将成为派生类的保护成员。
  • 私有继承(private):当一个类派生自私有基类时,基类的公有保护成员将成为派生类的私有成员。

C++ 中的函数重载

在同一个作用域内,可以声明几个功能类似的同名函数,但是这些同名函数的形式参数(指参数的个数、类型或者顺序)必须不同。您不能仅通过返回类型的不同来重载函数。

下面的实例中,同名函数 print() 被用于输出不同的数据类型:

#include <iostream>
using namespace std;
 
class printData
{
   public:
    //--------------------------------------------三个print同名函数,但参数不同则是不同的函数
void print(int i) { cout << "整数为: " << i << endl; } void print(double f) { cout << "浮点数为: " << f << endl; } void print(char c[]) { cout << "字符串为: " << c << endl; } }; int main(void) { printData pd; // 输出整数 pd.print(5); // 输出浮点数 pd.print(500.263); // 输出字符串 char c[] = "Hello C++"; pd.print(c); return 0; }

 

C++ 接口(抽象类)

接口描述了类的行为和功能,而不需要完成类的特定实现。

C++ 接口是使用抽象类来实现的,抽象类与数据抽象互不混淆,数据抽象是一个把实现细节与相关的数据分离开的概念。

如果类中至少有一个函数被声明为纯虚函数,则这个类就是抽象类。纯虚函数是通过在声明中使用 "= 0" 来指定的,如下所示:

class Box
{
   public:
      // 有纯虚函数则该类为抽象类
      virtual double getVolume() = 0;
   private:
      double length;      // 长度
      double breadth;     // 宽度
      double height;      // 高度
};

如下实例:

#include <iostream>
 
using namespace std;
 
// 基类
class Shape 
{
public:
   // 提供接口框架的纯虚函数
   virtual int getArea() = 0;
   void setWidth(int w)
   {
      width = w;
   }
   void setHeight(int h)
   {
      height = h;
   }
protected:
   int width;
   int height;
};
 
// 派生类
class Rectangle: public Shape
{
public:
   int getArea()
   { 
      return (width * height); 
   }
};
class Triangle: public Shape
{
public:
   int getArea()
   { 
      return (width * height)/2; 
   }
};
 
int main(void)
{
   Rectangle Rect;
   Triangle  Tri;
 
   Rect.setWidth(5);
   Rect.setHeight(7);
   // 输出对象的面积
   cout << "Total Rectangle area: " << Rect.getArea() << endl;
 
   Tri.setWidth(5);
   Tri.setHeight(7);
   // 输出对象的面积
   cout << "Total Triangle area: " << Tri.getArea() << endl; 
 
   return 0;
}

 

#include<iostream>usingnamespacestd; // 基类classShape{public: // 提供接口框架的纯虚函数virtualintgetArea() = 0; voidsetWidth(intw){width = w; }voidsetHeight(inth){height = h; }protected: intwidth; intheight; }; // 派生类classRectangle: publicShape{public: intgetArea(){return(width * height); }}; classTriangle: publicShape{public: intgetArea(){return(width * height)/2; }}; intmain(void){RectangleRect; TriangleTri; Rect.setWidth(5); Rect.setHeight(7); // 输出对象的面积cout << "Total Rectangle area: " << Rect.getArea() << endl; Tri.setWidth(5); Tri.setHeight(7); // 输出对象的面积cout << "Total Triangle area: " << Tri.getArea() << endl; return0; }

C++面向对象

标签:函数调用   uri   ios   protected   通过   纯虚函数   auth   功能   mono   

原文地址:https://www.cnblogs.com/echoboy/p/10398198.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!