关于对象的认识 [英] What to know about Object

查看:75
本文介绍了关于对象的认识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好...

我是C ++的新手,并且了解什么知识.我知道
每个类都需要一个对象来访问该类的所有成员和成员函数.为什么类本身不直接调用成员函数?
所以,任何人都可以说为什么我们需要创建一个对象吗?

谢谢您

Hi Everyone......

I am novice in C++ and what to gain some knowledge.I came to know that
every class needs an object to access all members and member functions of that class.Why not member functions directly called by class itself ???

So, can any one say why we need to create an object???

Thanking You

推荐答案

类不是可以使用的物品,它是物品的描述:物品的规格.

您需要一个类的实例(即特定对象)才能执行该操作.这样想吧-不需要物体会发生什么?第一次尝试该方法效果很好,但是如果您尝试使用第二类,那么影响一个也会影响另一个.

考虑一下汽车.汽车是一个抽象的概念-您不能开车去汽车商店,必须指定要谈论的汽车:您的汽车,或者如果允许的话,请指定我的汽车.其他明智的做法是介入三审-警车.

如果您不必指定汽车的实例,那么总会出现混乱,因为当我将脚放在汽车上时,您的汽车也会加速.如果红绿灯刚刚变红就不好了...

话虽如此,并非所有成员和函数都必须与实例相关-它们可以为static,这意味着您可以直接通过类访问它们,而不需要实例.回到汽车上,一个静态的例子是汽车有多少个轮子?"
-由于所有汽车都有四个轮子,因此您无需实例即可提问或回答问题. 汽车是什么颜色的?"这不是一个静态的问题,因为汽车不能具有颜色-只有一个实例可以:您的汽车是红色,我的汽车是蓝色,那辆汽车如果是绿色,这辆汽车是黄色,等等.
A class is not an item you can do anything with - it is the description of an item: the specification of an item.

You need an instance of a class (i.e. a specific object) in order to do things with it. Think of it this way - what would happen is you didn''t need an object? It would work fine for the first time you tried it, but if you tried to have a second class item, then affecting one would also affect the other.

Think about cars. A Car is an abstract concept - you can''t drive to the shops in a Car, you have to specify which Car you are talking about: your Car, or my Car if I let you have the keys. Other wise a third instance gets involved - a police Car.

If you didn''t have to specify an instance of a car, there would be total chaos, because when I put my foot down in my car, your car would accelerate as well. Not so good if the traffic lights have just gone red...

Having said that, not all members and functions have to be instance related - they can be static which mean syou can access them directly via the class, rather than needing an instance. Going back to cars, a static example would be "how many wheels has a car?"
- since all cars have four wheels, you do not need an instance to ask, or answer, the question. "what color is a car?" is not a static question, because a car cannot have a color - only an instance can: your car is red, my car is blue, that car if green, this car is yellow and so on.


类是可以同时包含数据和方法的数据结构的定义.它定义了该类型的对象可以做什么.

对象是类的实例.它执行类定义所说明的功能.

为什么类本身不直接调用成员函数?

类"只是一个定义,它不能调用任何东西.类的对象可以调用事物.

这是马"一词与实际生活中的呼吸马之间的区别. 马"定义什么是马.但这是抽象的,您无法骑乘马的概念. 马"是阶级.您前面的马是对象(在C ++中也称为实例").它完成了马"一词所暗示的一切:您可以骑它.

a class is a definition of a data structure that can hold both data and methods. it defines what objects of the type can do.

an object is an instance of a class. it does what the class definition says it can do.

Why not member functions directly called by class itself ???

a ''class'' is simply a definition, it cannot call anything. an object of the class can call things.

it''s the difference between the word "horse" and an actual living, breathing horse. "horse" defines what a horse is. but it''s abstract, you cannot ride the concept of a horse. "horse" is the class. the horse in front of you is the object (also known, in C++ as the "instance"). it does everything the word "horse" implies: you can ride it.

CString myString;



CString是类. myString是对象.

也可以说myString是CString类型的变量.



CString is the class. myString is the object.

it''s also valid to say that myString is a variable of type CString.


object 是程序的组件,该程序知道如何执行某些操作并与程序的其他部分进行交互.
对象可以知道如何完成一个以上的特定任务,并且可以存储自己的数据集.

使用对象设计程序,程序员可以根据实际情况对程序进行建模.程序可以分解为特定的部分,而这些部分中的每一个都可以执行相当简单的任务.当所有这些简单的片段都网格化成一个程序时,它可以产生一个非常复杂和有用的应用程序.
对象是将程序的各个部分捆绑为可管理的小块的方法.

对象只是对要存储的数据类型的定义.

对象的实例包含有意义的信息,这些信息由程序操纵.

一个对象可以有多个实例.

对象实例 跟踪信息,称为成员数据或实例变量. 实例直到不再存在都会跟踪该数据.

对象实例还知道如何执行某些功能,这些功能称为成员功能或类功能.

对象的每个实例在执行成员函数时都执行相同的步骤,尽管这些步骤可能会受到实例的当前成员数据的影响.
An object is a component of a program that knows how to perform certain actions and to interact with other pieces of the program.
Objects can know how to do more than one specific task, and they can store their own set of data.

Designing a program with objects allows a programmer to model the program after the real world. A program can be broken down into specific parts, and each of these parts can perform fairly simple tasks. When all of these simple pieces are meshed together into a program, it can produce a very complicated and useful application.

Objects are ways of bundling parts of programs into small, manageable pieces.

Objects are simply a definition for a type of data to be stored.

An instance of an object contains meaningful information, these are manipulated by the program.

There can be more than one instance of an object.

Instances of objects keep track of information, called member data, or instance variables. This data is kept track of by the instance until it no longer exists.

Object instances also know how to perform certain functions, called member functions, or class functions.

Every instance of an object performs the same steps when carrying out a member function, although the steps can be influenced by the instances'' present member data.


这篇关于关于对象的认识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆