Kurt Nørmark
Department of Computer Science, Aalborg University
Abstract Previous lecture Next lecture Index References Contents | In this lecture we discuss the fundamental abstraction mechanisms in C++: classes and structs. At the end of the lecture, the concept of C++ friends is explored in relatively great details. |
From C# classes to C++ classes Slide Annotated slide Contents Index References |
|
Program: Class Point in C#. |
|
Program: Class Point i C++ together with a main function - member functions defined in class definition. |
|
Program: Class Point i C++ together with a main function - member functions defined outside the class. |
|
Program: Class Point i C++ - with an output function for points. |
|
Organization of classes and members Slide Annotated slide Contents Index References |
|
Program: The class design of class Point. |
|
|
Classes, structs and namespaces Slide Annotated slide Contents Index References |
|
|
|
|
Program: Struct Point in C++ . |
|
Program: A namespace with struct Point in C++ . |
|
Program: Same with illustration of ambiguities relative to the global namespace. |
|
Functions outside classes Slide Annotated slide Contents Index References |
|
|
|
|
|
Constructors Slide Annotated slide Contents Index References |
|
|
|
Program: The Point class. |
|
Program: Implementation of the Point class constructors. |
|
Program: The Rectangle class. |
|
Program: Implementation of the Rectangle class constructors. |
|
Program: Sample Rectangle constructions. |
|
Exercise 3.2. Methods in class Point and class Rectangle | In this exercise we will add methods to class Point and Rectangle from the enclosing slide. First, however, we notice that a rectangle contains its two points. In a similar Java or C# program (programmed with classes) the rectangle would have references (in the meaning of C++ pointers) to two points. Add a couple of methods that access the x and y coordinates of a point. This is very easy. Do you miss C#-like properties in C++? Next, write two methods that access the two corner points of a rectangle. Do you want to return copies of the points? Pointers to the points? Or references to the points? Please play with each of these possiblities, and discuss pros and cons. Add an area method to Rectangle. This method will most likely need to access to the x and y coordinates of the corner points of the rectangle. Add move methods to both class Point and Rectangle. The methods should implement relative displacement of points and rectangles. In Rectangle::move, are you able to establish (C++) references to its two corner points, such that moving the two points also will also move the rectangle? If you get too frustrated by dealing with Point& you may consider a version that works on Point*... Finally, supply output functions for Point and Rectangle objects, by overloading operator<< for these two classes. |
Program: Illustration of legal and illegal member initializers - constructor chaining is not supported. |
|
Constructors - initialization versus assignment Slide Annotated slide Contents Index References |
|
|
Program: The Rectangle class - header file. |
|
Program: Rectangle constructors - with initialization. |
|
Program: Rectangle constructors - with default initialization and subsequent assignments. |
|
|
More about constructors Slide Annotated slide Contents Index References |
|
|
|
Use of constructors Slide Annotated slide Contents Index References |
|
|
Program: Class Point with a variety of constructors. |
|
Program: Funny implementations of constructors. |
|
Program: Use of constructors - with automatic variables of class type. |
|
Program: Actual program output. |
|
Program: An additional Point construction attempt - a trap. |
|
Exercise 3.3. Use of constructors with object on the free store | Rewrite the program shown above such that all Point objects are constructed in the free store (on the heap). The dynamically allocated points should have the same (x,y) coordinates as the points in the program shown above. This involves use of pointer and dynamic allocation, instead of automatic variables of class type and 'static allocation'. The version of the program produced in this exercise is - in some respect - similar to a Java or C# version of the program. Discuss! Consider how to initialize the array ap, which in this version of the program should be an array of Point pointers. |
Destructors Slide Annotated slide Contents Index References |
|
|
|
Program: Class Point with a destructor. |
|
Program: Implementation of the destructor that reveal its activation. |
|
Program: Illustration of destructor activation when automatic variables go out of scope . |
|
Program: Actual program output. |
|
A class that needs a destructor Slide Annotated slide Contents Index References |
|
Program: Class Point - with a pointer representation . |
|
Program: Implementation of class point - with an insufficient destructor . |
|
Program: A sample program with memory leaks. |
|
Program: Class Point - with a pointer representation - same as before. |
|
Program: Implementation of class point - now with a destructor that deallocates the Point representation. |
|
Program: A sample program - now without memory leaks. |
|
Program: Output from the program. |
|
Exercise 3.4. Point destruction - now with a problematic point copy constructor | We continue our work with class Point, using a dynamically allocated point representation. It may be beneficial to review the two version of the classes (together with client programs) on the accompanying slide. In the verison below we add a copy constructor to class Point. This constructor turns out to be problematic. // Redoing the example - same header file as the previous version. class Point { private: double *point_representation; public: Point(); Point(double, double); Point(Point&); // A new copy constructor ~Point(); // Destructor double getx () const; double gety () const; void move(double dx, double dy); }; std::ostream& operator<<(std::ostream&, const Point&); The class is implemented as follows: #include <cmath> #include <iostream> #include "point.h" Point::Point(){ point_representation = new double[2]; point_representation[0] = 0.0; point_representation[1] = 0.0; } Point::Point(double x_coord, double y_coord){ point_representation = new double[2]; point_representation[0] = x_coord; point_representation[1] = y_coord; } Point::Point(Point& p): point_representation(p.point_representation){ // ATTEMPTING copy construction. } Point::~Point(){ std::cout << "Deleting point" << "(" << getx() << "," << gety() << ")" << std::endl; delete[] point_representation; } double Point::getx () const{ return point_representation[0]; } double Point::gety () const{ return point_representation[1]; } void Point::move(double dx, double dy){ point_representation[0] += dx; point_representation[1] += dy; } std::ostream& operator<<(std::ostream& s, const Point& p){ return s << "(" << p.getx() << "," << p.gety() << ")" ; } There are problems in the following program: (1) When point r is moved, s is also moved. This is not as intended. In addition, the program aborts on exit from the function f. #include <iostream> #include "point.h" using namespace std; int f(){ Point p, q, r(11.0, 12.0); cout << "Point p: " << p << endl; // (0,0) cout << "Point q: " << q << endl; // (0,0) cout << "Point r: " << r << endl; // (11,12) Point s(r); // s is intended to be a copy of r. cout << "Point s: " << s << endl << endl; // (11,12) r.move(1.0, 2.0); // Moves r. cout << "Point s: " << s << endl; // (12,14) !! s is ALSO MOVED - why? cout << "Point r: " << r << endl << endl; // (12,14) // PROGRAM ABORTS ON EXIT FROM F - why? } int main(){ f(); } Explain both of the problems, and make a correct version of class Point. |
|
Resource acquisition is initialization - RAII Slide Annotated slide Contents Index References |
|
|
Program: A class Resource and its application in the function use_resource - principles only. |
|
Program: A class Resource and its application in the function use_resource - compilable version. |
|
Program: Program output - with no problem_condition. |
|
Program: Program output - with a problem_condition. |
|
Auto Pointers Slide Annotated slide Contents Index References |
|
|
|
|
Program: The usual class Point - nothing of particular interest. |
|
Program: An illustration of auto_ptr<Point>. |
|
Program: The program output. |
|
|
Object copying Slide Annotated slide Contents Index References |
|
|
|
Copying Point objects in parameter passing Slide Annotated slide Contents Index References |
|
Program: Class Point with a variety of constructors. |
|
Program: Funny implementations of constructors. |
|
Program: Passing Point objects in and out of functions. |
|
Program: Actual program output. |
|
Example of copying objects: Default copying Slide Annotated slide Contents Index References |
|
|
Program: Class LineSegment WITHOUT copy constructor and and WITHOUT assignment operator. Header file. |
|
Program: The corresponding cc file - not particularly relevant. |
|
Program: A function that constructs, initializes and assigns LineSegments. |
|
Program: Program execution. |
|
|
Example of copying objects: Programmed copying Slide Annotated slide Contents Index References |
|
|
Program: Class LineSegment WITH a copy constructor and and WITH an assignment operator. Header file. |
|
Program: The corresponding cc file with implementations of the copy constructor and the assignment operator. |
|
Program: A function that constructs, initializes and assigns LineSegments. |
|
Program: Program execution. |
|
|
|
Preventing object copying Slide Annotated slide Contents Index References |
|
|
|
|
Classes and Conversion Slide Annotated slide Contents Index References |
Item 5 of More Effective C++ is informative about implicit type conversions |
|
|
Program: istream conversion to bool. |
|
Program: The while loop in the context of a full C++ program. |
|
Implicit Conversion Slide Annotated slide Contents Index References |
|
|
Classes and Conversion: Examples Slide Annotated slide Contents Index References |
|
Program: Class Point with conversion constructor and conversion operator, from and to double. |
|
Program: Class Point implementation. |
|
Program: Use implicit of the conversions. |
|
Program: Program output. |
|
|
Program: Class Tripple with Tripple(Point) constructor and a Point conversion operator. |
|
Program: Class Tripple implementation. |
|
Program: Illustration of conversions. |
|
Program: Program output. |
|
Exercise 3.5. Conversion via constructors | In the program shown above, a Point can be converted to a Tripple via a constructor Tripple(Point). The other way around, we convert a Tripple to a Point with use of a conversion operator in class Tripple In this exercise we want to provide for a more symmetric solution. We keep the constructor Tripple(Point). Your task is to program the symmetric constructor Point(Tripple). Your starting point is the header file and the cpp file You may enconter a couple of minor challenges when you rewrite the program. First, class Tripple must now be declared (it must be known by the compiler) in order to compile class Point. The reason is that class Point now uses the name Tripple (in the new constructor). As another challenge, the new constructor Point(Tripple) in class Point needs access to the three encapsulated, private data members in class Tripple. Consider setting up appropriate friendship, or program accessor member functions. With this in place, rerun the program from above. Do you get the expected results (without surprises)? |
Static class members Slide Annotated slide Contents Index References |
|
|
Program: A variant of class Point with static members. |
|
Program: Implementation of class Point. |
|
Program: A client of class Point. |
|
Program: Program output. |
|
|
|
Program: A variant of class Point with static member function for the defaultPoint. |
|
Program: Implementation of class Point. |
|
Program: A client of class Point. |
|
Const member functions Slide Annotated slide Contents Index References |
|
|
|
Program: A variant of Point with cached polar representation. |
|
Program: The implementation of class Point - with compilation problems. |
|
|
Const member functions - const and mutable Slide Annotated slide Contents Index References |
|
|
Program: A variant of Point with cached polar representation. |
|
Program: The implementation of class Point - with compilation problems. |
|
Program: A simple client program of Point. |
|
Program: Program output. |
|
|
Object Self-reference Slide Annotated slide Contents Index References |
|
|
Program: Class Point - almost the usual header file. |
|
Program: Class Point - a variant with explicit use of this. |
|
Program: A sample use of points, with emphasis on moving. |
|
|
Inline member functions Slide Annotated slide Contents Index References |
|
|
|
Concrete classes Slide Annotated slide Contents Index References |
|
|
|
|
Visibility and Access Control Slide Annotated slide Contents Index References |
|
|
|
Friends Slide Annotated slide Contents Index References |
|
|
|
|
Friends - Example 1 Slide Annotated slide Contents Index References |
|
Program: A function f get access to the private state of both class A and B. |
|
|
Friends - Example 2 Slide Annotated slide Contents Index References |
|
Program: Class A provides friendship to class B. |
|
Program: Class A and B are mutual friends of each other - problems in this version. |
|
Program: Class A and B are mutual friends of each other - this version is OK. |
|
Program: A variant were we want ma to be a friend of B and mb to be friend of A - problems in this version. |
|
Friends - class Point - notational convenience Slide Annotated slide Contents Index References |
|
Program: Class Point - header file. |
|
Program: Class Point - implementation - not important for this example. |
|
Program: The program including implementation of friend moving functions. |
|
|
Friends - Class Point - operator friends Slide Annotated slide Contents Index References |
|
Program: Class Point - header file. |
|
Program: Class Point - implementation - including the implementation of operator<<. |
|
Program: The program that uses the operator - nothing new here. |
|
Friends - Class Point - implicit conversion Slide Annotated slide Contents Index References |
|
|
Program: Class Point - header file. |
|
Program: Class Point - implementation - including the implementation of operator<<. |
|
Program: The program that use the operator. |
|
Discussion - Encapsulation, Visibility and Access Slide Annotated slide Contents Index References |
|
|
Exercise 3.6. Discuss encapsulation, visibility and access | Discusss encapsulation, visibility and access, based on the enclosing slide. |
Operator overloading Slide Annotated slide Contents Index References |
|
|
|
|
|
Example: Operator overloading in class Point Slide Annotated slide Contents Index References |
|
Program: Class Point with operators as members. |
|
Program: Definition of Point member functions. |
|
Program: A program that illustrates uses the Point operators. |
|
Program: Program output. |
|
Program: Class Point with non-member operators. |
|
Program: Definition of Point member functions and non-member operators. |
|
Program: An identical program that illustrates uses the Point operators. |
|
Program: Program output. |
|
Collected references Contents Index |
|
Chapter 3: Abstraction Mechanisms, Part 1
Course home Author home About producing this web Previous lecture (top) Next lecture (top) Previous lecture (bund) Next lecture (bund)
Generated: March 26, 2013, 13:03:29