Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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
Posted 13-Jul-12 1:21am
Comments
enhzflep at 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.
Wes Aday at 13-Jul-12 7:37am
   
That's a 5
Albert Holguin at 13-Jul-12 10:26am
   
OP posted as solution:
Thanks a lot to You all for quick response............
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v2
Comments
Manas Bhardwaj at 16-Jul-12 5:55am
   
+5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 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.
  Permalink  
Comments
Manas Bhardwaj at 16-Jul-12 5:55am
   
good +5
Prasad_Kulkarni at 16-Jul-12 8:29am
   
Thank you Manas!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 446
1 OriginalGriff 325
2 Yogesh Kumar Tyagi 304
3 Maciej Los 235
4 Prakriti Goyal 200
0 OriginalGriff 6,417
1 Sergey Alexandrovich Kryukov 5,344
2 Maciej Los 3,434
3 Peter Leow 3,259
4 DamithSL 2,490


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 13 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100