answersLogoWhite

0

A class is the definition of an user-defined type while an object is an instance of a class.

class foo {}; // definition of a type (implementation omitted for brevity).

struct bar {}; // another definition of a type

foo x; // x is an instance of the foo class, therefore x is an object

bar y; // y is an instance of the bar class, therefore y is an object

Note that in C++, struct and class are exactly the same (they are both classes), the only difference being that members of a class are private by default while members of a struct are public by default. Thus the following definitions are exactly the same:

struct X {

Y(){} // public by default

};

class Y {

public:

X(){} // public by specification

};

As are the following:

struct A {

private:

int m_data; // private by specification

};

class B {

int m_data; // private by default

};

User Avatar

Wiki User

11y ago

Still curious? Ask our experts.

Chat with our AI personalities

MaxineMaxine
I respect you enough to keep it real.
Chat with Maxine
BeauBeau
You're doing better than you think!
Chat with Beau
JordanJordan
Looking for a career mentor? I've seen my fair share of shake-ups.
Chat with Jordan
More answers

Because if we didn't need classes or objects then we'd use C instead. Note that an object is simply an instance of a class. Classifying our data objects makes it much easier to develop highly complex data structures than with C alone, because each object is a self-contained capsule that is entirely responsible for its own data. Encapsulating data and the methods that operate upon that data is one of the four fundamentals of object-oriented programming. The other three are abstraction, inheritance and polymorphism. C alone has none of these fundamentals, thus programming in C is much more complex than in C++. By the same token, C++ is every bit as efficient as C, but is many times easier to work with.

User Avatar

Wiki User

11y ago
User Avatar

Add your answer:

Earn +20 pts
Q: What are classes and objects in Cpp?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Can you use objects in class level declaration?

Objects are classes... It's the most abstract type of data.


What is simple inheritance?

In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, or to establish a subtype from an existing object, or both, depending upon programming language support.In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes, superclasses, parent classes or ancestor classes. The resulting classes are known as derived classes, subclasses or child classes.The relationships of classes through inheritance gives rise to a hierarchy. In prototype-based programming, objects can be defined directly from other objects without the need to define any classes, in which case this feature is called differential inheritance.The inheritance concept was invented in 1968 for Simula.


CPP Program for Implementing Knuth-morris-pratt pattern matching algorithm?

what is the pure algorithm instead of cpp program?


Can you explain in which scenario the primitive types are used as objects?

It is sometimes easier to deal with primitives as objects. Moreover most of the collection classes store objects and not primitive data types. And also the wrapper classes provide many utility methods also. Because of these resons we need wrapper classes. And since we create instances of these classes we can store them in any of the collection classes and pass them around as a collection. Also we can pass them around as method parameters where a method expects an objectIt is sometimes easier to deal with primitives as objects. Moreover most of the collection classes store objects and not primitive data types. And also the wrapper classes provide many utility methods also. Because of these resons we need wrapper classes. And since we create instances of these classes we can store them in any of the collection classes and pass them around as a collection. Also we can pass them around as method parameters where a method expects an object


What is c plus plus called object oriented programming language?

C++ is an extension of C; object-oriented capabilities - basically, the possibility of defining classes and of creating objects based on those classes - have been added to the original C.C++ is an extension of C; object-oriented capabilities - basically, the possibility of defining classes and of creating objects based on those classes - have been added to the original C.C++ is an extension of C; object-oriented capabilities - basically, the possibility of defining classes and of creating objects based on those classes - have been added to the original C.C++ is an extension of C; object-oriented capabilities - basically, the possibility of defining classes and of creating objects based on those classes - have been added to the original C.