Reputation:
What is a Class and Object in C++?
Can we say that a Class is an Object?
Upvotes: 15
Views: 64308
Reputation: 1
In object-oriented programming , a class is a template definition of the method s and variable s in a particular kind of object . ... Subclasses can also define their own methods and variables that are not part of their superclass. The structure of a class and its subclasses is called the class hierarchy.
Upvotes: 0
Reputation: 315
No,class is not a object.
A class is a data type and object is the variable(instance) of class data type.
Upvotes: 1
Reputation: 12140
I will try to give more technical explanation rather than an abstract one. I think that definitions like "a class is a blueprint and an object is something made from this blueprint" are impossible to understand for newbies simply because these kind of definitions are abstract and context-less.
Classes and objects have a pure abstract meaning in the object oriented world but for simplicity I will reduce the definition to a more practical one.
Consider the following statement:
int a;
"int" is a type and is "a" is a variable which has the type "int".
C++ provides various ways to let the programmer define new types; for example:
typedef int* int_ptr;
int_ptr a;
In this example , a new type is defined int_ptr. "int_ptr" is a type , "a" is a variable which has the type "int_ptr". Another example:
struct Point
{
int x;
int y;
};
Point a;
Here, a new type is defined, "Point", and "a" is a variable which has the type "Point".
So what is a class in C++? A class is another way to define a new type, just like the other ways mentioned above.
What is an object? An object is a variable which has a type that was defined using the class keyword.
For example:
class SmartPoint
{
public:
Point(x,y);
Move(x,y);
protected:
int x,y ;
};
SmartPoint a;
In this example, a new type is defined, "SmartPoint", and "a" is a variable which has the type "SmartPoint".
You may ask then what is different between a type defined by using the "class" keyword or "struct" keyword or "typedef" — but that is a matter for another discussion.
Upvotes: 6
Reputation: 15110
A Class is like a blueprint, an object is like a house built from that blueprint.
You can have many houses with the same layout/floorplan (read class), but each is it's own instance (read object). Each has it's own owner, furniture, etc.
Note that there are also objects whose blueprint is not a class (e.g. integers).
Upvotes: 52
Reputation: 1
Object is an data or a function which has an adress in run time memory and are also called as instances of class . Thus object is an class type variable or entity of c++ programming language.
Upvotes: 0
Reputation: 171
Here is an anology.
we have a classification called vehicles. Each vehicle will have some properties like :
Car, bike, truck, are some instances of vehicles. Each may have different set of properties.
Here vehicles is a class and all the properties are it's members and car, bike, truck are objects of the class vehicles.
Upvotes: 1
Reputation: 99
A class is a logical construct while object is its physical copy.
A class can be thought of as a mould from which multiple objects are created which appear identical to the class
Objects can be thought of as carbon copies of the class. A perfect example of inheritance principle.
A class can be thought of as a parent of its children - objects
Upvotes: 0
Reputation: 135
Class is collection of data and functions,its user defined data type.Class is specification for object.So you can say that object is variable for class or object is instance of class.
Upvotes: 0
Reputation: 1
When you define a class, you define a blueprint for a data type. This doesn't actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.
A class definition starts with the keyword class followed by the class name; and the class body, enclosed by a pair of curly braces. A class definition must be followed either by a semicolon or a list of declarations. For example, we defined the Box data type using the keyword class as follows:
class Box
{
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
The keyword public determines the access attributes of the members of the class that follow it. A public member can be accessed from outside the class anywhere within the scope of the class object. You can also specify the members of a class as private or protected which we will discuss in a sub-section.
Upvotes: -1
Reputation: 1
Class
is a collection of data member and member function.
Class
is a user define data type.
Object
is a class type variable.
Objects are also called instance of the class.
Each object contains all members(variables and functions) declared in the class. We can access any data member or member function from object of that class using .
operator.
Upvotes: 1
Reputation: 317
A class is not an object.
In simpler C language, a class is like a struct
type, but more complex.
Using a C struct
example as analogy:
struct class_ {
int attribute00;
float attribute02;
...
}
struct class_ object_ = {0, 0.0, ...};
struct class_
is act like a class
and object_
is act like an object
.
struct class_
has no physical storage in memory, object_
has physical storage in memory.
In human language, a word 'house' (as class
) can defined in dictionary as place to stay, with doors, with windows, and rooms that you can only speak with your mouth to tell other people what is a house. A physical house (as object
) is a solid build house on a land that you can move in and stay with your family.
A word 'house' take no physical occupation of land or space. A physical house occupy land and space.
Upvotes: 0
Reputation:
No, an object is an instance of a class...
Unless...
If you are implementing a software design tool that allows you to represent classes, interfaces, properties, inheritance, associations, aggregations, etc., then at runtime, yes, each class you place in the designer will be an object instance of the Class class. Ok, couldn't help myself finding an example so twisted and meta.
Now seriously, a class is not an object.
Upvotes: 2
Reputation: 41
Class: A class defines a particular type's behaviours and properties.
Object: An object is an instance of a class.
For example, if you have a Dog named Bingo.
Dog would be the class defining its behaviours and properties
Bingo would be an object that is an instance of the Dog class
Strictly speaking, a Class is not an Object in C++. But in languages such as C# and Java that supports reflection, classes can be used like objects but that is a more advance topic and probaly not what the original question is asking.
Upvotes: 0
Reputation:
Short Answer: In Languages C++, Java: No. In Languages like Python: Yes
Upvotes: -1
Reputation: 126105
Both classes and instances are objects, but object oriented programming doesn't force the language to have classes & instances.
Upvotes: -2
Reputation: 43110
In C++, Objects are essentially the variables and Classes are the types of their values.
Upvotes: -1
Reputation: 49311
C++ supports many paradigms, but not the 'everything is an object' style of object-oriented programming. Classes exist in the source code, but do not exist at run time. Even runtime type information doesn't preserve Classes as object, but only provides a capability to get opaque ids which correspond to the type.
Upvotes: 0
Reputation: 56113
An object is some data, which has an address in run-time memory.
There are different types of object (e.g. int, float, etc.). You can create user-defined types, called 'classes'.
For example, I can define Dog as a class ...
class Dog {};
... and then create several objects, each of which is one instance of that class ...
Dog fido;
Dog spot;
Upvotes: 7
Reputation: 156158
This is analogous to asking if a cat is a "My Kitten Mittens".
Upvotes: -1