10. Object-oriented Programming. 7. Juli 2011

Size: px
Start display at page:

Download "10. Object-oriented Programming. 7. Juli 2011"

Transcription

1 7. Juli 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 47

2 Outline Object Case Study Brain Teaser Copy Constructor & Operators Object-oriented Programming, i.e. Inheritance Dynamic and Static Polymorphism The Keyword Static Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 2 of 47

3 10.1. An OO Case Study Create a Struct Vector Create a struct Vector with three doubles and an additional double holding the length of the vector. s t r u c t Vector { double x [ 3 ] ; double length ; Create three vectors a = (0.2, 0.4), b = (1.0, 0.0), and c = ( 1.2, 0.0) in your main routine. The first two instances shall be variables, the latter instance shall be created on the heap. Vector a, b ; Vector c = new Vector ; / / assign values now Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 3 of 47

4 Compute the Length Assume there is an operation void computelength( Vector & vector) that computes the length of the passed vector and sets it accordingly. # i n c l u d e <cmath> s t r u c t Vector { double x [ 3 ] ; double length ; void computelength ( Vector& vector ) { vector. l e n g t h = std : : s q r t ( v ector. x [ 0 ] v e c t o r. x [ 0 ] + v ector. x [ 1 ]... ) ;... computelength ( a ) ; computelength ( c ) ; Make void computelength( Vector & vector) a method of Vector, i.e. change this piece of code into its object-based variant. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 4 of 47

5 Methods Instead of Functions # i n c l u d e <cmath> s t r u c t Vector { double x [ 3 ] ; double length ; void computelength ( ) ; void Vector : : computelength ( ) { l e n g t h = std : : s q r t ( x [ 0 ] x [ 0 ] + x [ 1 ]... ) ; a. computelength ( ) ; c >computelength ( ) ; Add an operation toterminal() writing the vector to the terminal. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 5 of 47

6 Methods Instead of Functions # i n c l u d e <cmath> s t r u c t Vector { double x [ 3 ] ; double length ; void computelength ( ) ; void Vector : : computelength ( ) { l e n g t h = std : : s q r t ( x [ 0 ] x [ 0 ] + x [ 1 ]... ) ; a. computelength ( ) ; c >computelength ( ) ; Add an operation toterminal() writing the vector to the terminal. void Vector : : toterminal ( ) { std : : cout << ( << x [ 0 ] <<, <<... ; Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 5 of 47

7 A Constructor Add a constructor to Vector which accepts three doubles and automatically ensures that length holds the right value. Do not use initialisation lists (makes it a little bit simpler). Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 6 of 47

8 A Constructor Add a constructor to Vector which accepts three doubles and automatically ensures that length holds the right value. Do not use initialisation lists (makes it a little bit simpler). # i n c l u d e <cmath> s t r u c t Vector {... Vector ( double x1, double x2, double x3 ) ; Vector : : Vector ( double x1, double x2, double x3 ) { x [ 0 ] = x1 ;... computelength ( ) ; Make your struct a class and try to manipulate the attribute length of a manually within the main function. Add a function scale(double value) that scales the vector and automatically also updates the length. Make computelength() private, too. Does your code still work? Why? Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 6 of 47

9 Setters and Getters Write an operation getlength(). Add a function void getangle( const Vector& a, const Vector& b). It is not defined within a class/object but is a plain (old-fashioned) function. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 7 of 47

10 Setters and Getters Write an operation getlength(). Add a function void getangle( const Vector& a, const Vector& b). It is not defined within a class/object but is a plain (old-fashioned) function. Make the operation getlength() a const operation. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 7 of 47

11 Brain Teaser class S t r i n g { p r i v a t e : / / ain t a 0 terminated s t r i n g char data ; / / length of s t r i n g i n t l e n g t h ; S t r i n g ( ) ; void copyfromotherstring ( const String& s t r i n g ) ;... S t r i n g : : S t r i n g ( ) : data ( 0 ), l e n g t h ( 0 ) { void String : : copyfromotherstring ( const String& s t r i n g ) { data = s t r i n g. data ; l e n g t h = s t r i n g. l e n g t h ; Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 8 of 47

12 10.2. Copy Constructor and Operators C++ generates a couple of default operations for any class/struct to allow you to use these datatypes as you use a built-in datatype. i n t a ( 1 0 ) ; Date moated ( anotherdate ) ; Sometimes, we have to rewrite these default operations. For the copy constructor, it is very simple. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 9 of 47

13 Copy Constructor for a String Class class S t r i n g { p r i v a t e : / / ain t a 0 terminated s t r i n g char data ; / / length of s t r i n g i n t l e n g t h ; S t r i n g ( ) ; S t r i n g ( const S t r i n g& s t r i n g ) ;... S t r i n g : : S t r i n g ( ) : data ( 0 ), l e n g t h ( 0 ) { S t r i n g : : S t r i n g ( const S t r i n g& s t r i n g ) : data ( 0 ), l e n g t h ( s t r i n g. l e n g t h ) { data = new... ; f o r (... ) {... Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 10 of 47

14 Excursus: C++ Strings Good news: You now know how to write a copy constructor for your personal string class. Bad news: Such a class does already exist. # i n c l u d e <s t r i n g > std : : s t r i n g mystring0 ; std : : s t r i n g mystring1 ( h e l l o world ) ; std : : s t r i n g mystring2 ( mystring1 ) ; std : : s t r i n g mystring3 = h e l l o MPG ; std : : s t r i n g mystring4 = mystring3 ; mystring. length ( ) ; mystring4 += mystring1 ; Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 11 of 47

15 Default Operations The following operations are generated automatically as public operations if we don t provide our own implementation: Default constructor (attributes contain garbage). Destructor (does nothing). Copy constructor (bit-wise copying). Assignment (bit-wise copying). However, there s still another pitfall/missing thing: MyString a ; / / do something with a ; MyString b ( a ) ; / / works MyString c ; i f ( c==a ) { / / does not work c o r r e c t l y std : : cout << c ; / / does not work at a l l Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 12 of 47

16 Operators In C++, operators such as +,-,+=,=,<< are just methods, too. We can redefine the operators. We now know, what std : : cout << s t u p i d << std : : endl means. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 13 of 47

17 Operators Redefined Part I class Vector { / / copy c o n s t r u c t o r Vector ( const Vector& vector ) ; Vector& operator =( const Vector& vector ) ; / / unary operators Vector& operator +=( const Vector& vector ) ; Vector& operator =( const double& value ) ; void tostream ( std : : ostream& out ) const ; / / binary operators Vector& operator +( const Vector& lhs, const Vector& rhs ) ; std : : ostream& operator <<(std : : ostream& out, const Vector& vector ) ;... Vector& operator +( const Vector& lhs, const Vector& rhs ) { Vector r e s u l t ( l h s ) ; r e s u l t += rhs ; r e t u r n r e s u l t ; Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 14 of 47

18 Operators Redefined Part II class Vector { / / copy c o n s t r u c t o r Vector ( const Vector& vector ) ; Vector& operator =( const Vector& vector ) ; / / unary operators Vector& operator +=( const Vector& vector ) ; Vector& operator =( const double& value ) ; void tostream ( std : : ostream& out ) const ; / / binary operators Vector& operator +( const Vector& lhs, const Vector& rhs ) ; std : : ostream& operator <<(std : : ostream& out, const Vector& vector ) ;... std : : ostream& operator <<(std : : ostream& out, const Vector& vector ) { vector. tostream ( out ) ; r e t u r n out ; Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 15 of 47

19 10.3. Inheritance A Case Study I Image you write a galaxy simulation program, i.e. Gadget-2 does not exist. You have Planets and Suns. Due to your brilliant C++ course, you decide to model both as classes. Both of them have a mass, a diameter, and the sun additionally has a temperature. The planet holds a boolean flag that indicates whether there s life on this planet. class Sun { p r i v a t e : double mass ; double diameter ; double temperature ; class Planet { p r i v a t e : double mass ; double diameter ; bool l i f e ; Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 16 of 47

20 A Case Study II class Sun { p r i v a t e : double mass, diameter ; double temperature ; class Planet { p r i v a t e : double mass, diameter ; bool l i f e ; Write a member function (method) getweight() const, and Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 17 of 47

21 A Case Study II class Sun { p r i v a t e : double mass, diameter ; double temperature ; class Planet { p r i v a t e : double mass, diameter ; bool l i f e ; Write a member function (method) getweight() const, and write a function double getforce(xxx, xxx) with either a sun or a planet as input parameters. Keep in mind that these parameters should be passed as const references and that you can use overloading. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 17 of 47

22 A Case Study II class Sun { p r i v a t e : double mass, diameter ; double temperature ; class Planet { p r i v a t e : double mass, diameter ; bool l i f e ; Write a member function (method) getweight() const, and write a function double getforce(xxx, xxx) with either a sun or a planet as input parameters. Keep in mind that these parameters should be passed as const references and that you can use overloading. Is it good code (code duplication)? Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 17 of 47

23 Case Study Rewritten The planet and the sun have something in common. They both are celestial bodies. Imagine there is something like a celestial body in C. A Planet then is an extension (specialisation) of CelestialBody. A S then is an extension (specialisation) of CelestialBody, too. Sun and Planet are two different things. Both have in common that they have a weight and a diameter. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 18 of 47

24 is a Relations CelestialBody Sun Planet Planet has all operations and attributes CelestialBody has. Sun has all operations and attributes CelestialBody has. Every operation expecting an instance of CelestialBody could also be passed an instance of Sun. Every operation expecting an instance of CelestialBody could also be passed an instance of Planet. Again, we could image this to be an extension of a cut-n-paste mechanism. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 19 of 47

25 is a Relations in Source Code (Part I) class CelestialBody { p r i v a t e : double mass, diameter ; double getmass ( ) const ; class Sun : public CelestialBody { p r i v a t e : double temperature ; class Planet : p u b l i c CelestialBody { p r i v a t e : bool l i f e ;... Planet earth ; earth. getmass ( ) ; / / t h a t s f i n e Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 20 of 47

26 is a Relations in Source Code (Part II) class CelestialBody { p r i v a t e : double mass, diameter ; double getmass ( ) const ; class Sun : public CelestialBody { p r i v a t e : double temperature ; void explode ( ) ; class Planet : p u b l i c CelestialBody { p r i v a t e : bool l i f e ; void foo ( const CelestialBody& a ) { a. getmass ( ) ; / / t h a t s f i n e a. explode ( ) ; / / arrgh Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 21 of 47

27 Object-oriented Programming is-a relations are mapped to inheritance. Object-based programming becomes object-oriented programming due to this inheritance concept. Whenever an object of type A is expected somewhere, we can also pass an object with a subtype of B. This concept is called polymorphism. There s a new visibility mode protected: These attributes and operations behave like private, i.e. they are not visible from outside, but they are visible within subclasses. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 22 of 47

28 Super Constructors class A { A( i n t v a r i a b l e ) ; class B : p u b l i c A { B ( ) ;... B : : B ( ) : A( 23 ) { This is the reason, C++ introduced initialisation lists, and treats both attributes and super types the same way. Please take care of the order: First, you have to call the supertype s constructor, then you have to initialise your attributes in the right order. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 23 of 47

29 Case Study Revisited Write Your First Inheritance class CelestialBody { p r i v a t e : double mass, diameter ; CelestialBody ( double mass ) ; double getmass ( ) const ; class Sun : public CelestialBody { p r i v a t e : double temperature ; Sun ( double mass ) ; class Planet : p u b l i c CelestialBody { p r i v a t e : bool l i f e ; Planet ( double mass ) ; double computeforce ( const CelestialBody& a, const CelestialBody& b ) { / / your job Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 24 of 47

30 Inheritance Summarised Inheritance represents is-a relations. It enables us to represent/model the real world more accurately than just plain structs and functions/methods. It helps us to avoid code duplication. We can (without overloading) write functions for a whole set of different structs. We can extend existing structs without toughing them due to an additional subclass. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 25 of 47

31 10.4. virtual Operations class A { void foo ( ) ; class B : p u b l i c A { void foo ( ) ; class C: p u b l i c B { void foo ( ) ; A object1 ; B object2 ; C object3 ; A object4 = new A ( ) ; A object5 = new B ( ) ; B object6 = new C ( ) ; object1. foo ( ) ; object2. foo ( ) ; object3. foo ( ) ; object4 >foo ( ) ; object5 >foo ( ) ; object6 >foo ( ) ; Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 26 of 47

32 Static Polymorphism class A { void foo ( ) ; class B : p u b l i c A { void foo ( ) ; A object4 = new A ( ) ; object4 >foo ( ) ; C++ implements a static polymorphism by default. It is called static, as the (known) object type determines which operation is to be invoked. There is a variant called dynamic polymorphism, too. Java, C# support only dynamic polymorphism. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 27 of 47

33 Dynamic Polymorphism class A { void foo ( ) ; v i r t u a l void bar ( ) ; class B : p u b l i c A { void foo ( ) ; v i r t u a l void bar ( ) ; A object1 = new A ( ) ; B object2 = new B ( ) ; A object3 = new B ( ) ; object1 >foo ( ) ; object2 >foo ( ) ; object3 >foo ( ) ; object1 >bar ( ) ; object2 >bar ( ) ; object3 >bar ( ) ; Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 28 of 47

34 Dynamic Destructors If you use inheritance, always make your destructor virtual. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 29 of 47

35 Abstract Operations class A { class B : p u b l i c A { class C: p u b l i c A { Let B and C be classes that belong together logically. An example: B writes measurements into a database, C plots data to Matlab. However, B and C have to implementation in common. They share solely the signature. Consequently, all operations (in A) have to be virtual. However, it does not make sense to provide any default implementation of an operation in A. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 30 of 47

36 Abstract Operations class A { v i r t u a l void foo ( ) = 0; class B : p u b l i c A { v i r t u a l void foo ( ) ; class C: p u b l i c A { v i r t u a l void foo ( ) ; virtual operations can be made abstract. A class with at least one abstract method is an abstract class. We cannot instantiate abstract types. This way, we enforce subclasses to implement them. Classes with solely abstract operations are called interface. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 31 of 47

37 Case Study Revisited Abstract Operations class CelestialBody {... v i r t u a l void plottocout ( ) const = 0; class Sun : public CelestialBody {... v i r t u a l void plottocout ( ) const ; class Planet : p u b l i c CelestialBody {... v i r t u a l void plottocout ( ) const ; CelestialBody a = new CelestialBody ( ) ; CelestialBody b = new Sun (... ) ; a >plottocout ( ) ; b >plottocout ( ) ; Implement plottocout() for both subclasses. Does the code snippet from above work? Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 32 of 47

38 10.5. static Variable Lifecycle void foo ( ) { i n t myint ;... void bar ( ) { s t a t i c i n t myint ;... Variables belong to a scope. Whenever we encounter a variable definition, we reserve memory. At the end of the scope, the variable is destroyed (invoke destructor, free memory). Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 33 of 47

39 Static Variables void foo ( ) { i n t myint ;... void bar ( ) { s t a t i c i n t myint ;... Variables belong to a scope. Whenever we encounter a static variable definition for the first time, we reserve memory. A static variable is freed when the application terminates (invoke destructor, free memory). Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 34 of 47

40 An Example for Static Variables void bar ( ) { s t a t i c i n t myint = 20; myint ++;... bar ( ) ; / / f i r s t c a l l of bar ( ) ever bar ( ) ; bar ( ) ; Static variables are bind to the application, not to the scope. However, they are still accessible only within the scope. Usually, (good?) procedural programmers do not use static. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 35 of 47

41 Object Variables class A { i n t f i e l d ; void foo ( ) ; void A : : foo ( ) { f i e l d ++; A instance1, instance2 ; instance1. foo ( ) ; instance2. foo ( ) ; Both instances work on different instantiations of field. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 36 of 47

42 Class Variables (Static) class A { s t a t i c i n t f i e l d ; void foo ( ) ; void A : : foo ( ) { f i e l d ++; A instance1, instance2 ; instance1. foo ( ) ; instance2. foo ( ) ; A : : f i e l d ++; Here, static binds the attribute to the class. It is a class attribute. Depending on the visibility, one can either access the static attribute within any operation of A, or even outside of the class (not recommended as it violates the encapsulation idea). If the field is public, the class acts (from a syntax point of view) similar to a namespace. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 37 of 47

43 Example: Instance Counter class A { p r i v a t e : s t a t i c i n t instances ; A ( ) ; void A : : A ( ) { instances ++; This code keeps track how many instances of A are created. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 38 of 47

44 Static Operations class A { void foo ( ) ; s t a t i c void bar ( ) ; void A : : foo ( ) {... void A : : bar ( ) {... We can also declare methods as static. Static methods (class methods) belong to the class, not to an object. Static methods may work on class attributes only. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 39 of 47

45 Case Study Revisited Static Operations class CelestialBody {... s t a t i c i n t getnumberofcallstogetmass ( ) ; double getmass ( ) const ; class Sun : public CelestialBody {... class Planet : p u b l i c CelestialBody {... CelestialBody a = new Planet ( ) ; CelestialBody b = new Sun (... ) ; std : : cout << t o t a l number of c a l l s to getmass ( ) : << CelestialBody : : getnumberofcallstogetmass ( ) << std : : endl ; Implement getnumberofcelestialbodiescreated() for CelestialBody. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 40 of 47

46 10.6. C++ vs. Java Multiple Inheritance class A { void foo ( ) ; class B { void bar ( ) ; class C: p u b l i c A, B { C myc; myc. foo ( ) ; / / works myc. bar ( ) ; / / works Multiple inheritance is supported by C++. Java does not have such a concept. Often, it is considered to be a bad smell, while inheriting from multiple interfaces is not a bad smell. However, it is sometimes useful; in particular if it reflects ideas of (AOP). Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 41 of 47

47 Private Inheritance class A { void foo ( ) ; class B : p r i v a t e A { void bar ( ) ; void B : : bar ( ) { foo ( ) ; / / works B myb; myb. bar ( ) ; / / works myb. foo ( ) ; / / doesn t work Private inheritance is supported by C++. Private downgrades the visibility of the super class. Here, the is-a relationship does not hold anymore, i.e. it is a pure implementation inheritance, not a behavioural. However, today it is considered to be a bad smell. Java only supports public inheritance. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 42 of 47

48 Explicit Call of Supertype class A { v i r t u a l void foo ( ) ; class B : p r i v a t e A { v i r t u a l void foo ( ) ; void A : : foo ( ) { / / do something void B : : foo ( ) { A : : foo ( ) ; B myb; myb. foo ( ) ; We still can access the supertype s implementation, but only inside the subclass not from the outside. This is sometimes useful if we extend a function, i.e. add behaviour to an already existing function (such as tracing). Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 43 of 47

49 Ambiguity I class B { i n t m y A t t r i b u t e ; void foo ( ) { / / works with m y A t t r i b u t e class C { i n t m y A t t r i b u t e ; void bar ( ) { / / works with m y A t t r i b u t e class D: p u b l i c B,C { void t a r ( ) { myattribute ++; / / doesn t work B : : myattribute ++; C : : myattribute ++; / / should work D myd; myd. foo ( ) ; myd. bar ( ) ; Class D holds two myattribute instances. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 44 of 47

50 Ambiguity III class A { i n t m y A t t r i b u t e ; class B : p u b l i c A { void foo ( ) { / / works with m y A t t r i b u t e class C: p u b l i c A { void bar ( ) { / / works with m y A t t r i b u t e class D: p u b l i c B,C { D myd; myd. foo ( ) ; myd. bar ( ) ; Question of the day: How many myattribute instances does an object of type D have? Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 45 of 47

51 Virtual Inheritance class A { i n t m y A t t r i b u t e ; class B : v i r t u a l p u b l i c A { void foo ( ) { / / works with m y A t t r i b u t e class C: v i r t u a l p u b l i c A { void bar ( ) { / / works with m y A t t r i b u t e class D: v i r t u a l p u b l i c B,C { D myd; myd. foo ( ) ; myd. bar ( ) ; There s also a concept called virtual inheritance, i.e. at runtime the system identifies which attributes do exist. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 46 of 47

52 Missing C++ Topics Generic programming Idioms of the standard library (iterators, e.g.) Standard library RTTI Exception handling (Design) patterns Outlook: Parallel programming And then there s due to the course open issues, questions, and feedback. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 47 of 47

8. Object-oriented Programming. June 15, 2010

8. Object-oriented Programming. June 15, 2010 June 15, 2010 Introduction to C/C++, Tobias Weinzierl page 1 of 41 Outline Recapitulation Copy Constructor & Operators Object-oriented Programming Dynamic and Static Polymorphism The Keyword Static The

More information

6. Pointers, Structs, and Arrays. 1. Juli 2011

6. Pointers, Structs, and Arrays. 1. Juli 2011 1. Juli 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 50 Outline Recapitulation Pointers Dynamic Memory Allocation Structs Arrays Bubble Sort Strings Einführung

More information

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

6. Pointers, Structs, and Arrays. March 14 & 15, 2011 March 14 & 15, 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 47 Outline Recapitulation Pointers Dynamic Memory Allocation Structs Arrays Bubble Sort Strings Einführung

More information

4. Functions. March 10, 2010

4. Functions. March 10, 2010 March 10, 2010 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 40 Outline Recapitulation Functions Part 1 What is a Procedure? Call-by-value and Call-by-reference Functions

More information

11. Generic Programming and Design Patterns. 8. Juli 2011

11. Generic Programming and Design Patterns. 8. Juli 2011 8. Juli 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 26 Outline Recapitulation Template Programming An Overview over the STL Design Patterns (skipped) Evaluation/feedback

More information

5. Applicative Programming. 1. Juli 2011

5. Applicative Programming. 1. Juli 2011 1. Juli 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 41 Outline Recapitulation Computer architecture extended: Registers and caches Header files Global variables

More information

Inheritance, and Polymorphism.

Inheritance, and Polymorphism. Inheritance and Polymorphism by Yukong Zhang Object-oriented programming languages are the most widely used modern programming languages. They model programming based on objects which are very close to

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

Lecture 15: Object-Oriented Programming

Lecture 15: Object-Oriented Programming Lecture 15: Object-Oriented Programming COMP 524 Programming Language Concepts Stephen Olivier March 23, 2009 Based on slides by A. Block, notes by N. Fisher, F. Hernandez-Campos, and D. Stotts Object

More information

Instantiation of Template class

Instantiation of Template class Class Templates Templates are like advanced macros. They are useful for building new classes that depend on already existing user defined classes or built-in types. Example: stack of int or stack of double

More information

Fast Introduction to Object Oriented Programming and C++

Fast Introduction to Object Oriented Programming and C++ Fast Introduction to Object Oriented Programming and C++ Daniel G. Aliaga Note: a compilation of slides from Jacques de Wet, Ohio State University, Chad Willwerth, and Daniel Aliaga. Outline Programming

More information

G52CPP C++ Programming Lecture 13

G52CPP C++ Programming Lecture 13 G52CPP C++ Programming Lecture 13 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last lecture Function pointers Arrays of function pointers Virtual and non-virtual functions vtable and

More information

CS105 C++ Lecture 7. More on Classes, Inheritance

CS105 C++ Lecture 7. More on Classes, Inheritance CS105 C++ Lecture 7 More on Classes, Inheritance " Operator Overloading Global vs Member Functions Difference: member functions already have this as an argument implicitly, global has to take another parameter.

More information

G52CPP C++ Programming Lecture 9

G52CPP C++ Programming Lecture 9 G52CPP C++ Programming Lecture 9 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last lecture const Constants, including pointers The C pre-processor And macros Compiling and linking And

More information

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

G52CPP C++ Programming Lecture 7. Dr Jason Atkin G52CPP C++ Programming Lecture 7 Dr Jason Atkin 1 This lecture classes (and C++ structs) Member functions inline functions 2 Last lecture: predict the sizes 3 #pragma pack(1) #include struct A

More information

Chapter 5 Object-Oriented Programming

Chapter 5 Object-Oriented Programming Chapter 5 Object-Oriented Programming Develop code that implements tight encapsulation, loose coupling, and high cohesion Develop code that demonstrates the use of polymorphism Develop code that declares

More information

Data Structures (list, dictionary, tuples, sets, strings)

Data Structures (list, dictionary, tuples, sets, strings) Data Structures (list, dictionary, tuples, sets, strings) Lists are enclosed in brackets: l = [1, 2, "a"] (access by index, is mutable sequence) Tuples are enclosed in parentheses: t = (1, 2, "a") (access

More information

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE Abstract Base Classes POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors class B { // base class virtual void m( ) =0; // pure virtual function class D1 : public

More information

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors CSC 330 OO Software Design 1 Abstract Base Classes class B { // base class virtual void m( ) =0; // pure virtual

More information

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe OBJECT ORIENTED PROGRAMMING USING C++ CSCI 5448- Object Oriented Analysis and Design By Manali Torpe Fundamentals of OOP Class Object Encapsulation Abstraction Inheritance Polymorphism Reusability C++

More information

CMSC 132: Object-Oriented Programming II

CMSC 132: Object-Oriented Programming II CMSC 132: Object-Oriented Programming II Java Support for OOP Department of Computer Science University of Maryland, College Park Object Oriented Programming (OOP) OO Principles Abstraction Encapsulation

More information

22. Subtyping, Inheritance and Polymorphism

22. Subtyping, Inheritance and Polymorphism 741 Last Week: ression Trees 742 22. Subtyping, Inheritance and Polymorphism ression Trees, Separation of Concerns and Modularisation, Type Hierarchies, Virtual Functions, Dynamic Binding, Code Reuse,

More information

And Even More and More C++ Fundamentals of Computer Science

And Even More and More C++ Fundamentals of Computer Science And Even More and More C++ Fundamentals of Computer Science Outline C++ Classes Special Members Friendship Classes are an expanded version of data structures (structs) Like structs, the hold data members

More information

An Introduction to C++

An Introduction to C++ An Introduction to C++ Introduction to C++ C++ classes C++ class details To create a complex type in C In the.h file Define structs to store data Declare function prototypes The.h file serves as the interface

More information

COMP 2355 Introduction to Systems Programming

COMP 2355 Introduction to Systems Programming COMP 2355 Introduction to Systems Programming Christian Grothoff christian@grothoff.org http://grothoff.org/christian/ 1 Today Class syntax, Constructors, Destructors Static methods Inheritance, Abstract

More information

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management Session 8 Memory Management The issues Dr Christos Kloukinas City, UoL http://staff.city.ac.uk/c.kloukinas/cpp (slides originally produced by Dr Ross Paterson) Programs manipulate data, which must be stored

More information

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am CMSC 202 Section 010x Spring 2007 Computer Science II Final Exam Name: Username: Score Max Section: (check one) 0101 - Justin Martineau, Tuesday 11:30am 0102 - Sandeep Balijepalli, Thursday 11:30am 0103

More information

PIC 10A Objects/Classes

PIC 10A Objects/Classes PIC 10A Objects/Classes Ernest Ryu UCLA Mathematics Last edited: November 13, 2017 User-defined types In C++, we can define our own custom types. Object is synonymous to variable, and class is synonymous

More information

Object-oriented Programming. Object-oriented Programming

Object-oriented Programming. Object-oriented Programming 2014-06-13 Object-oriented Programming Object-oriented Programming 2014-06-13 Object-oriented Programming 1 Object-oriented Languages object-based: language that supports objects class-based: language

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Increases Program Structure which results in greater reliability. Polymorphism

Increases Program Structure which results in greater reliability. Polymorphism UNIT 4 C++ Inheritance What is Inheritance? Inheritance is the process by which new classes called derived classes are created from existing classes called base classes. The derived classes have all the

More information

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value Paytm Programming Sample paper: 1) A copy constructor is called a. when an object is returned by value b. when an object is passed by value as an argument c. when compiler generates a temporary object

More information

From Java to C++ From Java to C++ CSE250 Lecture Notes Weeks 1 2, part of 3. Kenneth W. Regan University at Buffalo (SUNY) September 10, 2009

From Java to C++ From Java to C++ CSE250 Lecture Notes Weeks 1 2, part of 3. Kenneth W. Regan University at Buffalo (SUNY) September 10, 2009 From Java to C++ CSE250 Lecture Notes Weeks 1 2, part of 3 Kenneth W. Regan University at Buffalo (SUNY) September 10, 2009 C++ Values, References, and Pointers 1 C++ Values, References, and Pointers 2

More information

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com More C++ : Vectors, Classes, Inheritance, Templates with content from cplusplus.com, codeguru.com 2 Vectors vectors in C++ basically arrays with enhancements indexed similarly contiguous memory some changes

More information

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor Outline EDAF50 C++ Programming 4. Classes Sven Gestegård Robertz Computer Science, LTH 2018 1 Classes the pointer this const for objects and members Copying objects friend inline 4. Classes 2/1 User-dened

More information

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity. OOPS Viva Questions 1. What is OOPS? OOPS is abbreviated as Object Oriented Programming system in which programs are considered as a collection of objects. Each object is nothing but an instance of a class.

More information

Hierarchical inheritance: Contains one base class and multiple derived classes of the same base class.

Hierarchical inheritance: Contains one base class and multiple derived classes of the same base class. 1. What is C#? C# (pronounced "C sharp") is a simple, modern, object oriented, and type safe programming language. It will immediately be familiar to C and C++ programmers. C# combines the high productivity

More information

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University C++ Review CptS 223 Advanced Data Structures Larry Holder School of Electrical Engineering and Computer Science Washington State University 1 Purpose of Review Review some basic C++ Familiarize us with

More information

377 Student Guide to C++

377 Student Guide to C++ 377 Student Guide to C++ c Mark Corner January 21, 2004 1 Introduction In this course you will be using the C++ language to complete several programming assignments. Up to this point we have only provided

More information

2 ADT Programming User-defined abstract data types

2 ADT Programming User-defined abstract data types Preview 2 ADT Programming User-defined abstract data types user-defined data types in C++: classes constructors and destructors const accessor functions, and inline functions special initialization construct

More information

What are the characteristics of Object Oriented programming language?

What are the characteristics of Object Oriented programming language? What are the various elements of OOP? Following are the various elements of OOP:- Class:- A class is a collection of data and the various operations that can be performed on that data. Object- This is

More information

CLASSES AND OBJECTS IN JAVA

CLASSES AND OBJECTS IN JAVA Lesson 8 CLASSES AND OBJECTS IN JAVA (1) Which of the following defines attributes and methods? (a) Class (b) Object (c) Function (d) Variable (2) Which of the following keyword is used to declare Class

More information

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi Modern C++ for Computer Vision and Image Processing Igor Bogoslavskyi Outline Move semantics Classes Operator overloading Making your class copyable Making your class movable Rule of all or nothing Inheritance

More information

C++ Important Questions with Answers

C++ Important Questions with Answers 1. Name the operators that cannot be overloaded. sizeof,.,.*,.->, ::,? 2. What is inheritance? Inheritance is property such that a parent (or super) class passes the characteristics of itself to children

More information

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++ Session 6 - Inheritance in C++ The most important slide of the lecture Dr Christos Kloukinas City, UoL http://staff.city.ac.uk/c.kloukinas/cpp (slides originally produced by Dr Ross Paterson) Why use inheritance?

More information

Chapter 11 Object and Object- Relational Databases

Chapter 11 Object and Object- Relational Databases Chapter 11 Object and Object- Relational Databases Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11 Outline Overview of Object Database Concepts Object-Relational

More information

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017 Overview of OOP Dr. Zhang COSC 1436 Summer, 2017 7/18/2017 Review Data Structures (list, dictionary, tuples, sets, strings) Lists are enclosed in square brackets: l = [1, 2, "a"] (access by index, is mutable

More information

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture - 31 Static Members Welcome to Module 16 of Programming in C++.

More information

C++ (Non for C Programmer) (BT307) 40 Hours

C++ (Non for C Programmer) (BT307) 40 Hours C++ (Non for C Programmer) (BT307) 40 Hours Overview C++ is undoubtedly one of the most widely used programming language for implementing object-oriented systems. The C++ language is based on the popular

More information

COEN244: Class & function templates

COEN244: Class & function templates COEN244: Class & function templates Aishy Amer Electrical & Computer Engineering Templates Function Templates Class Templates Outline Templates and inheritance Introduction to C++ Standard Template Library

More information

04-24/26 Discussion Notes

04-24/26 Discussion Notes 04-24/26 Discussion Notes PIC 10B Spring 2018 1 When const references should be used and should not be used 1.1 Parameters to constructors We ve already seen code like the following 1 int add10 ( int x

More information

More C++ : Vectors, Classes, Inheritance, Templates

More C++ : Vectors, Classes, Inheritance, Templates Vectors More C++ : Vectors,, Inheritance, Templates vectors in C++ basically arrays with enhancements indexed similarly contiguous memory some changes defined differently can be resized without explicit

More information

CS-202 Introduction to Object Oriented Programming

CS-202 Introduction to Object Oriented Programming CS-202 Introduction to Object Oriented Programming California State University, Los Angeles Computer Science Department Lecture III Inheritance and Polymorphism Introduction to Inheritance Introduction

More information

Polymorphism Part 1 1

Polymorphism Part 1 1 Polymorphism Part 1 1 What is Polymorphism? Polymorphism refers to a programming language s ability to process objects differently depending on their data type or class. Number person real complex kid

More information

Lecture 10: building large projects, beginning C++, C++ and structs

Lecture 10: building large projects, beginning C++, C++ and structs CIS 330: / / / / (_) / / / / _/_/ / / / / / \/ / /_/ / `/ \/ / / / _/_// / / / / /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / / /_/ / / / / / \ /_/ /_/_/_/ _ \,_/_/ /_/\,_/ \ /_/ \ //_/ /_/ Lecture 10:

More information

Programming, numerics and optimization

Programming, numerics and optimization Programming, numerics and optimization Lecture A-4: Object-oriented programming Łukasz Jankowski ljank@ippt.pan.pl Institute of Fundamental Technological Research Room 4.32, Phone +22.8261281 ext. 428

More information

Operator overloading

Operator overloading 1 Introduction 2 The copy constructor 3 Operator Overloading 4 Eg 1: Adding two vectors 5 The -> operator 6 The this pointer 7 Overloading = 8 Unary operators 9 Overloading for the matrix class 10 The

More information

The pre-processor (cpp for C-Pre-Processor). Treats all # s. 2 The compiler itself (cc1) this one reads text without any #include s

The pre-processor (cpp for C-Pre-Processor). Treats all # s. 2 The compiler itself (cc1) this one reads text without any #include s Session 2 - Classes in C++ Dr Christos Kloukinas City, UoL http://staff.city.ac.uk/c.kloukinas/cpp (slides originally produced by Dr Ross Paterson) A C++ source file may contain: include directives #include

More information

Java Object Oriented Design. CSC207 Fall 2014

Java Object Oriented Design. CSC207 Fall 2014 Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code

More information

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language Categories of languages that support OOP: 1. OOP support is added to an existing language - C++ (also supports procedural and dataoriented programming) - Ada 95 (also supports procedural and dataoriented

More information

Object Oriented Programming: Inheritance Polymorphism

Object Oriented Programming: Inheritance Polymorphism Object Oriented Programming: Inheritance Polymorphism Shahram Rahatlou Computing Methods in Physics http://www.roma1.infn.it/people/rahatlou/cmp/ Anno Accademico 2018/19 Today s Lecture Introduction to

More information

CS 251 Intermediate Programming Inheritance

CS 251 Intermediate Programming Inheritance CS 251 Intermediate Programming Inheritance Brooke Chenoweth University of New Mexico Spring 2018 Inheritance We don t inherit the earth from our parents, We only borrow it from our children. What is inheritance?

More information

Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor Programming C++ Lecture 3 Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor Jennifer.sartor@elis.ugent.be S Inheritance S Software reuse inherit a class s data and behaviors and enhance with new capabilities.

More information

Comp-304 : Object-Oriented Design What does it mean to be Object Oriented?

Comp-304 : Object-Oriented Design What does it mean to be Object Oriented? Comp-304 : Object-Oriented Design What does it mean to be Object Oriented? What does it mean to be OO? What are the characteristics of Object Oriented programs (later: OO design)? What does Object Oriented

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 12 Thomas Wies New York University Review Last lecture Modules Outline Classes Encapsulation and Inheritance Initialization and Finalization Dynamic

More information

Lecture 14: more class, C++ streams

Lecture 14: more class, C++ streams CIS 330: / / / / (_) / / / / _/_/ / / / / / \/ / /_/ / `/ \/ / / / _/_// / / / / /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / / /_/ / / / / / \ /_/ /_/_/_/ _ \,_/_/ /_/\,_/ \ /_/ \ //_/ /_/ Lecture 14:

More information

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Object-Oriented Programming (OOP) Fundamental Principles of OOP Object-Oriented Programming (OOP) O b j e c t O r i e n t e d P r o g r a m m i n g 1 Object-oriented programming is the successor of procedural programming. The problem with procedural programming is

More information

CE221 Programming in C++ Part 1 Introduction

CE221 Programming in C++ Part 1 Introduction CE221 Programming in C++ Part 1 Introduction 06/10/2017 CE221 Part 1 1 Module Schedule There are two lectures (Monday 13.00-13.50 and Tuesday 11.00-11.50) each week in the autumn term, and a 2-hour lab

More information

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington CSE 333 Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington Administrivia New exercise posted yesterday afternoon, due Monday morning - Read a directory

More information

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples: Unit IV Pointers and Polymorphism in C++ Concepts of Pointer: A pointer is a variable that holds a memory address of another variable where a value lives. A pointer is declared using the * operator before

More information

Programming Languages: OO Paradigm, Objects

Programming Languages: OO Paradigm, Objects Programming Languages: OO Paradigm, Objects Onur Tolga Şehitoğlu Computer Engineering,METU 15 April 2008 Outline 1 Object Oriented Programming 2 Constructors/Destructors Constructors Heap Objects Destructors

More information

COP 3330 Final Exam Review

COP 3330 Final Exam Review COP 3330 Final Exam Review I. The Basics (Chapters 2, 5, 6) a. comments b. identifiers, reserved words c. white space d. compilers vs. interpreters e. syntax, semantics f. errors i. syntax ii. run-time

More information

CMSC 132: Object-Oriented Programming II

CMSC 132: Object-Oriented Programming II CMSC 132: Object-Oriented Programming II Java Support for OOP Department of Computer Science University of Maryland, College Park Object Oriented Programming (OOP) OO Principles Abstraction Encapsulation

More information

Extending Classes (contd.) (Chapter 15) Questions:

Extending Classes (contd.) (Chapter 15) Questions: Extending Classes (contd.) (Chapter 15) Questions: 1 Virtual Functions in C++ Employee /\ / \ ---- Manager 2 Case 1: class Employee { string firstname, lastname; //... Employee( string fnam, string lnam

More information

OBJECT ORIENTED PROGRAMMING USING C++

OBJECT ORIENTED PROGRAMMING USING C++ OBJECT ORIENTED PROGRAMMING USING C++ Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both operators and functions can be overloaded

More information

Lecture 2, September 4

Lecture 2, September 4 Lecture 2, September 4 Intro to C/C++ Instructor: Prashant Shenoy, TA: Shashi Singh 1 Introduction C++ is an object-oriented language and is one of the most frequently used languages for development due

More information

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

the gamedesigninitiative at cornell university Lecture 7 C++ Overview Lecture 7 Lecture 7 So You Think You Know C++ Most of you are experienced Java programmers Both in 2110 and several upper-level courses If you saw C++, was likely in a systems course Java was based on

More information

Object-Oriented Programming

Object-Oriented Programming - oriented - iuliana@cs.ubbcluj.ro Babes-Bolyai University 2018 1 / 56 Overview - oriented 1 2 -oriented 3 4 5 6 7 8 Static and friend elements 9 Summary 2 / 56 I - oriented was initially created by Bjarne

More information

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine Homework 6 Yuji Shimojo CMSC 330 Instructor: Prof. Reginald Y. Haseltine July 21, 2013 Question 1 What is the output of the following C++ program? #include #include using namespace

More information

Inheritance, Polymorphism and the Object Memory Model

Inheritance, Polymorphism and the Object Memory Model Inheritance, Polymorphism and the Object Memory Model 1 how objects are stored in memory at runtime? compiler - operations such as access to a member of an object are compiled runtime - implementation

More information

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++ CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 19 Introduction to C++ C++ C++ is an enormous language: All of C Classes and objects (kind of like Java, some crucial differences) Many

More information

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4 CS32 - Week 4 Umut Oztok Jul 15, 2016 Inheritance Process of deriving a new class using another class as a base. Base/Parent/Super Class Derived/Child/Sub Class Inheritance class Animal{ Animal(); ~Animal();

More information

Polymorphism. Zimmer CSCI 330

Polymorphism. Zimmer CSCI 330 Polymorphism Polymorphism - is the property of OOP that allows the run-time binding of a function's name to the code that implements the function. (Run-time binding to the starting address of the code.)

More information

AN OVERVIEW OF C++ 1

AN OVERVIEW OF C++ 1 AN OVERVIEW OF C++ 1 OBJECTIVES Introduction What is object-oriented programming? Two versions of C++ C++ console I/O C++ comments Classes: A first look Some differences between C and C++ Introducing function

More information

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe Chapter 12 Outline Overview of Object Database Concepts Object-Relational Features Object Database Extensions to SQL ODMG Object Model and the Object Definition Language ODL Object Database Conceptual

More information

EMBEDDED SYSTEMS PROGRAMMING OO Basics

EMBEDDED SYSTEMS PROGRAMMING OO Basics EMBEDDED SYSTEMS PROGRAMMING 2014-15 OO Basics CLASS, METHOD, OBJECT... Class: abstract description of a concept Object: concrete realization of a concept. An object is an instance of a class Members Method:

More information

CS250 Final Review Questions

CS250 Final Review Questions CS250 Final Review Questions The following is a list of review questions that you can use to study for the final. I would first make sure you review all previous exams and make sure you fully understand

More information

C++ Constructor Insanity

C++ Constructor Insanity C++ Constructor Insanity CSE 333 Spring 2018 Instructor: Justin Hsia Teaching Assistants: Danny Allen Dennis Shao Eddie Huang Kevin Bi Jack Xu Matthew Neldam Michael Poulain Renshu Gu Robby Marver Waylon

More information

Chapter 2: Java OOP I

Chapter 2: Java OOP I Chapter 2: Java OOP I Yang Wang wyang AT njnet.edu.cn Outline OO Concepts Class and Objects Package Field Method Construct and Initialization Access Control OO Concepts Object Oriented Methods Object An

More information

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010 CSE 374 Programming Concepts & Tools Hal Perkins Spring 2010 Lecture 19 Introduction ti to C++ C++ C++ is an enormous language: g All of C Classes and objects (kind of like Java, some crucial differences)

More information

EMBEDDED SYSTEMS PROGRAMMING More About Languages

EMBEDDED SYSTEMS PROGRAMMING More About Languages EMBEDDED SYSTEMS PROGRAMMING 2015-16 More About Languages JAVA: ANNOTATIONS (1/2) Structured comments to source code (=metadata). They provide data about the code, but they are not part of the code itself

More information

a data type is Types

a data type is Types Pointers Class 2 a data type is Types Types a data type is a set of values a set of operations defined on those values in C++ (and most languages) there are two flavors of types primitive or fundamental

More information

Interview Questions of C++

Interview Questions of C++ Interview Questions of C++ Q-1 What is the full form of OOPS? Ans: Object Oriented Programming System. Q-2 What is a class? Ans: Class is a blue print which reflects the entities attributes and actions.

More information

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year Object Oriented Programming Assistant Lecture Omar Al Khayat 2 nd Year Syllabus Overview of C++ Program Principles of object oriented programming including classes Introduction to Object-Oriented Paradigm:Structures

More information

G52CPP C++ Programming Lecture 20

G52CPP C++ Programming Lecture 20 G52CPP C++ Programming Lecture 20 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Wrapping up Slicing Problem Smart pointers More C++ things Exams 2 The slicing problem 3 Objects are not

More information

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck C++ Crash Kurs Polymorphism Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck http://www.itm.uni-luebeck.de/people/pfisterer C++ Polymorphism Major abstractions of C++ Data abstraction

More information

Classes and Objects. Class scope: - private members are only accessible by the class methods.

Classes and Objects. Class scope: - private members are only accessible by the class methods. Class Declaration Classes and Objects class class-tag //data members & function members ; Information hiding in C++: Private Used to hide class member data and methods (implementation details). Public

More information

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011 More C++ David Chisnall March 17, 2011 Exceptions A more fashionable goto Provides a second way of sending an error condition up the stack until it can be handled Lets intervening stack frames ignore errors

More information

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a. Intro to OOP - Object and class - The sequence to define and use a class in a program - How/when to use scope resolution operator - How/when to the dot operator - Should be able to write the prototype

More information