×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

C++ Constructor and Destructor | Find output programs | Set 2

This section contains the C++ find output programs with their explanations on C++ Constructor and Destructor (set 2).
Submitted by Nidhi, on June 12, 2020

Program 1:

#include<iostream>
using namespace std;

class Sample
{
	private:
		int X;
		int Y;
			
	public:
		Sample(int x, int y)
		{
			X = x;
			Y = y;
		}
	
		void set(int x, int y)
		{
			X = x;
			Y = y;
		}
		
		void print()
		{
			cout<<X<<" "<<Y<<endl;
		}
		
};
		
int main()
{		
	Sample S[2];
	Sample *PTR;
	
	PTR = S;

	PTR[0].set(10,20);
	PTR[1].set(30,40);
	
	PTR[0].print();
	PTR[1].print();
		
	return 0;
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:32:12: error: no matching function for call to ‘Sample::Sample()’
  Sample S[2];

Explanation:

The above code will generate a syntax error because in the main() function we created an array of objects that must be instantiated by the default constructor, but in the class, the default constructor is not defined.

Program 2:

#include <iostream>
using namespace std;

class Sample {
private:
    int X;
    int Y;

public:
    Sample(int x, int y)
    {
        X = x;
        Y = y;
    }

    void set(int x, int y)
    {
        X = x;
        Y = y;
    }

    void print()
    {
        cout << X << " " << Y << endl;
    }
};

int main()
{
    Sample S[2] = { Sample(0, 0), Sample(0, 0) };
    Sample* PTR;

    PTR = S;

    PTR[0].set(10, 20);
    PTR[1].set(30, 40);

    PTR[0].print();
    PTR[1].print();

    return 0;
}

Output:

10 20
30 40

Explanation:

In the above program, we created a class Sample that contains two data members X and Y, one parameterized constructor, set() and print() member functions. 

Now coming to the main() function, we created an array of objects and that will instantiated by the parameterized constructor. And created a pointer that is being initialized with the base address of array objects. And then called set() and print() functions using the pointer.

Program 3:

#include <iostream>
using namespace std;

class Sample {
private:
    int X;

public:
    Sample() const
    {
    }

    void set(int x)
    {
        X = x;
    }
    void print()
    {
        cout << X << endl;
    }
};

int main()
{
    Sample S;

    S.set(10);
    S.print();

    return 0;
}

Output:

main.cpp:9:14: error: constructors may not be cv-qualified
     Sample() const
              ^~~~~

Explanation:

The above program will generate an error because we cannot create a constructor as a const member function in C++.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.