c ++类指针与类对象转化_C ++类和对象简介

c ++类指针与类对象转化

The classes are the most important feature of C++ that leads to Object Oriented programming. Class is a user defined data type, which holds its own data members and member functions, which can be accessed and used by creating instance of that class.

类是C ++导致面向对象编程的最重要功能。 类是用户定义的数据类型,它拥有自己的数据成员和成员函数,可以通过创建该类的实例来访问和使用它们。

The variables inside class definition are called as data members and the functions are called member functions.

类定义内的变量称为数据成员,而这些函数称为成员函数。

For example: Class of birds, all birds can fly and they all have wings and beaks. So here flying is a behavior and wings and beaks are part of their characteristics. And there are many different birds in this class with different names but they all posses this behavior and characteristics.

例如:鸟类类别,所有鸟类都可以飞翔,并且都具有翅膀和喙。 因此,在这里飞行是一种行为,而机翼和喙是其特征的一部分。 此类中有许多不同的鸟类,它们的名称不同,但是它们都具有这种行为和特征。

Similarly, class is just a blue print, which declares and defines characteristics and behavior, namely data members and member functions respectively. And all objects of this class will share these characteristics and behavior.

同样,类只是一个蓝图,它声明并定义了特征和行为,分别是数据成员和成员函数。 并且此类的所有对象将共享这些特征和行为。

有关课程的更多信息 (More about Classes)

  1. Class name must start with an uppercase letter(Although this is not mandatory). If class name is made of more than one word, then first letter of each word must be in uppercase. Example,

    类名必须以大写字母开头(尽管这不是必需的)。 如果类名由多个单词组成,则每个单词的首字母必须大写。 例子

    class Study, class StudyTonight etc
  2. Classes contain, data members and member functions, and the access of these data members and variable depends on the access specifiers (discussed in next section).

    类包含数据成员和成员函数,对这些数据成员和变量的访问取决于访问说明符(在下一节中讨论)。

  3. Class's member functions can be defined inside the class definition or outside the class definition.

    类的成员函数可以在类定义内部定义,也可以在类定义外部定义。

  4. Class in C++ are similar to structures in C, the only difference being, class defaults to private access control, where as structure defaults to public.

    C ++中的类类似于C中的结构,唯一的区别是,类默认为私有访问控制,而结构默认为公共。

  5. All the features of OOPS, revolve around classes in C++. Inheritance, Encapsulation, Abstraction etc.

    OOPS的所有功能都围绕C ++中的类。 继承,封装,抽象等

  6. Objects of class holds separate copies of data members. We can create as many objects of a class as we need.

    类的对象保存数据成员的单独副本。 我们可以根据需要创建任意数量的类对象。

  7. Classes do posses more characteristics, like we can create abstract classes, immutable classes, all this we will study later.

    类确实具有更多的特征,例如我们可以创建抽象类,不可变类,所有这些我们将在以后进行研究。

类的对象 (Objects of Classes)

Class is mere a blueprint or a template. No storage is assigned when we define a class. Objects are instances of class, which holds the data variables declared in class and the member functions work on these class objects.

类仅仅是一个蓝图或模板。 当我们定义一个类时,没有分配存储空间。 对象是类的实例,其中包含在类中声明的数据变量,并且成员函数对这些类对象起作用。

Each object has different data variables. Objects are initialised using special class functions called Constructors. We will study about constructors later.

每个对象都有不同的数据变量。 使用称为构造函数的特殊类函数初始化对象。 稍后我们将研究构造函数。

And whenever the object is out of its scope, another special class member function called Destructor is called, to release the memory reserved by the object. C++ doesn't have Automatic Garbage Collector like in JAVA, in C++ Destructor performs this task.

每当对象超出其范围时,就会调用另一个称为Destructor的特殊类成员函数,以释放该对象保留的内存。 C ++没有JAVA中的自动垃圾收集器,在C ++中,Destructor执行此任务。

class Abc
{
    int x;
    void display()
    {
        // some statement
    } 
};  

int main()
{
    Abc obj;   // Object of class Abc created
}

翻译自: https://www.studytonight.com/cpp/class-and-objects.php

c ++类指针与类对象转化

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值