C++ Programming :: Functions - Discussion
3 / 14
#include <iostream>
using namespace std;
class base {
int b;
public:
base (int x) {
b = x;
cout << "Base";
};
};
class derived : public base
{
double d;
public:
derived(double x, int y);
d = x;
cout << " Derived";
};
int main () {
derived d(23.5, 10);
}
#include <iostream>
using namespace std;
class base {
int b;
public:
base (int x) {
b = x;
cout << "Base";
};
};
class derived : public base
{
double d;
public:
derived(double x, int y);
d = x;
cout << " Derived";
};
int main () {
derived d(23.5, 10);
}
ADerived
BBase
CBase Derived
DCompilation error: No appropriate base class constructor available
Show Explanation
#include <iostream>
using namespace std;
class base {
int b;
public:
base (int x) {
b = x;
cout << "Base";
};
};
class derived : public base
{
double d;
public:
derived(double x, int y);
d = x;
cout << " Derived";
};
int main () {
derived d(23.5, 10);
}
Answer: Compiler throws "error: ‘d’ does not name a type" and "error: ‘cout’ does not name a type" errors.
This is because statements in c++ should be inside a function but d = x; and cout << " Derived"; are defined directly inside a class.
Asked In ::
Correct program:
#include <iostream>
using namespace std;
class base {
int b;
public:
base (int x) {
b = x;
cout << "Base";
};
};
class derived : public base
{
double d;
public:
derived(double x, int y);
d = x;
cout << " Derived";
};
int main () {
derived d(23.5, 10);
}
Answer: Compiler throws "error: ‘d’ does not name a type" and "error: ‘cout’ does not name a type" errors.
This is because statements in c++ should be inside a function but d = x; and cout << " Derived"; are defined directly inside a class.
Read Full Answer
Report Error
Please Login First Click Here