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

This section contains the C++ find output programs with their explanations on C++ Dynamic Memory Allocation (set 3).
Submitted by Nidhi, on October 11, 2020

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

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.

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

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.

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

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.

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

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.

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

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.





Comments and Discussions!

Load comments ↻





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