How unit 5 is examined
This unit covers the basic OOP concepts and a first C++ program; the marks sit in inheritance, polymorphism, applications of C++, class versus object, and POP versus OOP.
Basic concepts of OOP: object
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>An object is a run-time instance of a class that bundles data (attributes) and functions (behaviour) and has its own identity and state.</mark>
Key points.
- An object models a real-world entity such as a student, a bank account or a car.
- It has state (its data members), behaviour (its member functions) and identity (its own memory address).
- Objects communicate by calling each other's member functions.
Class
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Low weight</span>
Definition. <mark>A class is a user-defined data type that acts as a blueprint for objects, grouping data members and member functions under one name.</mark>
Key points.
- A class is only a description, so it occupies no memory until an object is created from it.
- Members are private by default, and the
public,privateandprotectedlabels control access. - One class can produce any number of objects, each with its own copy of the data members.
| Basis | Class | Object |
|---|---|---|
| Meaning | Blueprint or template | Instance of the class |
| Memory | None until objects exist | Allocated when created |
| Existence | Logical | Physical, at run time |
| Declaration | class Car { ... }; |
Car c1, c2; |
| Count | Defined once | Many per class |
Example. class Car { public: int speed; void run(); }; then Car c1; creates the object c1.
Asked: [7 marks] (Dec 2020) How class is different from objects explain?
Data abstraction
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>Data abstraction means showing only the essential features to the user and hiding the background implementation details.</mark>
Key points.
- The user of a class needs to know what an operation does, not how it does it.
- In C++ abstraction is achieved with classes, where public functions form the interface and private members hide the details.
- It reduces complexity and lets the implementation change without affecting the user's code.
Data encapsulation
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>Encapsulation is the wrapping up of data and the functions that operate on it into a single unit called a class.</mark>
Key points.
- Data members are normally kept private, so they can be reached only through the class's own member functions.
- This gives data hiding and protects data from accidental outside modification.
- Abstraction hides complexity at the design level, while encapsulation hides data at the implementation level.
Inheritance
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Medium weight</span>
Definition. <mark>Inheritance is the process by which a derived class acquires the properties and behaviour of a base class, which gives code reusability.</mark>
Diagram.
<figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u5-01" viewBox="0 0 596 166" width="596" height="166" role="img" aria-label="Arrows point from derived to base. Left, single (B1, D1); centre, hierarchical (H0 base with H1 and H2 derived); right, multiple (M1 and M2 bases, M3 derived)"><style>#dsfig-u5-01 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u5-01 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u5-01 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u5-01 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u5-01 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u5-01 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u5-01 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u5-01 .t{fill:#16181D;font-weight:500}#dsfig-u5-01 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u5-01 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u5-01 .dot{fill:#16181D}#dsfig-u5-01 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u5-01 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u5-01 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u5-01 .ah{fill:#454C5A}#dsfig-u5-01 .ah.hi{fill:#2340B8}#dsfig-u5-01 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u5-01 .wl .t{font-size:12px;font-weight:700}#dsfig-u5-01 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u5-01 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u5-01 .e{stroke:#B1B7C3}html.dark #dsfig-u5-01 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u5-01 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u5-01 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u5-01 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u5-01 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u5-01 .t{fill:#E6E8ED}html.dark #dsfig-u5-01 .t.inv{fill:#0F1115}html.dark #dsfig-u5-01 .kd{stroke:#E6E8ED}html.dark #dsfig-u5-01 .dot{fill:#E6E8ED}html.dark #dsfig-u5-01 .ann{fill:#8FA3FF}html.dark #dsfig-u5-01 .lbl{fill:#858D9C}html.dark #dsfig-u5-01 .ptr{fill:#8FA3FF}html.dark #dsfig-u5-01 .ah{fill:#B1B7C3}html.dark #dsfig-u5-01 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u5-01 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u5-01 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u5-01 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah2" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah" d="M0,1 L9,5 L0,9 z"/></marker><marker id="ahh2" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah hi" d="M0,1 L9,5 L0,9 z"/></marker></defs><path class="e" d="M40,107 L40,61" marker-end="url(#ah2)"/><path class="e" d="M169,107 L169,61" marker-end="url(#ah2)"/><path class="e" d="M263.5,109 L288.6,58.8" marker-end="url(#ah2)"/><path class="e" d="M332.5,109 L307.4,58.8" marker-end="url(#ah2)"/><path class="e" d="M504.5,109 L479.4,58.8" marker-end="url(#ah2)"/><path class="e" d="M521.5,109 L546.6,58.8" marker-end="url(#ah2)"/><circle class="n" cx="40" cy="126" r="18"/><text class="t" x="40" y="126" dy=".35em" text-anchor="middle">D1</text><circle class="n" cx="40" cy="40" r="18"/><text class="t" x="40" y="40" dy=".35em" text-anchor="middle">B1</text><circle class="n" cx="169" cy="40" r="18"/><text class="t" x="169" y="40" dy=".35em" text-anchor="middle">B2</text><circle class="n" cx="169" cy="126" r="18"/><text class="t" x="169" y="126" dy=".35em" text-anchor="middle">D2</text><circle class="n" cx="298" cy="40" r="18"/><text class="t" x="298" y="40" dy=".35em" text-anchor="middle">H0</text><circle class="n" cx="255" cy="126" r="18"/><text class="t" x="255" y="126" dy=".35em" text-anchor="middle">H1</text><circle class="n" cx="341" cy="126" r="18"/><text class="t" x="341" y="126" dy=".35em" text-anchor="middle">H2</text><circle class="n" cx="470" cy="40" r="18"/><text class="t" x="470" y="40" dy=".35em" text-anchor="middle">M1</text><circle class="n" cx="556" cy="40" r="18"/><text class="t" x="556" y="40" dy=".35em" text-anchor="middle">M2</text><circle class="n" cx="513" cy="126" r="18"/><text class="t" x="513" y="126" dy=".35em" text-anchor="middle">M3</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">Arrows point from derived to base. Left, single (B1, D1); centre, hierarchical (H0 base with H1 and H2 derived); right, multiple (M1 and M2 bases, M3 derived)</figcaption></figure>
Key points.
- Single inheritance has one base class and one derived class, for example
class B : public A. - Multiple inheritance has two or more base classes and one derived class, for example
class C : public A, public B. - Hierarchical inheritance has one base class and several derived classes.
- Multilevel and hybrid inheritance are the remaining forms, a chain A to B to C and a mix of the others.
- A derived class keeps the base's public and protected members and can add its own.
- Multiple inheritance can cause ambiguity when both bases have a member of the same name, which is resolved with the scope operator
A::show(). - In the diamond case a class reaches the same grandparent through two paths and receives two copies of it.
| Basis | Single | Hierarchical | Multiple |
|---|---|---|---|
| Structure | Base to derived | One base to many derived | Many bases to one derived |
| Base classes | 1 | 1 | 2 or more |
| Derived classes | 1 | 2 or more | 1 |
| Syntax | class D : public A |
class D1 : public A, class D2 : public A |
class D : public A, public B |
| Example | Vehicle to Car | Shape to Circle and Square | Student and Sports to Result |
Virtual base class. A virtual base class is a base inherited with the keyword virtual, so that the diamond problem gives only one shared copy of it. Without it, Intern in the code below would hold two age members, and i.age would be ambiguous.
class Person { public: int age; };
class Student : virtual public Person {};
class Employee : virtual public Person {};
class Intern : public Student, public Employee {};
// Intern i; i.age = 20; -> one copy, no ambiguity
Example (multiple inheritance program).
#include <iostream>
using namespace std;
class Student { protected: int roll; public: void getRoll(int r){roll=r;} };
class Sports { protected: int score; public: void getScore(int s){score=s;} };
class Result : public Student, public Sports {
public: void show(){ cout<<"Roll: "<<roll<<"\nScore: "<<score<<endl; } };
int main(){ Result r; r.getRoll(7); r.getScore(85); r.show(); return 0; }
// Output: Roll: 7
// Score: 85
Answer frame. For the comparison, open with the definition of inheritance, draw the three class diagrams, then fill the table and close with one example each. For the virtual base class, open with the diamond problem, draw the diamond (Person at top, Student and Employee below, Intern at bottom), show the virtual syntax, and close by saying only one copy remains. For the program, open with the syntax class D : public A, public B, write the program, and show the output.
Pitfall: Forgetting the
virtualkeyword on both intermediate classes leaves two copies and an ambiguity error.
Asked: [7 marks] (Dec 2020) State the purpose of virtual base class? Asked: [7 marks] (Dec 2020) Differentiate between single, hierarchical and multiple inheritance. Asked: [7 marks] (Jun 2020) Write a C++ program to illustrate multiple inheritance.
Polymorphism
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Medium weight</span>
Definition. <mark>Polymorphism means "many forms": the same name or operator behaves differently depending on the context, for example the types or number of its arguments or the object it is called on.</mark>
Diagram.
<figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u5-02" viewBox="0 0 1546 194" width="1546" height="194" role="img" aria-label="Types of polymorphism"><style>#dsfig-u5-02 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u5-02 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u5-02 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u5-02 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u5-02 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u5-02 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u5-02 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u5-02 .t{fill:#16181D;font-weight:500}#dsfig-u5-02 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u5-02 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u5-02 .dot{fill:#16181D}#dsfig-u5-02 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u5-02 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u5-02 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u5-02 .ah{fill:#454C5A}#dsfig-u5-02 .ah.hi{fill:#2340B8}#dsfig-u5-02 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u5-02 .wl .t{font-size:12px;font-weight:700}#dsfig-u5-02 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u5-02 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u5-02 .e{stroke:#B1B7C3}html.dark #dsfig-u5-02 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u5-02 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u5-02 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u5-02 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u5-02 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u5-02 .t{fill:#E6E8ED}html.dark #dsfig-u5-02 .t.inv{fill:#0F1115}html.dark #dsfig-u5-02 .kd{stroke:#E6E8ED}html.dark #dsfig-u5-02 .dot{fill:#E6E8ED}html.dark #dsfig-u5-02 .ann{fill:#8FA3FF}html.dark #dsfig-u5-02 .lbl{fill:#858D9C}html.dark #dsfig-u5-02 .ptr{fill:#8FA3FF}html.dark #dsfig-u5-02 .ah{fill:#B1B7C3}html.dark #dsfig-u5-02 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u5-02 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u5-02 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u5-02 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah3" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah" d="M0,1 L9,5 L0,9 z"/></marker><marker id="ahh3" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah hi" d="M0,1 L9,5 L0,9 z"/></marker></defs><line class="e" x1="885.5" y1="37" x2="387.5" y2="101"/><line class="e" x1="885.5" y1="37" x2="1383.5" y2="101"/><line class="e" x1="387.5" y1="101" x2="138.5" y2="165"/><line class="e" x1="387.5" y1="101" x2="636.5" y2="165"/><line class="e" x1="1383.5" y1="101" x2="1134.5" y2="165"/><rect class="n" x="828.5" y="22" width="114" height="30" rx="8"/><text class="t" x="885.5" y="37" dy=".35em" text-anchor="middle">Polymorphism</text><rect class="n" x="268" y="86" width="239" height="30" rx="8"/><text class="t" x="387.5" y="101" dy=".35em" text-anchor="middle">Compile-time (early binding)</text><rect class="n" x="50.5" y="150" width="176" height="30" rx="8"/><text class="t" x="138.5" y="165" dy=".35em" text-anchor="middle">Function overloading</text><rect class="n" x="548.5" y="150" width="176" height="30" rx="8"/><text class="t" x="636.5" y="165" dy=".35em" text-anchor="middle">Operator overloading</text><rect class="n" x="1283.5" y="86" width="200" height="30" rx="8"/><text class="t" x="1383.5" y="101" dy=".35em" text-anchor="middle">Run-time (late binding)</text><rect class="n" x="1058" y="150" width="153" height="30" rx="8"/><text class="t" x="1134.5" y="165" dy=".35em" text-anchor="middle">Virtual functions</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">Types of polymorphism</figcaption></figure>
Key points.
- Compile-time polymorphism is resolved by the compiler before the program runs, so it is also called static or early binding.
- Function overloading uses one function name with different parameter lists, such as
add(int,int)andadd(float,float). - Operator overloading gives an operator a new meaning for user-defined types, such as
+adding twoComplexobjects. - Run-time polymorphism is resolved during execution through virtual functions, so it is called dynamic or late binding.
- Function overriding means the derived class redefines a base class function with the same name, return type and parameters.
- A base pointer calls the derived version only if the base function is
virtual; otherwise the base version runs.
Example (overriding).
class Shape { public: virtual void draw(){ cout<<"Shape\n"; } };
class Circle : public Shape { public: void draw(){ cout<<"Circle\n"; } };
// Shape *p; Circle c; p=&c; p->draw(); // Output: Circle
Answer frame. For the definition, open with "many forms", draw the type tree, explain compile-time with overloading examples and run-time with virtual functions, and close with the binding difference. For overriding, define it, contrast it with overloading, show the code above, and close by linking it to virtual functions and run-time polymorphism.
Asked: [7 marks] (Dec 2020, Jun 2020) Define polymorphism and its types? Explain the different types of Polymorphism. Asked: [7 marks] (Dec 2020) Explain overriding of member function with an example.
Static and dynamic binding
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>Binding is linking a function call to the function body; static binding does it at compile time and dynamic binding does it at run time.</mark>
Key points.
- Static (early) binding is used for normal functions and overloaded functions, and it is faster.
- Dynamic (late) binding is used for virtual functions called through a base pointer or reference.
- Dynamic binding chooses the function from the actual object type, which gives run-time polymorphism.
Message passing
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>Message passing is the way objects communicate, by sending a request to another object to execute one of its member functions.</mark>
Key points.
- A message names the object, the function to call and the arguments, as in
account.deposit(500). - The receiving object runs the matching member function and may return a value.
- Programs are thus built as a set of objects exchanging messages.
Benefits of OOP's
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Low weight</span>
Definition. <mark>OOP is a programming approach that organises a program around objects that combine data and functions, rather than around procedures.</mark>
Key points.
- The main features are objects, classes, encapsulation, abstraction, inheritance, polymorphism, dynamic binding and message passing.
- Inheritance gives reuse of tested code, and encapsulation gives data security.
- Programs are modular and easy to maintain, extend and map to real-world problems.
| Basis | POP | OOP |
|---|---|---|
| Focus | Functions and procedures | Objects and data |
| Approach | Top-down | Bottom-up |
| Data | Global and shared, less secure | Hidden inside objects, secure |
| Modularity | Functions | Classes and objects |
| Reusability | Limited | High, through inheritance |
| Examples | C, Pascal | C++, Java |
Asked: [7 marks] (Jun 2020) State the important features of Object Oriented Programming. Difference between procedure oriented programming and object oriented programming.
Disadvantage of OOP's
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>The disadvantages of OOP are the costs that come with its design, mainly size, speed and learning effort.</mark>
Key points.
- OOP has a steeper learning curve, because the concepts are harder to grasp than plain procedures.
- Programs are often larger and slower because of extra layers such as virtual function calls.
- Poor class design and over-use of inheritance make code complex, and small programs gain little from OOP.
Application of OOP's
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Medium weight</span>
Definition. <mark>C++ is an object-oriented language that suits any large, complex system where reuse, modularity and speed matter.</mark>
Key points.
- Operating systems and system software, such as parts of Windows and Linux tools, use C++ for speed and hardware access.
- Graphical user interfaces and desktop software, such as Adobe products and Qt applications, use classes for windows, buttons and menus.
- Database management systems, such as MySQL, use C++ for performance and object handling.
- Games and simulation or modelling software use objects to represent characters, vehicles and physical entities.
- Compilers, browsers and embedded or real-time control systems, such as Chrome, are written in C++.
- Other areas are CAD/CAM design tools, artificial intelligence and neural networks, and office automation.
Answer frame. Open by defining OOP and why it is needed for large software; list five areas with one named example each; close by saying that C++ combines reusability, modularity and near-hardware speed.
Asked: [7 marks] (Dec 2020, Jun 2020) What are the applications of C++? Explain any five applications of oops.
A simple program
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>A simple C++ program reads input, processes it and prints output using cin and cout inside main().</mark>
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << "Sum = " << a + b << endl;
return 0;
}
// Input 2 3 -> Output: Sum = 5
Key points.
cout <<is the insertion operator for output andcin >>is the extraction operator for input.endlends the line and flushes the output.return 0;tells the operating system that the program ended successfully.
Anatomy of program
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>The anatomy of a C++ program is its structure: header files, namespace, optional class and function definitions, and the main() function.</mark>
Key points.
- Include section:
#include <iostream>is a preprocessor directive that brings in the input-output library. using namespace std;lets names such ascoutbe written without thestd::prefix.- Class and function definitions come next, and execution always starts at
main(). - Comments
//and/* */are ignored by the compiler.
Creating a source file
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>A source file is a plain text file containing C++ code, written in an editor and saved with the extension .cpp.</mark>
Key points.
- Write the program in any text editor or IDE, such as Turbo C++ or Code::Blocks.
- Save it with the
.cppextension, for examplesum.cpp. - Header files use
.hand are included with#include.
Compiling and Linking
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>Compiling translates the source file into an object file, and linking joins the object file with library code to make an executable.</mark>
<figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u5-03" viewBox="0 0 596 80" width="596" height="80" role="img" aria-label="Source file (.cpp), preprocessor, compiler, object file (.obj or .o), linker joins libraries, executable (.exe)"><style>#dsfig-u5-03 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u5-03 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u5-03 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u5-03 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u5-03 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u5-03 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u5-03 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u5-03 .t{fill:#16181D;font-weight:500}#dsfig-u5-03 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u5-03 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u5-03 .dot{fill:#16181D}#dsfig-u5-03 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u5-03 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u5-03 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u5-03 .ah{fill:#454C5A}#dsfig-u5-03 .ah.hi{fill:#2340B8}#dsfig-u5-03 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u5-03 .wl .t{font-size:12px;font-weight:700}#dsfig-u5-03 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u5-03 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u5-03 .e{stroke:#B1B7C3}html.dark #dsfig-u5-03 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u5-03 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u5-03 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u5-03 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u5-03 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u5-03 .t{fill:#E6E8ED}html.dark #dsfig-u5-03 .t.inv{fill:#0F1115}html.dark #dsfig-u5-03 .kd{stroke:#E6E8ED}html.dark #dsfig-u5-03 .dot{fill:#E6E8ED}html.dark #dsfig-u5-03 .ann{fill:#8FA3FF}html.dark #dsfig-u5-03 .lbl{fill:#858D9C}html.dark #dsfig-u5-03 .ptr{fill:#8FA3FF}html.dark #dsfig-u5-03 .ah{fill:#B1B7C3}html.dark #dsfig-u5-03 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u5-03 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u5-03 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u5-03 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah4" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah" d="M0,1 L9,5 L0,9 z"/></marker><marker id="ahh4" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah hi" d="M0,1 L9,5 L0,9 z"/></marker></defs><path class="e" d="M59,40 L148,40" marker-end="url(#ah4)"/><path class="e" d="M188,40 L277,40" marker-end="url(#ah4)"/><path class="e" d="M317,40 L406,40" marker-end="url(#ah4)"/><path class="e" d="M446,40 L535,40" marker-end="url(#ah4)"/><g class="wl"><rect x="471.1" y="31" width="40.8" height="18" rx="9"/><text class="t" x="491.5" y="40" dy=".35em" text-anchor="middle">link</text></g><circle class="n" cx="40" cy="40" r="18"/><text class="t" x="40" y="40" dy=".35em" text-anchor="middle">SRC</text><circle class="n" cx="169" cy="40" r="18"/><text class="t" x="169" y="40" dy=".35em" text-anchor="middle">PRE</text><circle class="n" cx="298" cy="40" r="18"/><text class="t" x="298" y="40" dy=".35em" text-anchor="middle">CMP</text><circle class="n" cx="427" cy="40" r="18"/><text class="t" x="427" y="40" dy=".35em" text-anchor="middle">OBJ</text><circle class="n" cx="556" cy="40" r="18"/><text class="t" x="556" y="40" dy=".35em" text-anchor="middle">EXE</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">Source file (.cpp), preprocessor, compiler, object file (.obj or .o), linker joins libraries, executable (.exe)</figcaption></figure>
Key points.
- The preprocessor expands
#includeand#definelines before compilation. - The compiler checks syntax and produces an object file, and syntax errors are reported here.
- The linker joins the object file with library functions such as
cout, and unresolved names give linker errors. - The loader then loads the executable into memory to run.
Last-minute revision
- An object is an instance of a class, and a class is a blueprint that uses no memory until objects are made.
- Encapsulation wraps data and functions into one unit, and abstraction shows only essential details.
- Single inheritance has 1 base and 1 derived, hierarchical 1 base and many derived, multiple many bases and 1 derived.
- A virtual base class (
virtualkeyword) gives one shared copy in the diamond problem. - Multiple inheritance syntax is
class D : public A, public B. - Polymorphism has compile-time types (function and operator overloading) and run-time types (virtual functions).
- Overriding is the same signature redefined in the derived class and needs
virtualfor run-time binding. - Static binding is at compile time, and dynamic binding is at run time.
- POP is function-focused with global data, while OOP is object-focused with hidden data.
- C++ applications include operating systems, GUIs, databases, games, compilers and embedded systems.
- The build path is
.cppto object file to executable through the compiler and linker.
Memory hooks
- "A PIE" covers the features: Abstraction, Polymorphism, Inheritance, Encapsulation.
- Class is the cookie cutter, and objects are the cookies.
- Single 1-1, hierarchical 1-many, multiple many-1.
- Overload means same name with different arguments, and override means same signature in a child class.
- Compile then link: the compiler makes
.obj, and the linker makes.exe.
Coverage checklist
- Basic concepts of OOP: object
- class (Q3 class versus object)
- data abstraction
- data encapsulation
- inheritance (Q4 virtual base class, Q5 single, hierarchical and multiple, Q6 multiple inheritance program)
- polymorphism (Q7 overriding, Q8 polymorphism and its types)
- Static and dynamic binding
- message passing
- benefits of OOP’s (Q2 features and POP versus OOP)
- disadvantage of OOP’s
- application of OOP’s (Q1 applications of C++)
- a simple program
- anatomy of program
- creating a source file
- compiling and Linking