answersLogoWhite

0


Best Answer

The question is unclear, but classes can only have one destructor at most. They can have as many constructors as required. Even if you do not declare any constructors, the compiler will automatically generate a default constructor (which initialises all member variables to zero) and a copy constructor (which performs a member-wise, shallow copy of the members). If your class contains member pointers and allocates memory to them, you must provide your own destructor to clean up those memory allocations as well as provide a copy constructor to deep copy the memory allocations (thus ensuring no two instances of the class point to the same memory).

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you construct a program with destructor more than constructors?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Can you overload destructor for your class?

No. Classes can only have one destructor, whether you define one yourself or allow the compiler to generate one for you. The compiler-generated destructor is public by default, does not release any memory allocated to any class' member pointers, and is non-virtual, which are the three main reasons for defining your own.


How do you get more rockets on kerbal space program?

Construct them in the VAB (Vehicle Assembly Building)


When you define clean up destructor how does it affect garbage collector?

If you define clean up in destructor garbage collector will take more time to clean up the objects and more and more objects are created in Gen 2..


Can you use two while loops in a program?

You can use zero or more while-loops, there is no limit.


What are virtual constructors or destructors?

If an object (with a non-virtual destructor) is destroyed explicitly by applying the delete operator to a base-class pointer to the object, the base-class destructor function (matching the pointer type) is called on the object. There is a simple solution to this problem


How many constructors can c have?

A class can have any number of constructors, as far as they are having different parameters or different number of parameters. For example, a class A can have following constructors & even more: A() -the default constructor A(A objectA) -the copy constructor A(int p) A(int p1, int p2) A(int[] p1, float p2) A(double p1, double p2, int p3) A(A objA, int[] p) A(B objB)


How do you write a C plus plus program for destructor?

Class destructors define operations that will be performed whenever an object of the class falls from scope. This usually involves manually releasing any resources allocated to the object. However, by using resource handles or smart pointers, all resources will be released automatically, thus there is no need to define a destructor. The only time we really need to define a destructor is when the class is intended to be used as a polymorphic base class (has one or more virtual methods) but does not inherit a virtual destructor, in which case we must define a virtual destructor. Classes that do define or inherit a virtual destructor cannot be used polymorphically. However, by using resource handles or smart pointers to manage resources, the destructor body can be left empty. The only reason for declaring the destructor at all is simply to declare it virtual because all methods are non-virtual by default -- unless they override a virtual method of the base class (in which case they can simply be declared as overrides). The only time we need to define a non-empty destructor body is when implementing a resource handle or smart pointers, however the standard library already provides efficient implementations so, other than for educational purposes, there is no need to define our own resource handles.


What are parameterized constructors with example in RDBMS?

Parameterized constructors (or more simply "constructors") allow you to create a new instance of a class while simultaneously passing arguments to the new instance. Constructors are essential for object oriented programming since they allow user-defined construction code to be passed parameters by the creator of the instance. They simplify client code by allowing a new object instance to be created and initialized in a single expression.


What is the use of Constructor with Dynamic allocation?

Constructors are necessary to initialize classes. It allows to avoid to a lot of problems with unauthorized access of memory. Dynamic allocation makes possible allocation of memory during execution of program. If you do not use dynamic allocation, all required memory will be allocated during initialization phase (constructors are usually responsible for that). But you can't use more memory. Dynamic allocation was designed to overcome such problems.


What is a good example of personal construct theory?

A good example of personal construct theory is when individuals use their own subjective mental frameworks to categorize and make sense of the world around them. These constructs influence how a person perceives and interprets different situations and experiences. For instance, a person who views themselves as adventurous may interpret traveling to a new country as exciting and enriching, while someone who sees themselves as cautious may view the same experience as risky and stressful.


What are the advantages of constructor and destructor?

Without a copy constructor the only way to copy an object would be to instantiate a new object (or use an existing object) and then assign another object's value to it. However, it would be very odd indeed to have a copy assignment operator without a matching copy constructor. If you have one, you must have both. If you do not need to copy an object of a particular class, however, you can simply delete both the copy constructor and the copy assigment operator for that class. Any attempt to copy or copy assign would then result in a compile-time error.


What is overloading constructor in java?

This is when you overload the constructor so that there isn't just one way you can initialize the class objectAssume you have a class called Foo and you want two different constructorsFoo(int a, int b);Foo(float a, float b);using a method like this can make your classes more dynamic and you don't need to to write a new class to handle different types. Sorry for the lack of Java code, i program in C++ but it you can overload functions and constructors in C++ too.