answersLogoWhite

0

What is an object in C plus plus?

Updated: 8/11/2023
User Avatar

Haritha0286

Lvl 1
12y ago

Best Answer

In c++

object is an instance of a class.class is an abstract data type.abstract data type means humen defined data type which is created in order to full fill the requirements of the problem.once we create a class ,we can have as many objects (of that class )as we want.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

An object is an instance of a class. A class is a definition for a complex data type that combines both data and the specific methods that may operate upon that data. A class may encapsulate any combination of the following members:

  • static attributes
  • static methods
  • instance attributes
  • instance methods

Attributes are simply another name for data (variables and constants) while

methods are simply another name for functions or operators. All attributes and methods associated with a class are known as the members of the class.

Static members are members of the class itself. Instance members are members of instances of the class (objects). However, while every object has its own discrete set of instance attributes, there is only one instance of every instance method. An instance method differs from a static method in that a hidden argument is always passed to instance methods; the this pointer.

When calling static methods or accessing static attributes from outwith the class, you will use the scope-resolution operator (::). E.g., class::member.

When calling instance members or accessing instance attributes from outwith the class, you will use the member-of operator (.). E.g., object.member. If using indirection (pointers), you will use the pointer-to-member operator (->). E.g., pointer->member.

All class members have access specifiers associated with them. Access specifiers may be one of private, protected or public for each member. Private access means only the class and friends of the class have access to those members. Protected access is the same as private but also extends access to derivatives of the class. Public access imposes no restrictions.

Access specifiers are placed in the class declaration and apply to all the class members that follow until another access specifier is encountered. Where no access is specified, classes default to private access while structs default to public access (the default remains in force until another specifier is encountered). Aside from this, there is no difference between a class and a C++ struct (either keyword can be used to define a class). Note that a C++ struct differs from a C-struct in that a C-struct has no methods associated with it. By convention, struct classes are simple classes that conform with C-style structs for backward compatibility with C code.

Classes can also designate friend classes, methods or functions. Friends have the same access rights as members of the class itself and are used to extend the class interface. For instance, when two classes are dependent upon each other (such as a parent and child class), it is tempting to expose members that would allow them to interoperate. However, by exposing these members to each other, you expose them to all other code as well, which could undermine the encapsulation of either or both objects. Encapsulation is vital in an object-oriented environment because the less you expose the less unlikely it is that errant code will invalidate the underlying data. In the parent child scenario, it is usually better to make the entire parent class a friend of the child class, and allow specific methods of the child class to be friends with the parent class. Thus the interfaces are extended but are limited specifically each other.

Aside from encapsulation, object-oriented programming (OOP) also supports inheritance, polymorphism and abstraction. Inheritance allows new objects (derivatives) to be designed around existing classes (base classes). That is, the derivative inherits the public and protected interface of the base class which can then be specialised to suit. Without OOP, you would have to copy/paste the code and then modify it to suit, which inevitably leads to code duplication and increased maintenance. Specialisation does not alter the underlying base class, you need only cater for the differences.

Polymorphism is an extension of inheritance in that it is not necessary to know the runtime type of an object in order to invoke specialised behaviour. This is achieved through the use of virtual methods. When calling a virtual method upon an instance of a base class, the most-derived override is called instead, even when the runtime type of the object is not known -- as would be the case when deriving a new object that would typically be unknown to the base class. Polymorphism is enabled through use of the virtual table: every class that declares or inherits one or more virtual methods has its own virtual table that maps the method to the most-derived function pointer associated with the runtime type of the object.

Abstraction relates to pure-virtual methods where the implementation must be provided by a derived object. Abstract data types (those that do not provide or inherit a complete implementation) cannot be instantiated, they serve merely to provide a common interface to all derivatives. For instance, a shape class is an abstraction whereas a square or circle are actual classes. The shape class may provide a draw() method but only the square and circle can actually provide the implementation. By declaring draw() as a pure-virtual method you not only ensure that users cannot instantiate a shape class, but you ensure that all derivatives must implement a draw() method otherwise they, too, become abstract classes. Thus any instance of a shape-derived object is guaranteed to provide its own draw() method (otherwise it wouldn't exist).

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is an object in C plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Is c plus plus 100 percent object oriented?

No; C++ is not 100% object oriented.


Is c plus plus an object oriented language or an object based language?

C++ is object-oriented. It is not object-based because, like C before it, C++ supports the principal of primitive data types, which are not object-based.


What is the different of c and c plus plus?

c is procedure oriented and c++ is object oriented & much newer.


Can you use c in c plus plus without using class and object?

Sure.


What command is used to destroy object in c plus plus?

You use delete object in C++ to delete an object. You can also implicitly delete the object, if it is automatic type, by going out of local scope.

Related questions

What is the role of object in c plus plus?

An object in C++ is an instance of a C++ class.


Which is best C or C plus plus?

depends what you use it for. c++ = object oriented c = not object oriented


Is c plus plus 100 percent object oriented?

No; C++ is not 100% object oriented.


What is object in c plus plus?

An object is simply an instance of a class.


Is c plus plus an object oriented language or an object based language?

C++ is object-oriented. It is not object-based because, like C before it, C++ supports the principal of primitive data types, which are not object-based.


What is the significance of c plus plus?

C++ is an object oriented programming language


What is the different of c and c plus plus?

c is procedure oriented and c++ is object oriented & much newer.


Can you use c in c plus plus without using class and object?

Sure.


What do you call an object function in c plus plus?

method


What is 'this' pointer in c plus plus?

Address of the current object.


Why c plus plus is partially object oriented?

To allow backward compatibility and interoperability with ANSI C, which is entirely non-object-oriented.


What command is used to destroy object in c plus plus?

You use delete object in C++ to delete an object. You can also implicitly delete the object, if it is automatic type, by going out of local scope.