Q:

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

belongs to collection: C++ find output programs

0

This section contains the C++ find output programs with their explanations on C++ Constructor and Destructor (set 2).

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;
}

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;
}

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;
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Program1:

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.

Answer Program 2:

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.

Answer Program 3:

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++.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ Constructor and Destructor | Find output progr... >>
<< C++ Constructor and Destructor | Find output progr...