Predict the C++ program output and enhance C++ Skill. To refresh c++ skill and concept, some very simple c++ program’s output prediction is best way to learn. Try to predict the outputs and exercise by compiling and running the below sample c++ program.

what is output of below c++ program?

#include <iostream>
#include<stdlib.h>

using namespace std;

class A {
public:
	A() {
		this->Foo();
		exit(0);
	}
	virtual void Foo() {
		cout << 1 << endl;
	}
};

class B: public A {
public:
	B() {
		this->Foo();
	}
	virtual void Foo() {
		cout << 2 << endl;
	}
};

int main(int, char**) {
	B *obj = new B();
	return 0;
}

output : 1

Predict the C++ program output. will the below code compile? if not then why?

#include <iostream>
using namespace std;

void foo(float& a)
{
	cout << a << endl;
}

int main() {
	int b = 2;
	foo(b);
	return 0;
}

what will be the output of below code?

#include <iostream>

int main()
{
   char c = 10;
   const int& y = c;
   c = 20;
   std::cout << y << std::endl;
   return 0;
}

explanation:
int& x = c;

Here an implicit conversion from char to int is being performed by the compiler and for that a temporary int variable would be created. The resulting temporary int can only be bound to a const reference. So The output would be 10, The value of y will not changed when the value of c is changed.

Predict the C++ program output?

#include <iostream>
using namespace std;
class Base {
public:
    int b;
    Base() {
        b = 0;
    }
};
class Der : public Base {
public:
    int d;
    Der() {
        d = 1;
    }
};
void func(Base* ptr, int no_obj) {
    for (int i = 0; i < no_obj; i++, ptr++) {
        cout << (*ptr).b << endl;;
    }
}
int main() {
    Der obj[4];
    func(obj, 4);
    return 0;
}

output:

0
1
0
1

Find output of c++ program?

#include <iostream>
using namespace std;
class Base {
public:
    void print() { cout << "hello\n"; }
};
class Der : public Base {
public:
    void print() { cout << "world\n";}
};

int main() {
        Base *b = new Der();
        b->print();
    return 0;
}

Find output of c++ program?

#include <iostream>
using namespace std;

class A {
public:
    A() {}
    ~A() {
        cout << 1;
    }
};

int main(int argc, const char * argv[]) {
    try {
        A a;
        throw 32;
    } catch(int a) {
        std::cout << a;
    }
}

output:

132

what would be the output?

#include <iostream>
using namespace std;
class Base {
public:
    void virtual print() { cout << "hello\n"; }
};
class Der : public Base {
public:
    void print() { cout << "world\n";}
};

int main() {
        Base *b = new Der();
        b->print();
    return 0;
}

predict c++ output of below?

#include <iostream>
using namespace std;
class Base {
public:
    void virtual print() { cout << "hello\n"; }
};
class Der : public Base {
public:
    void virtual print() { cout << "world\n";}
};

int main() {
        Base *b = new Der();
        b->print();
    return 0;
}
why the below code will not compile?

#include <iostream>
using namespace std;

class A {
  virtual void foo() = 0;
};

class Base : public A{
public:
    void virtual print() { cout << "hello\n"; }
};
class Der : public Base {
public:
    void virtual print() { cout << "world\n";}
};

int main() {
        Base *b = new Der();
    return 0;
}

Explanation:

In C++ a class with at least one pure virtual function is called abstract class. You can not create objects of that class, but may only have pointers or references to it.If you are deriving from an abstract class, then make sure you override and define all pure virtual functions for your class.

what is problem with below code?

#include <iostream>
using namespace std;

int& a = 5;

int main() {
     return 0;
}

problem :
invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’

will the below code compile using -std=c++11?

#include <iostream>
using namespace std;

int&& a = 5;

int main() {
     int&& b = a;
     return 0;
}

Note: No , c++ 11 rvalue references binds only to rvalues



Related Contents to follow