8. Object-oriented Programming. June 15, 2010
|
|
- Gwendolyn Shaw
- 6 years ago
- Views:
Transcription
1 June 15, 2010 Introduction to C/C++, Tobias Weinzierl page 1 of 41
2 Outline Recapitulation Copy Constructor & Operators Object-oriented Programming Dynamic and Static Polymorphism The Keyword Static The Singleton Pattern Generic Programming Introduction to C/C++, Tobias Weinzierl page 2 of 41
3 8.1. 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 ; s } Introduction to C/C++, Tobias Weinzierl page 3 of 41
4 8.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 mydate ( anotherdate ) ; Sometimes, we have to rewrite these default operations. For the copy constructor, it is very simple. Introduction to C/C++, Tobias Weinzierl page 4 of 41
5 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 ( ) { } } Introduction to C/C++, Tobias Weinzierl page 5 of 41
6 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 ; Introduction to C/C++, Tobias Weinzierl page 6 of 41
7 Default Operations The following operations are generated automatically as public operations if we don t provide our own implementation: Default constructor. Destructor. Copy constructor. However, there s still another pitfall: MyString a ; / / do something with a ; MyString b ( a ) ; / / works MyString c ; c = a ; / / does not work Introduction to C/C++, Tobias Weinzierl page 7 of 41
8 Operators In C++, operators such as +,-,+=,=, are just methods, too. The = operators (assignment operator) is generates automatically, if we don t overwrite it. It then is a bit-wise copy of the object. We can redefine the operators. We now know, what std : : cout << s t u p i d << std : : endl means. Introduction to C/C++, Tobias Weinzierl page 8 of 41
9 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 ; } Introduction to C/C++, Tobias Weinzierl page 9 of 41
10 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 ; } Introduction to C/C++, Tobias Weinzierl page 10 of 41
11 8.3. Inheritance Sometimes, someone hands you over an object, but this object lacks some functionality. Now, it is up to you to add this functionality. However, what happens if there is different variants of this functionality? Image you write a researcher management application. Let Employee be a multi-purpose class for bookkeeping. Task: Add a function setsalary() and gettaxespaid. However: There are also people getting a stipend. These guys do not pay taxes. Writing two classes is not a variant, as all employees have to have a getweight() operation. Challenge: If we duplicate the code, then multiple operations have to be rewritten: bool isbmiabovethreshold ( ) { } Introduction to C/C++, Tobias Weinzierl page 11 of 41
12 Challenge Rewritten A Stipend is an extension of Employee. A PostDoc is an extension of Employee, too. Stipdend and PostDoc are two different things. Whatever we do with an Employee, we can do with a Stipend and PostDoc, too. Introduction to C/C++, Tobias Weinzierl page 12 of 41
13 is a Relations getentry() AbstractDB RealDatabase HardcodedTestDB Student has all operations and attributes, Employee has. Every operation expecting an instance of Employee could also be passed an instance of Student. Again, we could image this to be an extension of a cut-n-paste mechanism. Introduction to C/C++, Tobias Weinzierl page 13 of 41
14 is a Relations in Source Code (Part I) class Employee { p r i v a t e : double weight ; ; bool isbmiabovethreshold ( ) ; class Stipend : p u b l i c Employee { p r i v a t e : double getstipdend ( ) const ; Stipend mystipend ; mystipend. isbmiabovethreshold ( ) ; / / t h a t s f i n e Introduction to C/C++, Tobias Weinzierl page 14 of 41
15 is a Relations in Source Code (Part II) class Employee { class Stipend : p u b l i c Employee { void foo ( const Employee& employee ) { } Stipend mystipend ; foo ( mystipend ) ; Introduction to C/C++, Tobias Weinzierl page 15 of 41
16 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. Introduction to C/C++, Tobias Weinzierl page 16 of 41
17 8.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 ( ) ; Introduction to C/C++, Tobias Weinzierl page 17 of 41
18 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. Introduction to C/C++, Tobias Weinzierl page 18 of 41
19 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 ( ) ; Introduction to C/C++, Tobias Weinzierl page 19 of 41
20 Dynamic Destructors If you use inheritance, always make your destructor virtual. Introduction to C/C++, Tobias Weinzierl page 20 of 41
21 Finally: Super Constructor 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. Introduction to C/C++, Tobias Weinzierl page 21 of 41
22 Excursus: Multiple Inheritance class A { class B { class C: p u b l i c A, B { Multiple inheritance is supported by C++. However, today it is considered to be a bad smell. There s also a concept called virtual inheritance which is also considered to be a bad smell nowadays. Introduction to C/C++, Tobias Weinzierl page 22 of 41
23 Excursus: Private Inheritance class A { class B { class C: p r i v a t e A,B { Private inheritance is supported by C++. 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. I nevertheless find is useful. Introduction to C/C++, Tobias Weinzierl page 23 of 41
24 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. Introduction to C/C++, Tobias Weinzierl page 24 of 41
25 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 ( ) ; vitual 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. Introduction to C/C++, Tobias Weinzierl page 25 of 41
26 8.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). Introduction to C/C++, Tobias Weinzierl page 26 of 41
27 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). Introduction to C/C++, Tobias Weinzierl page 27 of 41
28 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. Usuall, (good?) procedural programmers do not use static. Introduction to C/C++, Tobias Weinzierl page 28 of 41
29 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. Introduction to C/C++, Tobias Weinzierl page 29 of 41
30 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. Introduction to C/C++, Tobias Weinzierl page 30 of 41
31 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. Introduction to C/C++, Tobias Weinzierl page 31 of 41
32 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. Introduction to C/C++, Tobias Weinzierl page 32 of 41
33 A Factory Method class A { p r i v a t e : i n t a t t r i b u t e ; s t a t i c A createanafromterminal ( ) ; s t a t i c A createanafromterminal ( ) { A r e s u l t ; std : : c i n >> r e s u l t. a t t r i b u t e ; r e t u r n A ; } A mya; mya = A : : createanafromterminal ( ) ; Static methods may manipulate any attribute of the class as they are part of the class. Static methods are called similar to methods wrapped by a namespace. Static methods typically represent operations on sets while methods represent operations on an element of a set. Introduction to C/C++, Tobias Weinzierl page 33 of 41
34 8.6. Design Patterns Design Patterns: Stem from reocurring solutions for certain technical challenges. More complex than idioms. Something different than an algorithm: It describes the technical realisation. Famous book by the gang of four. Standard book by the gang of four. Introduction to C/C++, Tobias Weinzierl page 34 of 41
35 A Singleton / Implements a connection to a database ( big f i l e ) w ith l o t s of m a t e r i a l constants. / class MaterialConstants { We want to ensure that MaterialConstants is created only once throughout the application lifecycle. Here are some hints: MaterialConstants should not have a public (default) constructor. As anyone could need the database, a static getdatabase() operation might be helpful that returns an instance of MaterialConstants. This static operation might hold an instance of MaterialConstants. However, it should not create it each time it is invoked. Introduction to C/C++, Tobias Weinzierl page 35 of 41
36 Singleton Implementation - Part I class MaterialConstants { p r i v a t e : / noone should be able i n s t a n t i a t e the database on h i s own / MaterialConstants ( ) ; / noone should be able to copy a database / MaterialConstants ( const MaterialConstants & ) ; / / noone should be able i n t getentry ( ) const ; void addentry ( ) ; MaterialConstants mydatabase ; / / forbidden / / however, we need something l i k e mydatabase. getentry ( ) ; We could make all the operations static. However, then we don t have control of the open process. Even worse, this won t fit to our fancy new object-oriented paradigm. Introduction to C/C++, Tobias Weinzierl page 36 of 41
37 Singleton Implementation - Part II class MaterialConstants { p r i v a t e : / noone should be able i n s t a n t i a t e the database on h i s own / MaterialConstants ( ) ; / noone should be able to copy a database / MaterialConstants ( const MaterialConstants & ) ; / / noone should be able / Provide access to one s i n g l e database ( s i n g l e t o n ) / s t a t i c MaterialConstants& getdatabase ( ) ; MaterialConstants : : getdatabase ( ). getentry ( ) ; / / f i n e MaterialConstants myconstants = MaterialConstants : : getdatabase ( ) ; / / forbidden Introduction to C/C++, Tobias Weinzierl page 37 of 41
38 Singleton Implementation - Part III class MaterialConstants { p r i v a t e : / Provide access to one s i n g l e database ( s i n g l e t o n ) / s t a t i c MaterialConstants& getdatabase ( ) ; MaterialConstants& MaterialConstants : : getdatabase ( ) { MaterialConstants r e s u l t ; r e t u r n r e s u l t ; } This simple realisation of the static operation works. However, it creates a segmentation fault. Introduction to C/C++, Tobias Weinzierl page 38 of 41
39 Singleton Implementation - Part III class MaterialConstants { p r i v a t e : / Provide access to one s i n g l e database ( s i n g l e t o n ) / s t a t i c MaterialConstants& getdatabase ( ) ; MaterialConstants& MaterialConstants : : getdatabase ( ) { MaterialConstants r e s u l t ; r e t u r n r e s u l t ; } This simple realisation of the static operation works. However, it creates a segmentation fault. If we removed the reference symbol, it would create one instance and one copy per call. That is not what we want. Introduction to C/C++, Tobias Weinzierl page 38 of 41
40 Singleton Implementation - Part IV class MaterialConstants { p r i v a t e : / Provide access to one s i n g l e database ( s i n g l e t o n ) / s t a t i c MaterialConstants& getdatabase ( ) ; MaterialConstants& MaterialConstants : : getdatabase ( ) { s t a t i c MaterialConstants r e s u l t ; r e t u r n r e s u l t ; } Introduction to C/C++, Tobias Weinzierl page 39 of 41
41 A Factory Pattern Study our simple MaterialConstants type. This shall be part of a project where we are promised to get access to the real database by the end of the project. Meanwhile, we have to work with a small number of artificial material constants. Could be switch from one database to another without changing anything in the code using the material database? Introduction to C/C++, Tobias Weinzierl page 40 of 41
42 A Factory Pattern getentry() AbstractDB RealDatabase HardcodedTestDB We define an abstract Database type. It is an interface (see UML diagram). This type is delivered by our singleton, i.e. codes work only with the interface. We implement the interface with a stupid class where we hardcode some material constants (basically a big case statement). Within the static operation, we then decide whether we deliver a real database or our artificial one. Introduction to C/C++, Tobias Weinzierl page 41 of 41
10. Object-oriented Programming. 7. Juli 2011
7. Juli 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 47 Outline Object Case Study Brain Teaser Copy Constructor & Operators Object-oriented Programming, i.e.
More information6. 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 information6. 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 informationInheritance, 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 information5. 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 information4. 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 information11. 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 informationObject-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 informationProgramming 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 informationInstantiation 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 informationObject 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 informationOBJECT 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 informationProgramming 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 informationOutline. 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 informationCS105 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 informationPolymorphism 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 informationG52CPP 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 informationExtending 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 informationPOLYMORPHISM 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 informationPOLYMORPHISM 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 informationObject 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 informationLecture 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 informationC++ 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 informationWhat 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 informationThe 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 informationG52CPP 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 informationFast 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 informationI BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++
No. of Printed Pages : 3 I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination 05723. June, 2015 BCS-031 : PROGRAMMING IN C ++ Time : 3 hours Maximum Marks : 100 (Weightage 75%)
More informationCMSC 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 informationCMSC 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 informationCPSC 427: Object-Oriented Programming
CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 7 September 21, 2016 CPSC 427, Lecture 7 1/21 Brackets Example (continued) Storage Management CPSC 427, Lecture 7 2/21 Brackets Example
More informationC++ Inheritance and Encapsulation
C++ Inheritance and Encapsulation Private and Protected members Inheritance Type Public Inheritance Private Inheritance Protected Inheritance Special method inheritance 1 Private Members Private members
More informationEL2310 Scientific Programming
Lecture 15: Inheritance and Polymorphism, STL (pronobis@kth.se) Overview Overview Lecture 15: Inheritance and Polymorphism, STL Wrap Up Additional Bits about Classes Overloading Inheritance Polymorphism
More informationCS304 Object Oriented Programming Final Term
1. Which of the following is the way to extract common behaviour and attributes from the given classes and make a separate class of those common behaviours and attributes? Generalization (pg 29) Sub-typing
More informationCS6301 PROGRAMMING AND DATA STRUCTURES II QUESTION BANK UNIT-I 2-marks ) Give some characteristics of procedure-oriented language. Emphasis is on doing things (algorithms). Larger programs are divided
More informationAnd 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 informationOOPS 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 informationCOMP 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 informationC++ 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 informationG52CPP 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 informationWhat is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1
Polymorphism Part 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 adult
More informationChapter 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 informationPaytm 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 informationCE221 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 informationPart VII. Object-Oriented Programming. Philip Blakely (LSC) C++ Introduction 194 / 370
Part VII Object-Oriented Programming Philip Blakely (LSC) C++ Introduction 194 / 370 OOP Outline 24 Object-Oriented Programming 25 Member functions 26 Constructors 27 Destructors 28 More constructors Philip
More information2 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 informationPROGRAMMING IN C++ COURSE CONTENT
PROGRAMMING IN C++ 1 COURSE CONTENT UNIT I PRINCIPLES OF OBJECT ORIENTED PROGRAMMING 2 1.1 Procedure oriented Programming 1.2 Object oriented programming paradigm 1.3 Basic concepts of Object Oriented
More informationLecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?
CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 5: Inheritance & Polymorphism Lecture Contents 2 What is Inheritance? Super-class & sub class Protected members Creating subclasses
More informationCritique this Code. Hint: It will crash badly! (Next slide please ) 2011 Fawzi Emad, Computer Science Department, UMCP
Critique this Code string const & findsmallest(vector const &v) string smallest = v[0]; for (unsigned int i = 0; i < v.size(); i++) if (v[i] < smallest) smallest = v[i]; return smallest; Hint:
More informationINHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U
C A N A D I A N I N T E R N A T I O N A L S C H O O L O F H O N G K O N G INHERITANCE & POLYMORPHISM P2 LESSON 12 P2 LESSON 12.1 INTRODUCTION inheritance: OOP allows a programmer to define new classes
More informationData 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 informationComputer Science II CSci 1200 Lecture 18 Operators and Friends
Review from Lecture 17 Arrays and pointers Computer Science II CSci 1200 Lecture 18 Operators and Friends Different types of memory Dynamic allocation of arrays Today s Lecture Operators and Friends Chapter
More informationInheritance and Interfaces
Inheritance and Interfaces Object Orientated Programming in Java Benjamin Kenwright Outline Review What is Inheritance? Why we need Inheritance? Syntax, Formatting,.. What is an Interface? Today s Practical
More informationAbstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles
Abstract Data Types (ADTs) CS 247: Software Engineering Principles ADT Design An abstract data type (ADT) is a user-defined type that bundles together: the range of values that variables of that type can
More informationAn Introduction to Patterns
An Introduction to Patterns Robert B. France Colorado State University Robert B. France 1 What is a Pattern? - 1 Work on software development patterns stemmed from work on patterns from building architecture
More informationCOM S 213 PRELIM EXAMINATION #2 April 26, 2001
COM S 213 PRELIM EXAMINATION #2 April 26, 2001 Name: Student ID: Please answer all questions in the space(s) provided. Each question is worth 4 points. You may leave when you are finished with the exam.
More informationCS313D: ADVANCED PROGRAMMING LANGUAGE
CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 5: Inheritance & Polymorphism Lecture Contents 2 What is Inheritance? Super-class & sub class Protected members Creating subclasses
More informationInheritance and Polymorphism in Java
Inheritance and Polymorphism in Java Introduction In this article from my free Java 8 course, I will be discussing inheritance in Java. Similar to interfaces, inheritance allows a programmer to handle
More informationObject-Oriented Programming for Scientific Computing
Object-Oriented Programming for Scientific Computing Dynamic Memory Management Ole Klein Interdisciplinary Center for Scientific Computing Heidelberg University ole.klein@iwr.uni-heidelberg.de 2. Mai 2017
More information22. 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 informationCS304 Object Oriented Programming
1 CS304 Object Oriented Programming 1. Which of the following is the way to extract common behaviour and attributes from the given classes and make a separate class of those common behaviours and attributes?
More informationCopyright 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 informationCS 247: Software Engineering Principles. ADT Design
CS 247: Software Engineering Principles ADT Design Readings: Eckel, Vol. 1 Ch. 7 Function Overloading & Default Arguments Ch. 12 Operator Overloading U Waterloo CS247 (Spring 2017) p.1/17 Abstract Data
More informationAIMS Embedded Systems Programming MT 2017
AIMS Embedded Systems Programming MT 2017 Object-Oriented Programming with C++ Daniel Kroening University of Oxford, Computer Science Department Version 1.0, 2014 Outline Classes and Objects Constructors
More informationCLASSES 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 informationC++ (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 informationCPSC 427: Object-Oriented Programming
CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 22 November 28, 2016 CPSC 427, Lecture 22 1/43 Exceptions (continued) Code Reuse Linear Containers Ordered Containers Multiple Inheritance
More informationOBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak
OBJECT ORIENTED PROGRAMMING Ms. Ajeta Nandal C.R.Polytechnic,Rohtak OBJECT ORIENTED PARADIGM Object 2 Object 1 Data Data Function Function Object 3 Data Function 2 WHAT IS A MODEL? A model is an abstraction
More informationCSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN
CSSE 220 Day 15 Inheritance Check out DiscountSubclasses from SVN Discount Subclasses Work in pairs First look at my solution and understand how it works Then draw a UML diagram of it DiscountSubclasses
More informationShort 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 informationECE 461 Fall 2011, Final Exam
ECE 461 Fall 2011, Final Exam DO NOT START WORKING ON THIS UNTIL TOLD TO DO SO. LEAVE IT ON THE DESK. You have until 9:00PM to take this exam. Your exam should have 17 pages total (including this cover
More informationCS32 - 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 informationCOP 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 informationInheritance and Polymorphism
Division of Mathematics and Computer Science Maryville College Outline Inheritance 1 Inheritance 2 3 Outline Inheritance 1 Inheritance 2 3 The "is-a" Relationship The "is-a" Relationship Object classification
More informationCS93SI Handout 04 Spring 2006 Apr Review Answers
CS93SI Handout 04 Spring 2006 Apr 6 2006 Review Answers I promised answers to these questions, so here they are. I'll probably readdress the most important of these over and over again, so it's not terribly
More informationTackling Design Patterns Chapter 3: Template Method design pattern and Public Inheritance. 3.1 Introduction... 2
Department of Computer Science Tackling Design Patterns Chapter 3: Template Method design pattern and Public Inheritance Copyright c 2016 by Linda Marshall and Vreda Pieterse. All rights reserved. Contents
More informationCSE 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 informationCSE 303: Concepts and Tools for Software Development
CSE 303: Concepts and Tools for Software Development Hal Perkins Autumn 2008 Lecture 24 Introduction to C++ CSE303 Autumn 2008, Lecture 24 1 C++ C++ is an enormous language: All of C Classes and objects
More informationPIC 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 informationMore on Inheritance. Interfaces & Abstract Classes
More on Inheritance Interfaces & Abstract Classes Java interfaces A Java interface is used to specify minimal functionality that a client requires of a server. A Java interface contains: method specifications
More informationCS201 - 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 informationThe 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 informationCS313D: ADVANCED PROGRAMMING LANGUAGE
CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 4(b): Inheritance & Polymorphism Lecture Contents What is Inheritance? Super-class & sub class The object class Using extends keyword
More informationMARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012
MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012 Instructor: K. S. Booth Time: 70 minutes (one hour ten minutes)
More information4.1 Introduction Programming preliminaries Constructors Destructors An example... 3
Department of Computer Science Tackling Design Patterns Chapter 4: Factory Method design pattern Copyright c 2016 by Linda Marshall and Vreda Pieterse. All rights reserved. Contents 4.1 Introduction.................................
More informationG52CPP 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 informationRules and syntax for inheritance. The boring stuff
Rules and syntax for inheritance The boring stuff The compiler adds a call to super() Unless you explicitly call the constructor of the superclass, using super(), the compiler will add such a call for
More informationLecture 14: more class, C++ streams
CIS 330: / / / / (_) / / / / _/_/ / / / / / \/ / /_/ / `/ \/ / / / _/_// / / / / /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / / /_/ / / / / / \ /_/ /_/_/_/ _ \,_/_/ /_/\,_/ \ /_/ \ //_/ /_/ Lecture 14:
More informationInheritance and Polymorphism
Division of Mathematics and Computer Science Maryville College Outline Inheritance 1 Inheritance 2 3 Outline Inheritance 1 Inheritance 2 3 The "is-a" Relationship Object classification is typically hierarchical.
More informationJava: introduction to object-oriented features
Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: introduction to object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer
More informationCSE 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 informationProgramming, 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 informationWritten by John Bell for CS 342, Spring 2018
Advanced OO Concepts Written by John Bell for CS 342, Spring 2018 Based on chapter 3 of The Object-Oriented Thought Process by Matt Weisfeld, with additional material from other sources. Constructors Constructors
More informationInheritance and Polymorphism
Object Oriented Programming Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. Al-Azhar University Website: eaymanelshenawy.wordpress.com Email : eaymanelshenawy@azhar.edu.eg
More informationData Structures using OOP C++ Lecture 3
References: th 1. E Balagurusamy, Object Oriented Programming with C++, 4 edition, McGraw-Hill 2008. 2. Robert L. Kruse and Alexander J. Ryba, Data Structures and Program Design in C++, Prentice-Hall 2000.
More informationObject-Oriented Concepts and Design Principles
Object-Oriented Concepts and Design Principles Signature Specifying an object operation or method involves declaring its name, the objects it takes as parameters and its return value. Known as an operation
More informationOverloaded Operators, Functions, and Students
, Functions, and Students Division of Mathematics and Computer Science Maryville College Outline Overloading Symbols 1 Overloading Symbols 2 3 Symbol Overloading Overloading Symbols A symbol is overloaded
More informationNote 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...
CISC 2000 Computer Science II Fall, 2014 Note 12/1/2014 1 Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance... (a) What s the purpose of inheritance?
More information2.1 Introduction UML Preliminaries Class diagrams Modelling delegation... 4
Department of Computer Science COS121 Lecture Notes Chapter 2- Memento design pattern Copyright c 2015 by Linda Marshall and Vreda Pieterse. All rights reserved. Contents 2.1 Introduction.................................
More informationMARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012
MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012 Instructor: K. S. Booth Time: 70 minutes (one hour ten minutes)
More information