📜  类和对象的区别

📅  最后修改于: 2021-09-12 11:10:57             🧑  作者: Mango

在本文中,我们将讨论类和对象之间的区别。

类是导致面向对象编程的基石。它是一种用户定义的数据类型,拥有自己的数据成员和成员函数,可以通过创建该类的实例来访问和使用它们。它是任何对象的蓝图。

例如:考虑账户类别。可能有许多具有不同名称和类型的帐户,但它们都有一些共同的属性,因为它们都有一些共同的属性,如余额、帐户持有人姓名等。所以在这里, Account是类,而balance ,账户持有人的姓名是他们的财产。

对象对象是类的实例。类的所有数据成员和成员函数都可以在对象的帮助下访问。定义类时,不分配内存,但实例化(即创建对象)时,分配内存。例如,考虑 Account 类的对象是 Saving account、Current account 等。

类和对象的区别:

对象和类之间有很多区别。下面给出了对象和类之间的一些区别:

S. No. Class Object
1 Class is used as a template for declaring and 
creating the objects.      
An object is an instance of a class.
2 When a class is created, no memory is allocated. Objects are allocated memory space whenever they are created.
3 The class has to be declared only once. An object is created many times as per requirement.
4 A class cannot be manipulated as they are not
available in the memory.
Objects can be manipulated.
5 A class is a logical entity. An object is a physical entity.
6 It is declared with the class keyword It is created with a class name in C++ and 
with the new keywords in Java.
7 Class does not contain any values which 
can be associated with the field.
Each object has its own values, which are
associated with it.
8 A class is used to bind data as well as methods together as a single unit. Objects are like a variable of the class.
9

Syntax for Declaring Class in C++:

class {

};

Syntax for Instantiating an object for a Class in C++:

class Student {

   public:

      void put(){

          cout<<“Function Called”<

      }

};   // The class is declared here

int main(){

         Student s1;   // Object created

         s1.put();

}

10 Example: Bike Example: Ducati, Suzuki, Kawasaki