Q: Why are destructors needed in C++?
C++ does not have in-built garbage collection (there are external ones), so some manual memory management is needed. But there are also smart pointers now.
Q: Why doesn't C++ use garbage collectors? (GC works well in Java!)
It's complicated, but tl;dr - C/C++ is a little too low level for GC to work well (+ remember that C/C++ is also meant to work on extremely low-resource settings - even a simple GC will create too much overhead)
2. Inheritance in C++
New concept: Pure method
class Shape {
public :
Shape() { }
virtual void area() = 0 ;
// virtual void area() { cout << "undefined" << endl ;}
void name() { cout << " a shape" << endl ; }
};
class Rectangle : public Shape {
...
public :
Rectangle(int x, int y) : _length(x) , _height(y) { }
void area() { cout << "area is " << _length * _height << endl ; }
}
Basically this creates an abstract method and the class becomes an abstract class too.
// Make sure you watch out for the function signatures when you define the sub-classes - they
should be the same!
(i.e. if there is a `const` in your method in the superclass, it should be in your sub-class too)
3. Polymophism in C++
New concept: virtual
int main() {
Rectangle rect(10,20);
Shape *shapePtr = &rect ;
Shape &shapeRef = rect ;
Shape shapeVal = rect ;
shapePtr->area() ;
shapeRef.area() ;
shapeVal.area() ;
}
What adding the keyword virtual does: "force method evaluation to be based on object type rather than reference type"
Without virtual, all 3 lines will print the same thing (i.e. "undefined")
More about pointers and references here (section 2.4).
4. Array of Objects (build a zoo)
Keep things simple:
Having an array of 3 objects (๐ถ,๐ธ,๐ฆ) is fine, if you want to have more that's fine too as long as you don't run into any errors
You need to initialize your arrays (e.g. 0 or Mammal object) so that you don't run into issues (like uninitialized values) when iterating through the loop when the Zoo is not full ๐
Sending an animal to the zoo will then mean that you're replacing the value of the existing (null) value / object in the array
Remember to free the animals at the end (i.e. delete the objects and free up memory)
5. Header and Implementation
(abstraction!)
Many different ways to do it, so it's up to you: a basic example.
Try to at least have animal.h, childAnimal.h, ZooMain.cpp
(But it's cleaner to have animal.cpp and childAnimal.cpp too)
More scaffolds
Not in lab and Visual Studio takes up too much space? Eclipse works, CodeBlocks too or even VSCode (weird errors though)
Recap about the difference between compiling, linking and building
You might need to have #include guards. Here's how it works.
Destructors can be virtual too, some IDEs might warn but it's fine
You'll face issues printing the value stored in the enum - here's one way to solve it and here are more ways to solve it. But you can fix it with switch statements too.
Lab 5 Deliverables
Submit 1 .zip file to NTULearn (under โAssignmentsโ)
Your zip file should contain both .h and .cpp files
Depending on how you separated them you might have different number of files
You should have the main function in ZooMain.cpp
Follow the filename convention specified on NTULearn
Please submit it within 2 weeks from this lab!
And... you're done (with the labs)!
Hope you've learnt a lot :)
๐
Take-home Messages ๐
Knowing the best tool for the job makes you very valuable in the industry - be a jack of many trades (know the masters), master of one
IRL, people use a mix of paradigms (depending on the team too)
Over time, you should also develop the ability to break down a big task into smaller parts that are reusable and extensible
OOP is done at a higher level of abstraction + attributes & methods are tightly coupled - use when objects are the best representation
OOP principles are not unique, but inheritance is hard to achieve without OOP (i.e. if the task can be modelled nicely with a hierarchy of classes, OOP is probably the right tool)
The next few mods in your SCSE journey goes higher (unless you're in CE, where it goes lower ๐ค)
Even within CS, there are huge variations in terms of content type
Cybersec, external certs, very technical, experience-heavy
ML, math heavy, need own effort to explore ; research / intern exp
SE, arbitrary, should have portfolio of works (your own website!)
The earlier you figure out what you want to do, the better you can prepare / read ahead and you'll naturally do better. Do internships.
When others give you advice, rmb that they don't know your b/g.
References used
This set of slides is made using reveal.js.
It's really easy to make a basic set of slides (just HTML) and you can consider using it for simple (tech) presentations!
For more advanced customization, you do need CSS and JS but scripts can be easily googled for and it has good documentation.
Slide 1 image | Code snippets are from lecture notes
Here's even more info about OOP in C++
Please read ahead about design patterns (implement them) especially if you're interested in software engineering and especially if you're not, since it's covered (to varying extents) in CZ2006/3002/3003/3007 and it takes time to internalize.
All the best! :)