Libraries
COS2614 - Programming: Contemporary Concepts · Higher-Level Programming
Libraries
In programming, libraries are collections of pre-written code that developers can use to perform common tasks. They help save time and effort by allowing you to use existing solutions instead of writing code from scratch. Understanding libraries is essential because they are widely used in software development, especially when working with complex applications.
Key idea: After studying this topic, you should be able to:
- Explain what a library is and its purpose in programming.
- Define the concepts of inheritance and polymorphism.
- Implement classes using inheritance in C++.
- Use virtual functions to achieve polymorphism.
What is a Library?
A library is a set of functions and classes that you can use in your programs. Libraries help you avoid rewriting code for common tasks. For example, in C++, the Standard Template Library (STL) provides data structures like lists and maps, which you can use without implementing them yourself.
Understanding Inheritance
Inheritance is a fundamental concept in object-oriented programming (OOP). It allows you to create a new class based on an existing class. The new class, called a derived class, inherits attributes and methods from the base class. This promotes code reuse and helps in organizing code more effectively.
Basic Structure of Classes
Consider the following example of a base class named Student:
class Student {
public:
Student(QString nm, long id, QString m_Major, int year = 1);
~Student() {}
QString getClassName() const;
QString toString() const;
QString yearStr() const;
private:
QString m_Name;
QString m_Major;
long m_StudentId;
protected:
int m_Year;
};This class has attributes like m_Name, m_Major, and m_StudentId. It also has methods to return the class name and a string representation of the object.
Creating a Derived Class
You can create a derived class that inherits from Student. For example, here is how to create an Undergrad class:
class Undergrad: public Student {
public:
Undergrad(QString name, long id, QString major, int year);
QString getClassName() const;
};The Undergrad class inherits all attributes and methods from the Student class. You can add new methods or override existing ones.
Polymorphism
Polymorphism allows you to use a derived class object as if it were a base class object. This is useful when you want to treat different types of objects uniformly.
Using Virtual Functions
To enable polymorphism, you can declare a method as virtual in the base class. This allows derived classes to override the method. For example:
virtual QString getClassName() const;
In this case, both Undergrad and GradStudent can provide their own implementation of getClassName().
Example of Polymorphism
Consider a function that takes a pointer to a Student object:
void graduate(Student* student) {
cout << "The following " << student->getClassName() << " has graduated" << endl;
}This function can accept both Undergrad and GradStudent objects because they both derive from Student.
Implementation Example
Here is a complete example of how to implement the classes and use them:
#include <QTextStream>
#include "student.h"
static QTextStream cout(stdout, QIODevice::WriteOnly);
void graduate(Student* student) {
cout << "The following " << student->getClassName() << " has graduated" << endl;
}
int main() {
Undergrad us("Frodo", 5562, "Ring Theory", 4);
GradStudent gs("Bilbo", 3029, "History", 6, GradStudent::fellowship);
cout << "Here is the data for the two students:" << endl;
cout << gs.toString() << endl;
cout << us.toString() << endl;
cout << "Here is what happens when they graduate:" << endl;
graduate(&us);
graduate(&gs);
return 0;
}This program demonstrates how to create instances of Undergrad and GradStudent, and how to use the graduate function to print their graduation messages.
Watch out: A common mistake is to forget to declare methods as virtual in the base class. This will prevent polymorphism from working correctly.
Summary
- A library is a collection of pre-written code.
- Inheritance allows you to create new classes based on existing ones.
- Polymorphism enables treating derived classes as base class objects.
- Use
virtualfunctions to allow derived classes to override base class methods.
Check your understanding
- What is the purpose of a library in programming?
- Explain the concept of inheritance with an example.
- How does polymorphism work in C++?
- What is the effect of declaring a method as virtual in a base class?