29 Lecture

CS304

Midterm & Final Term Short Notes

ABSTRACT CLASSES

Abstract classes are classes that cannot be instantiated and are designed to be used as a base class for other classes. Abstract classes are used to provide a common interface for a set of derived classes, while allowing each derived class to ha


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. Which keyword is used to define an abstract class in C++? a) virtual b) abstract c) interface d) class Answer: b) abstract Which of the following is true about abstract classes? a) They can be instantiated b) They cannot be inherited c) They provide a common interface for derived classes d) They do not allow any data members Answer: c) They provide a common interface for derived classes Which of the following is true about pure virtual functions? a) They have an implementation in the base class b) They can be called from the base class c) They must be overridden in the derived class d) They cannot be overridden in the derived class Answer: c) They must be overridden in the derived class Can an abstract class have concrete (non-virtual) functions? a) Yes b) No Answer: a) Yes Which of the following is a correct syntax for declaring a pure virtual function? a) virtual void func() const = 0; b) pure virtual void func() = 0; c) void virtual func() = 0; d) void func() const = 0; Answer: a) virtual void func() const = 0; Which of the following is a correct way to create an instance of an abstract class? a) Shape s; b) Shape* s = new Shape(); c) Circle c; d) None of the above Answer: d) None of the above Which of the following is true about abstract classes and interfaces? a) They are the same thing b) Interfaces cannot have data members c) Abstract classes cannot have pure virtual functions d) None of the above Answer: b) Interfaces cannot have data members Which of the following is an advantage of using abstract classes? a) They allow multiple inheritance b) They allow for runtime polymorphism c) They provide a mechanism for code reuse d) They can be instantiated Answer: c) They provide a mechanism for code reuse Which of the following is not an example of an abstract class? a) Shape b) Animal c) Car d) Vehicle Answer: c) Car Which of the following statements is true about abstract classes? a) All member functions must be pure virtual functions b) Abstract classes cannot have constructors c) Abstract classes cannot have concrete functions d) Abstract classes cannot have data members Answer: c) Abstract classes cannot have concrete functions


Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is an abstract class? An abstract class is a class that cannot be instantiated and is used as a base class for creating derived classes. It contains one or more abstract methods, which have no implementation in the base class but are implemented in the derived classes. How is an abstract class different from a concrete class? An abstract class cannot be instantiated, whereas a concrete class can be instantiated. An abstract class may contain one or more abstract methods, whereas a concrete class does not contain any abstract methods. An abstract class may contain both abstract and non-abstract methods, whereas a concrete class contains only non-abstract methods. Can an abstract class have a constructor? Yes, an abstract class can have a constructor. However, it cannot be used to instantiate an object of the abstract class. The constructor is used to initialize the members of the class when a derived class object is created. How do you declare an abstract method in a class? To declare an abstract method in a class, you need to use the abstract keyword before the method declaration. For example: abstract void methodName(); Can a concrete class inherit from an abstract class? Yes, a concrete class can inherit from an abstract class. However, the concrete class must implement all the abstract methods of the abstract class. Can an abstract class implement an interface? Yes, an abstract class can implement an interface. In this case, the abstract class must provide implementations for all the methods in the interface. What is the purpose of an abstract class? The purpose of an abstract class is to provide a common base for a set of related classes. It defines the common behavior and properties of the derived classes and provides a framework for implementing the behavior. Can an abstract class have a final method? Yes, an abstract class can have a final method. However, the method cannot be abstract. Can an abstract class have a static method? Yes, an abstract class can have a static method. However, the method cannot be abstract. Can an abstract class be marked as final? No, an abstract class cannot be marked as final because it is meant to be inherited by other classes.
Abstract classes are classes that are declared but cannot be instantiated. The purpose of an abstract class is to provide a common base for a set of derived classes. An abstract class cannot be instantiated because it is not complete; it requires one or more methods to be declared abstract. Abstract classes are used as a way to enforce certain guidelines or behaviors for the derived classes. Here are some key points related to abstract classes:
  1. Abstract classes can have abstract methods which are declared in the class but not defined.
  2. An abstract class can have both abstract and non-abstract methods.
  3. A class that extends an abstract class must implement all the abstract methods declared in the abstract class.
  4. An abstract class cannot be instantiated directly, but can be instantiated through its concrete subclass.
  5. Abstract classes can have constructors and data members like a normal class.
  6. Abstract classes provide a way to enforce encapsulation and modularity in the design of an application.
  7. Abstract classes are useful when designing frameworks and libraries where certain functionality is common among multiple classes.
Overall, abstract classes provide a powerful tool for designing object-oriented software. They can help enforce encapsulation, modularity, and provide a common base for a set of derived classes. Understanding how to use and implement abstract classes is an important skill for any object-oriented programmer.