Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Comments
enhzflep 13-Jul-12 7:28am    
You have to actually have a car before you can call the openDoor method on it. Same thing goes for a c++ class.
[no name] 13-Jul-12 7:37am    
That's a 5
Albert Holguin 13-Jul-12 10:26am    
OP posted as solution:
Thanks a lot to You all for quick response............

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.
 
Share this answer
 
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 is the class. myString is the object.

it's also valid to say that myString is a variable of type CString.
 
Share this answer
 
v2
Comments
Manas Bhardwaj 16-Jul-12 5:55am    
+5
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.
 
Share this answer
 
Comments
Manas Bhardwaj 16-Jul-12 5:55am    
good +5
Prasad_Kulkarni 16-Jul-12 8:29am    
Thank you Manas!
1) Class/objects: An object is anything exists physical in the real world.
An objects contains properties and performs actions . properties are represented by variables actions are represented by methods.
2) A method is a function written inside a class.
3) An object contain variables and methods .
4) A class represents a group name that specifies properties and actions of objects.
5) A class is a model (or) plan for creating the object.
6) Class does not exist physically.
7) Object exists physically.
8) An object is an instance of a class .instance means physically happening.
9) An object does not exist without a class.
10) But a class can exists without an object.
 
Share this answer
 
If you say "car", it is a class. If you say "my car" or "that red car", this is an object. Technically speaking an object is an instance of a class.
In terms of hierarchy, you can say, car is a vehicle. Vehicle is also a class of objects, not an instance of objects. The spacial shuttle is also a vehicle. Bicycle, motorbike, aircraft, boat, are vehicles as well. They are classes, not instances. Saying it in C++, the class car inherit the class vehicle. The classes Bicycle, motorbike, aircraft, helicopter, boat also inherit the class vehicle. Mercedes inherits from car.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900