0% found this document useful (0 votes)
22 views

C++ Mini-Course: - Part 5: Inheritance - Part 6: Libraries - Part 7: Conclusion

This document summarizes parts 5-7 of a C++ mini-course covering inheritance, libraries, and concluding remarks. Part 5 discusses how inheritance works in C++ and differences from Java like using the "virtual" keyword. Part 6 covers namespaces for avoiding naming conflicts and using the standard template library (STL) for containers and algorithms. Part 7 provides additional resources for learning C++ like a Java to C++ tutorial and reference book.

Uploaded by

Livefor 786
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

C++ Mini-Course: - Part 5: Inheritance - Part 6: Libraries - Part 7: Conclusion

This document summarizes parts 5-7 of a C++ mini-course covering inheritance, libraries, and concluding remarks. Part 5 discusses how inheritance works in C++ and differences from Java like using the "virtual" keyword. Part 6 covers namespaces for avoiding naming conflicts and using the standard template library (STL) for containers and algorithms. Part 7 provides additional resources for learning C++ like a Java to C++ tutorial and reference book.

Uploaded by

Livefor 786
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

C++ Mini-Course

•Part 5: Inheritance
•Part 6: Libraries
•Part 7: Conclusion

CPSC 221, Texas A&M University

Adapted from: Brown CS123


http://www.cs.brown.edu/courses/cs123/resources/c++_mini_course.ppt
C++ Mini-Course

Part 5: Inheritance
How does inheritance work?

DottedSegment
must include parent publicly inherits from
header file Segment

#include “Segment.h”
class DottedSegment : public Segment
{
// DottedSegment declaration
};
virtual
• In Java every method invocation is dynamically
bound, meaning for every method invocation the
program checks if a sub-class has overridden
the method. You can disable this (somewhat) by
using the keyword “final” in Java
• In C++ you have to declare the method virtual if
you want this functionality. (So, “virtual” is the
same thing as “not final”)
• Just like you rarely say things are final in Java,
you should rarely not say things are virtual in
C++
pure virtual functions
• In Java, the “abstract” keyword means the
function is undefined in the superclass.
• In C++, we use pure virtual functions:
– virtual int mustRedfineMe(char *str) = 0;
– This function must be implemented in a
subclass.
Resolving functions
In Java: In C++:

// Overriding methods // Overriding methods


public void overloaded(){ void Subclass::overloaded(){
println(“woohoo”); cout<<“woohoo”<<endl;
super.overloaded(); Superclass::overloaded();
} }

//constructor //constructor
public Subclass(){ public Subclass() :
super(); Superclass()
} { }
virtual
• Basic advice: for now make every method
virtual except the constructor
• Make you declare your destructors virtual;
if you do not declare a destructor a non-
virtual one will be defined for you

Segment();
virtual ~Segment();

this is important
C++ Mini-Course

Part 6: Libraries
Namespaces
• Namespaces are kind of like packages in
Java
• Reduces naming conflicts
• Most standard C++ routines and classes
and under the std namespace
– Any standard C routines (malloc, printf, etc.)
are defined in the global namespace because
C doesn’t have namespaces
using namespace
#include <iostream>
...
std::string question =
“How do I prevent RSI?”;
std::cout << question << std::endl;

using namespace std;

string answer = “Type less.”;


cout << answer << endl;

Bad practice to do in header files!


STL
• Standard Template Library
• Contains well-written, templated
implementations of most data structures
and algorithms
– Templates are similar to generics in Java
– Allows you to easily store anything without
writing a container yourself
• Will give you the most hideous compile
errors ever if you use them even slightly
incorrectly!
STL example
#include <vector>
using namespace std;

typedef vector<Point> PointVector;


typedef PointVector::iterator PointVectorIter;

PointVector v;
v.push_back(Point(3, 5));

PointVectorIter iter;
for(iter = v.begin(); iter != v.end(); ++iter){
Point &curPoint = *iter;
}
C++ Mini-Course

Part 7: Conclusion
Other Resources
• The Java To C++ tutorial on the website is
probably your best source of information
• The big thick book by Stroustrop in the
back of the Sun Lab is the ultimate C++
reference
• A CS 123 TA, or specifically your mentor
TA if you have been assigned one
Question and Answer
Session

You might also like