Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
227 views

Abstract Class - Exp

The document discusses abstract classes in C++. An abstract class is a class that contains at least one pure virtual function. A pure virtual function is defined by using = 0 in the function declaration. The key aspects are: 1. An abstract class cannot be instantiated, and can only be used as a base class for other classes. 2. Derived classes of an abstract class must implement all pure virtual functions, otherwise they will also be abstract classes. 3. Pointers to abstract classes can be used to point to objects of derived classes polymorphically. Calling a pure virtual function through such a pointer will call the implementation in the derived class.

Uploaded by

Ipsha Guha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
227 views

Abstract Class - Exp

The document discusses abstract classes in C++. An abstract class is a class that contains at least one pure virtual function. A pure virtual function is defined by using = 0 in the function declaration. The key aspects are: 1. An abstract class cannot be instantiated, and can only be used as a base class for other classes. 2. Derived classes of an abstract class must implement all pure virtual functions, otherwise they will also be abstract classes. 3. Pointers to abstract classes can be used to point to objects of derived classes polymorphically. Calling a pure virtual function through such a pointer will call the implementation in the derived class.

Uploaded by

Ipsha Guha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Abstract Classes

1. What will be the output of the following C++ code?


1. #include <iostream>

2. using namespace std;

3. class p

4. {

5. protected:

6. int width, height;

7. public:

8. void set_values (int a, int b)

9. {

10. width = a; height = b;

11. }

12. virtual int area (void) = 0;

13. };

14. class r: public p

15. {

16. public:

17. int area (void)

18. {

19. return (width * height);

20. }

21. };

22. class t: public p

23. {

24. public:
25. int area (void)

26. {

27. return (width * height / 2);

28. }

29. };

30. int main ()

31. {

32. r rect;

33. t trgl;

34. p * ppoly1 = &rect;

35. p * ppoly2 = &trgl;

36. ppoly1->set_values (4, 5);

37. ppoly2->set_values (4, 5);

38. cout << ppoly1 -> area() ;

39. cout << ppoly2 -> area();

40. return 0;

41. }

a) 1020
b) 20
c) 10
d) 2010
2. What will be the output of the following C++ code?
1. #include <iostream>

2. using namespace std;

3. class MyInterface

4. {

5. public:

6. virtual void Display() = 0;

7. };

8. class Class1 : public MyInterface

9. {

10. public:

11. void Display()

12. {

13. int a = 5;

14. cout << a;

15. }

16. };

17. class Class2 : public MyInterface

18. {

19. public:

20. void Display()

21. {

22. cout <<" 5" << endl;

23. }

24. };

25. int main()


26. {

27. Class1 obj1;

28. obj1.Display();

29. Class2 obj2;

30. obj2.Display();

31. return 0;

32. }

a) 5
b) 10
c) 5 5
d) 15

3. What will be the output of the following C++ code?


1. #include <iostream>

2. using namespace std;

3. class sample

4. {

5. public:

6. virtual void example() = 0;

7. };

8. class Ex1:public sample

9. {

10. public:

11. void example()

12. {

13. cout << "ubuntu";

14. }
15. };

16. class Ex2:public sample

17. {

18. public:

19. void example()

20. {

21. cout << " is awesome";

22. }

23. };

24. int main()

25. {

26. sample* arra[2];

27. Ex1 e1;

28. Ex2 e2;

29. arra[0]=&e1;

30. arra[1]=&e2;

31. arra[0]->example();

32. arra[1]->example();

33. }

a) ubuntu
b) is awesome
c) ubuntu is awesome
d) ubunt esome
4. What will be the output of the following C++ code?
1. #include <iostream>

2. using namespace std;

3. class Base

4. {

5. public:

6. virtual void print() const = 0;

7. };

8. class DerivedOne : virtual public Base

9. {

10. public:

11. void print() const

12. {

13. cout << "1";

14. }

15. };

16. class DerivedTwo : virtual public Base

17. {

18. public:

19. void print() const

20. {

21. cout << "2";

22. }

23. };

24. class Multiple : public DerivedOne, DerivedTwo

25. {
26. public:

27. void print() const

28. {

29. DerivedTwo::print();

30. }

31. };

32. int main()

33. {

34. Multiple both;

35. DerivedOne one;

36. DerivedTwo two;

37. Base *array[ 3 ];

38. array[ 0 ] = &both;

39. array[ 1 ] = &one;

40. array[ 2 ] = &two;

41. for ( int i = 0; i < 3; i++ )

42. array[ i ] -> print();

43. return 0;

44. }

a) 121
b) 212
c) 12
d) 215
5. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A
{
int a;
public:
virtual void func() = 0;
};

class B: public A
{
public:
void func(){
cout<<"Class B"<<endl;
}
};

int main(int argc, char const *argv[])


{
B b;
b.func();
return 0;
}

a) Class B
b) Error
c) Segmentation fault
d) No output

6. #include <iostream>
#include <string>
using namespace std;
class A
{
int a;
public:
virtual void func() = 0;
};

class B: public A
{
public:
void func(){
cout<<"Class B"<<endl;
}
};
int main(int argc, char const *argv[])
{
A a;
a.func();
return 0;
}

a) Class B
b) Error
c) Segmentation fault
d) No output

7. #include <iostream>
#include <string>
using namespace std;
class A
{
int a;
public:
virtual void func() = 0;
};

class B: public A
{
public:
void func(){
cout<<"Class B"<<endl;
}
};

int main(int argc, char const *argv[])


{
A *a;
a->func();
return 0;
}

a) Class B
b) Error
c) Segmentation fault
d) No output

You might also like