Q:

C++ Dynamic Memory Allocation | Find output programs | Set 3

belongs to collection: C++ find output programs

0

This section contains the C++ find output programs with their explanations on C++ Dynamic Memory Allocation (set 3).

Program 1:

#include<iostream>
#include<stdlib.h>
using namespace std;

typedef class MyClass
{
	int A;
	public:
		MyClass()
		{
			A=10;
		}
		void Print()
		{
			cout<<A<<endl;
		}
}CLS;

int main()
{
	CLS *C;
	
	C = new CLS;
	
	C->Print();
	
	return 0;
}

Program 2:

#include<iostream>
#include<stdlib.h>
using namespace std;

typedef class MyClass
{
	int A;
	public:
		MyClass()
		{
			A=10;
		}
		void Print()
		{
			cout<<A<<endl;
		}
}CLS;

int main()
{
	CLS *C;
	
	C = new CLS(this);
	
	C->Print();
	
	return 0;
}

Program 3:

#include<iostream>
#include<stdlib.h>
using namespace std;

typedef class MyClass
{
	int A;
	public:
		MyClass()
		{
			A=10;
		}
		void Print()
		{
			cout<<A<<endl;
		}
}CLS;

int main()
{
	MyClass *C;
	
	C = (MyClass *)malloc(sizeof(MyClass));
	
	C->Print();
	
	free(C);
	
	return 0;
}

Program 4:

#include<iostream>
#include<stdlib.h>
using namespace std;

typedef class MyClass
{
	int A;
	public:
		MyClass()
		{
			A=10;
		}
		void Print()
		{
			cout<<A<<endl;
		}
}CLS;

int main()
{
	MyClass *C;
	
	C = new CLS();
	
	C->Print();
	
	free(C);
	
	return 0;
}

Program 5:

#include<iostream>
#include<stdlib.h>
using namespace std;

typedef class MyClass
{
	int A=5;
	public:
		CLS()
		{
			A=10;
			cout<<"Constructor called ";
		}
		void Print()
		{
			cout<<A<<endl;
		}
}CLS;

int main()
{
	MyClass *C;
	
	C = new CLS();
	
	C->Print();
	
	free(C);
	
	return 0;
}

All Answers

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

Answer Program  1:

Output:

10

Explanation:

It will print "10" on the console screen. Let's understand the program.

Here, we create a class using a typedef with data member A, default constructor, and Print() function.

Now, look to the main() function, here create a pointer of CLS class and allocate dynamically. Then the default constructor will call to initialize the value of A and then we called Print() function to print the value of A.

Answer Program  2:

Output:

main.cpp: In function ‘int main()’:
main.cpp:23:14: error: invalid use of ‘this’ in non-member function
  C = new CLS(this);
              ^~~~

Explanation:

It will generate an error because we cannot use "this" pointer outside the class.

Answer Program 3:

Output:

Garbage Value                              ^

Explanation:

It will print garbage value. Because allocate space for pointer C using malloc() function, it will not call the constructor of the class, then A is not initialized then Print() function will print it's a garbage value.

Answer Program 4:

Output:

10

Explanation:

It will print 10 on the console screen. Let's understand the program.

Here, we create a class using a typedef with data member A, default constructor, and Print() function.

Now, look to the main() function, here create a pointer of MyClass class and allocate space using CLS with the new operator, here CLS is the alias of MyClass so that we can use anyone. And, then we print the value of A using Print() function and free the pointer.

Answer Program 5:

Output:

main.cpp:9:7: error: ISO C++ forbids declaration of ‘CLS’ with no type [-fpermissive]
   CLS()
       ^

Explanation:

It will print 5 on the console screen. Let's understand the program.

Here, we create a class using typedef with data member A and initialized with 5, default constructor using alias name CLS, and Print() function.

Here, we created constructor using typedef name CLS, but when we allocate memory space using new operator, it will not call the constructor. Then Print() function will print 5 on the console screen.

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++ Dynamic Memory Allocation | Find output progra...