Identify the error in the following program.
#include <iostream>; using namespace std; class Info { char* name; int Number; public: void getInfo() { cout << "Info::getInfo"; getName(); } void getName() { cout << "Info::getName"; } }; class Name: public Info { char *name; public: void getName() { cout << "Name::getName"; } }; void main() { Info *P; Name n; P = n; p->getInfo(); } /*
Here P=n will replace with P=&n in the main() function. Because P is a pointer.
Identify the error in the following program.
#include <iostream>; using namespace std; class Person { int age; public: Person() { } Person(int age) { this.age = age; } Person& operator < (Person &p) { return age < p.age? p: *this; } int getAge() { return age; } }; void main() { Person P1 (15); Person P2 (11); Person P3; //if P1 is less than P2 P3 = P1 < P2; P1.lessthan(P2) cout << P3.getAge(); } /*
The function
person (int age) { this.age = age; }
should write like as…
person (int age) { this > age = age; }
Identify the error in the following program.
#include <iostream>; using namespace std; class Human { public: Human() { } virtual -Human() { cout << "Human::-Human"; } }; class Student: public Human { public: Student() { } -Student() { cout << "Student::-Student()"; } }; void main() { Human *H = new Student(); delete H; }
Here we cannot write Human *H = new student(); in the main() function because base class’s member includes in derived class’s object so we should write this as follow
student *H = new Student();
Correct the errors in the following program.
class Human { private: int m; public: void getdata() { cout << " Enter number:"; cin >> m; } }; main() { test T; T->getdata(); T->display(); test *p; p = new test; p.getdata(); (*p).display(); }
Here T->getdata replace with T.getdata and T->display replace with T.display in the main() function. Because in this program T is object to pointer.
Debug and run the following program. What will be the output?
#include<iostream> using namespace std; class A { protected: int a,b; public: A(int x = 0, int y) { a = x; b = y; } virtual void print (); }; class B: public A { private: float p,q; public: B(intm, int n, float u, float v) { p = u; q = v; } B() {p = p = 0;} void input(float u, float v); virtual void print(loat); }; void A::print(void) { cout << A values: << a <<""<< b << "\n"; } void B:print(float) { cout << B values: << u <<""<< v << "\n"; } void B::input(float x, float y) { p = x; q = y; } main() { A a1(10,20), *ptr; B b1; b1.input(7.5,3.142); ptr = &a1; ptr->print(); ptr = &b1; ptr->print(); }
The virtual keyword must write in base class and prototype base class and derived base class of print function should same.
Next Previous