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

08 Object Based Programming

The document discusses object-oriented programming concepts in C/C++ including operations on structs, constructors, destructors, and constant methods. It provides examples of defining methods within structs and calling those methods on struct objects. Constructors are described as special methods that initialize objects, and destructors are called automatically to cleanup objects when they go out of scope. Case studies on linked lists and binary trees demonstrate using constructors and destructors to properly allocate and free memory for objects.

Uploaded by

anidcohen9058
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

08 Object Based Programming

The document discusses object-oriented programming concepts in C/C++ including operations on structs, constructors, destructors, and constant methods. It provides examples of defining methods within structs and calling those methods on struct objects. Constructors are described as special methods that initialize objects, and destructors are called automatically to cleanup objects when they go out of scope. Case studies on linked lists and binary trees demonstrate using constructors and destructors to properly allocate and free memory for objects.

Uploaded by

anidcohen9058
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

8. Object-based Programming 6. Juli 2011

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 1 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

Outline

Recapitulation Operations on Structs: Methods Constructors and Destructors The Const Modier Encapsulation & Information Hiding Case Study: Types for Linear Algebra Case Study: A Binary Tree &

Depth-rst Traversal

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 2 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

Recapitulation
int a; int b; a = 3; b = &a ; b = 4; b = 5 ;

Explain/sketch what the individual lines do. What does b++; What happens if we write int a, b;

What happens if we call a function foo(int*) with b.

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 3 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

8.1. Operations on Structs

/ Represents a date . Each month s h a l l have 30 days . / s t r u c t Date { i n t month , day ; }; v o i d switchToNextDay ( Date& date ) { ... }

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 4 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

Class Diagrams with the Unied Modelling Language

The Unied Modelling Language is a

graphical representation of your system design.


Date int month int day switchToNextDay()

Operation and the data here go

hand-in-hand. UML illustrates this fact.


Object-based paradigm: Model whole

system in terms of structs and operations acting on these structs.


Remember: Structs can hold other

structs as attributes.
Remember: Structs can hold pointers

to other structs.

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 5 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

Operations on Structs Rewritten

/ Represents a date . Each month s h a l l have 30 days / s t r u c t Date { i n t month , day ; }; / / Declaration v o i d switchToNextDay ( Date& date ) { . . . }

/ Represents a date . Each month s h a l l have 30 days / s t r u c t Date { i n t month , day ; / / Belongs t o t h e s t r u c t i t i s embedded i n t o v o i d switchToNextDay ( ) ; };

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 6 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

Operations on Structs Rewritten

/ / Date . h s t r u c t Date { i n t month , day ; v o i d switchToNextDay ( ) ; }; / / Date . cpp # i n c l u d e Date . h v o i d Date : : switchToNextDay ( ) { day ++; ... } Syntax is similar to namespaces. It is now clear, how operations and data belong together. A good object always works solely on data of its own. If it has to manipulate data

from another object, it should use this objects functions (operations, methods).
Internally, it is still your straightforward C realisation.
8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 7 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

Invoke Operations on Structs

/ / Date . h s t r u c t Date { i n t month , day ; v o i d switchToNextDay ( ) ; }; ... Date myDate ; myDate . month = 7 ; myDate . day = 5; myDate . switchToNextDay ( ) ;

Create a variable: instantiate. Variable: object. Variables of a struct: attributes. Call a structs operation: invoke a method.

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 8 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

8.2. Constructors and Destructors

/ / Date . h s t r u c t Date { i n t month , day ; v o i d switchToNextDay ( ) ; }; ... Date myDate ; myDate . month = 7 ; myDate . day = 5; myDate . switchToNextDay ( ) ;

The two set operations also belong to

the struct itself.


It might be good design to write an

initialisation operation.
Add a method init(int month, int

day);.

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 9 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

Constructors
/ / Date . h s t r u c t Date { i n t month , day ; v o i d switchToNextDay ( ) ; Date ( i n t month , i n t day ) ; }; / / Date . cpp Date : : Date ( i n t month , i n t day ) : month ( month ) , day ( day ) { / / some checks }

A constructor is a special type of

operation.
Its name equals the struct name. It has no return type. It uses initialisation lists.

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 10 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

Creating an Instance
/ / Date . h s t r u c t Date { i n t month , day ; v o i d switchToNextDay ( ) ; Date ( i n t month , i n t day ) ; Date ( ) ; Date ( c o n s t char s t r i n g R e p r e s e n t a t i o n ) ; }; / / Another f i l e Date myDate1 ( 7 ,5 ) ; Date myDate2 ( ) ; / / doesn t work Date myDate3 ; Date myDate4 ( 5 J u l y ) ;

We can overload constructors. The default constructor is invoked without brackets. There is no need for a default constructor. If you dont provide a constructor at all, C++ automatically (in the background)

generates a default constructor.


8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 11 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

Object Destruction
... w h i l e ( something ) { Date myDate ( . . . ) ; / / we do something }

Instance of Date is destroyed at the end of the scope. If there is a constructor, why isnt there a counterpart?

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 12 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

Destructor Syntax
/ / Date . h s t r u c t Date { i n t month , day ; v o i d switchToNextDay ( ) ; Date ( i n t month , i n t day ) ; Date ( ) ; Date ( ) ; }; / / Date . cpp Date : : Date ( ) { }

Destructor is called always at the end of the scope containing the object. We cannot overload destructors. Destructors dont have a return value. A destructor is never called explicitly. If you dont dene a destructor, C++ automatically generates one.

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 13 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

Case Study: Single Linked List & Constructors


s t r u c t ListElement { ListElement nextElement ; int value ; ListElement ( ) ; v o i d connect ( L i s t E l e m e n t n e x t L i s t E l e m e n t ) ; }; ... ListElement : : ListElement ( ) : nextElement ( 0 ) {}

For our algorithms, it is important that the last list element points to 0, i.e. whenever we create a list element, we manually have to ensure that is points

to 0.
With the constructors, we can ensure that each single element equals a list of

length one.

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 14 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

Case Study: Single Linked List & Destructors


s t r u c t ListElement { ListElement nextElement ; ... ListElement ( ) ; }; ... L i s t E l e m e n t m y L i s t = new L i s t E l e m e n t ( ) ; / / append some l i s t elements delete myList ;

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 15 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

Case Study: Single Linked List & Destructors


s t r u c t ListElement { ListElement nextElement ; ... ListElement ( ) ; }; ... L i s t E l e m e n t m y L i s t = new L i s t E l e m e n t ( ) ; / / append some l i s t elements delete myList ;

So far, a delete always induced a memory leak. With the destructors, we can ensure that all connected list elements are deleted

as well.
ListElement : : ListElement ( ) { i f ( nextElement ! = 0 ) d e l e t e }

nextElement ;

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 15 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

Objects on the Heap


/ / Date . h s t r u c t Date { i n t month , day ; v o i d switchToNextDay ( ) ; Date ( i n t month , i n t day ) ; Date ( ) ; Date ( ) ; }; ... Date myDate = new Date ( 7 ,5 ) ; ... myDate >switchToNextDay ( ) ; ... d e l e l t e myDate ;

new reserves memory (as it does in C), and new invokes the constructor. delete invokes the destructor, and it frees the memory (as it does in C). Remember of linked list: If we delete the rst list entry, this entry can also free the subsequent entries. It is much easier to enforce the consistency with these new data structures.
page 16 of 35

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

Arrays of Objects
/ / Date . h s t r u c t Date { i n t month , day ; v o i d switchToNextDay ( ) ; Date ( i n t month , i n t day ) ; Date ( ) ; Date ( ) ; }; Date myDates [ 1 0 ] ; ... myDate[4]> switchToNextDay ( ) ; ...

Arrays of objects are supported by C++. However, such data structures have to have a standard constructor.

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 17 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

Explicit Constructor Calls


/ / Date . h s t r u c t Date { i n t month , day ; v o i d switchToNextDay ( ) ; Date ( i n t month , i n t day ) ; Date ( ) ; Date ( ) ; }; Date myDate1 = Date ( 7 ,5 ) ; Date myDate2 = Date ( ) ; ...

Here, we need the parentheses. Obviously, the constructor is kind of a function which returns a struct. This is bad style, as it induces a bit-wise copy.

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 18 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

8.3. Const Methods . . . why does this snippet not work?


struct IntegerEntry { int value ; I n t e g e r E n t r y next ; / / P r i n t t h i s e n t r y t o t e r m i n a l and c o n t i n u e w i t h n e x t e n t r y . void p r i n t L i s t ( ) ; / / Make argument n e x t argument . v o i d append ( I n t e g e r E n t r y n e x t E n t r y ) ; }; ... v o i d doSomething ( c o n s t I n t e g e r E n t r y & e n t r y ) { ... entry . p r i n t L i s t ( ) ; }

Call-by-value is expensive, as it copies the whole object bit-wise. This lead to a performance breakdown in this code. Thus, we used call-by-const-reference. However, the code now does not compile anymore.

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 19 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

Const Keyword
struct IntegerEntry { int value ; I n t e g e r E n t r y next ; / / P r i n t t h i s e n t r y t o t e r m i n a l and c o n t i n u e w i t h n e x t e n t r y . void p r i n t L i s t ( ) const ; / / Make argument n e x t argument . v o i d append ( I n t e g e r E n t r y n e x t E n t r y ) ; }; ... v o i d doSomething ( c o n s t I n t e g e r E n t r y & e n t r y ) { ... entry . p r i n t L i s t ( ) ; }

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 20 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

Semantics of Const
struct IntegerEntry { int value ; I n t e g e r E n t r y next ; / / P r i n t t h i s e n t r y t o t e r m i n a l and c o n t i n u e w i t h n e x t e n t r y . void p r i n t L i s t ( ) const ; }; void I n t e g e r E n t r y : : p r i n t L i s t ( ) const { value = 2; / / e r r o r r e t u r n value ; / / o . k . }

const operations may not alter object state. const operations may not call non-const methods. const operations can be invoked on objects passed by call-by-const-reference. const operations allow the compiler to optimise. const operations allow user to enforce encapsulation and to write safer apps.

use the const modier whenever possible.

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 21 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

Const Variants
struct IntegerEntry { int value ; I n t e g e r E n t r y next ; c o n s t i n t getValue ( ) ; c o n s t i n t getValue ( ) c o n s t ; / / variant A i n t getValue ( ) c o n s t ; i n t getValue ( ) ; / / variant B c o n s t i n t & getValue ( ) c o n s t ; c o n s t i n t & getValue ( ) ; };

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 22 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

Const Variants
struct IntegerEntry { int value ; I n t e g e r E n t r y next ; c o n s t i n t getValue ( ) ; c o n s t i n t getValue ( ) c o n s t ; / / variant A i n t getValue ( ) c o n s t ; i n t getValue ( ) ; / / variant B c o n s t i n t & getValue ( ) c o n s t ; c o n s t i n t & getValue ( ) ; };

const belongs to the signature, i.e. we can overload with respect to const. const after the operation enforces the operation not to manipulate object state. const before the return argument does not allow programmer to manipulate result. The compiler tries to use const operations before it falls back to non-const

operations.
8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 22 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

8.4. Classes
Edsger Dijkstra, The Humble Programmer (EWD340), Communications of the ACM: The major cause of the software crisis is that the machines have become several orders of magnitude more powerful! To put it quite bluntly: as long as there were no machines, programming was no problem at all; when we had a few weak computers, programming became a mild problem, and now we have gigantic computers, programming has become an equally gigantic problem.

Projects running over-budget. Projects running over-time. Software was very inefcient. Software was of low quality. Software often did not meet requirements. Projects were unmanageable and code difcult to maintain. Software was never delivered.

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 23 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

Resume
With the new technique at hand, we can encapsulate data and operations as

these two things go hand in hand.


We can write a couple of setter and getter operations to allow the user to

manipulate our brand new data structure.


However, the user still can reset attributes manually. We can not forbid this. Consequently, we need an alternative, a new technique to forbid this. Furthermore, it would be nice if the user doesnt even see the attributes, as it might be reasonable that the user cant even read attributes if we dont provide

the corresponding getters (next pointer in our list example, e.g.).

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 24 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

Our Beloved Colleagues


/ Represents a date . Each month s h a l l have 30 days . / s t r u c t Date { i n t month , day ; v o i d switchToNextDay ; };

/ / t h i s i s t h e code our c o l l e a g u e wrote Date myDate ( . . . ) ; myDate . day ++;

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 25 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

Encapsulation
/ Represents a date . Each month s h a l l have 30 days . / c l a s s Date { private : i n t month , day ; public : v o i d switchToNextDay ( ) ; };

In principle, class is an alias for struct, i.e. we can do all the things we can do with structs with classes, too. However, for classes we can create public and private sections. Only add them

in the header.
Private attributes and operations are not available from outside, but only within the

operations of the class (encapsulation).

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 26 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

Encapsulation at Work
/ Represents a date . Each month s h a l l have 30 days . / c l a s s Date { private : i n t month , day ; public : v o i d switchToNextDay ( ) ; };

... v o i d Date : : switchToNextDay ( ) { day ++; / / o . k . } Date myDate ; myDate . day ++; / / doesn t work .

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 27 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

Encapsulation and the Object Lifecycle


/ Represents a date . Each month s h a l l have 30 days . / c l a s s Date { private : i n t month , day ; Date ( ) ; public : v o i d switchToNextDay ( ) ; Date ( i n t month , i n t day ) ; };

We can make constructors private and, thus, forbid everybody to create an

instance of our object.


We could make the destructor private, too. However, this never makes sense.

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 28 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

Remark: Classes and Namespaces


namespace c a l e n d a r { / Represents a date . Each month s h a l l have 30 days . / c l a s s Date { private : i n t month , day ; Date ( ) ; public : v o i d switchToNextDay ( ) ; Date ( i n t month , i n t day ) ; }; } v o i d c a l e n d a r : : Date : : switchToNextDay ( ) { ... } / / f u l l y q u a l i f i e d argument here n o t necessary / / we can access p r i v a t e arguments o f o t h e r i n s t a n c e s v o i d c a l e n d a r : : Date : : copyFromOtherDate ( c o n s t c a l e n d a r : : Date& date ) { ... }

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 29 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

Remark: Classes and Recursion


class IntegerEntry { private : int value ; I n t e g e r E n t r y next ; public : v o i d append ( I n t e g e r E n t r y newEntry ) ; }; v o i d I n t e g e r E n t r y : : append ( I n t e g e r E n t r y newEntry ) { i f ( n e x t ==0) { n e x t = newEntry ; } else { next >append ( newEntry ) ; } }

This is not recursion, as the operation is invoked on another object. However, recursion and object-based programming work together.

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 30 of 35

Operations on Structs

Constructors and Destructors

Const Methods

Classes

An OO Case Study

Why initialisation lists are faster than assignments in the constructor.

8. Object-based Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 31 of 35

You might also like