CS 294-73 (CCN 27241) Software Engineering for Scientific Computing Lecture 5: C++ Key Concepts.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 5 – Abstract Data Types.
C++ Programming Languages
Abstract Data Type Fraction Example
Inheritance Inheritance Reserved word protected Reserved word super
Classes & Objects Computer Science I Last updated 9/30/10.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
Software Testing and Quality Assurance
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Introduction to CS Agenda Syllabus Schedule Lecture: the management of complexity.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
OOP Languages: Java vs C++
CS 1031 C++: Object-Oriented Programming Classes and Objects Template classes Operator Overloading Inheritance Polymorphism.
Programming Languages and Paradigms Object-Oriented Programming.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Chapter 11 and 12: Abstract Data Types and Encapsulation Constructs; Support for Object Orientation Lesson 11.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Week 14 - Wednesday.  What did we talk about last time?  More C++  new  delete  Differences for structs  OOP.
Object Oriented Programming (OOP) Lecture No. 10.
38 4/11/98 CSE 143 Modules [Chapter 2]. 39 4/11/98 What is a Module?  Collection of related items packaged together  Examples:  Stereo System Components.
Introduction to Programming
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Advanced C++ Topics Chapter 8. CS 308 2Chapter 8 -- Advanced C++ Topics This chapter describes techniques that make collections of reusable software components.
CS 132 Spring 2008 Chapter 2 Object-Oriented Design (OOD) and C++ Ideas: * Inheritance (and protected members of a class) ** Operator overloading Pointer.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
OO in Context Lecture 13: Dolores Zage. Confused about OO Not alone, there is much confusion about OO many programs are claimed to be OO but are not really.
Classes, Interfaces and Packages
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance.
ENEE150 – 0102 ANDREW GOFFIN Abstract Data Types.
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
CPS120: Introduction to Computer Science Lecture 16A Object-Oriented Concepts.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Chapter 11: Abstract Data Types Lecture # 17. Chapter 11 Topics The Concept of Abstraction Advantages of Abstract Data Types Design Issues for Abstract.
Regarding assignment 1 Style standards Program organization
OOP-4-Templates, ListType
Review: Two Programming Paradigms
C++ Classes & Object Oriented Programming
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
C++ Classes C++ Interlude 1.
Overview of C++ Overloading
COP 3330 Object-oriented Programming in C++
Java Programming Language
CPS120: Introduction to Computer Science
Lecture 8 Object Oriented Programming (OOP)
Presentation transcript:

CS (CCN 27241) Software Engineering for Scientific Computing Lecture 5: C++ Key Concepts

08/25/2011CS – Lecture 8 Objects in C++ -int a, b, c; -FooBar tr; -FooBar& m = tr; -FooBar* p = &tr; -FooBar v[10]; -m = v[5]; -p=v; -m=p[3]; -p++; p++; // OK -m++; //uh, would this work ? see later for how this might work -m=p[3]; // what element of v is this pointing to now ? -FooBar* v2 = new FooBar[5]; -FooBar* v3 = malloc(5*sizeof(FooBar)); // how are v3 and v2 different? 2

08/25/2011CS – Lecture 8 Functions in C++ Declaration (also called a prototype) double extractNorm(int a_el, double[3]& a_vec, MaxFactor a_ele); -We are telling the compiler: -There will be a function with the name extractNorm which –takes three arguments: an int, a reference to a trilplet of double precision numbers, and an object of type MaxFactor. –on completion will return a double precision floating point number. Declarations go into a header file. -Header files will have the file extension.h or.H 3

08/25/2011CS – Lecture 8 Source file littleFile.cpp double extractNorm(int a_el, double a_vec[3], MaxFactor a_ele); bool rightQuadrant(double a_vex[3], MaxFactor a_blink) { if(extractNorm(1,vex, blink)>0) return true; return false; } >g++ -c littleFile.cpp littleFile.cpp:3: error: ‘MaxFactor’ has not been declared littleFile.cpp:4: error: ‘MaxFactor’ has not been declared 4

08/25/2011CS – Lecture 8 littleFile.cpp but a bit better enum MaxFactor {LEFT, RIGHT, STRAIGHT}; int HowAboutAnIntRightHere=5; double extractNorm(int a, double vec[3], MaxFactor ele); bool rightQuadrant(double vex[3], MaxFactor blink) { if(extractNorm(1,vex, blink)>0) return true; return false; } >g++ -c littleFile.cpp ; ls littleFile.o So, what is in this file we just made ? 5

08/25/2011CS – Lecture 8 Let’s look at an object file >file littleFile.o; nm littleFile.o littleFile.o: Mach-O 64-bit object x86_ s EH_frame D _HowAboutAnIntRightHere U__Z11extractNormiPd9MaxFactor T__Z13rightQuadrantPd9MaxFactor S__Z13rightQuadrantPd9MaxFactor.eh D: data segment U: undefined T text segment S: symbol So, what is different about extractNorm and rightQuadrant ? 6

08/25/2011CS – Lecture 8 Trivial example class Counter { private: int m_MyCounter ; int m_zeroEncounters; public: //null constructor Counter():m_MyCounter(0),m_zeroCrossings(0) {} void incrementCounter() { if(m_MyCounter==-1) m_zeroEncounters++; m_MyCounter++; } void decrementCounter() { if(m_MyCounter==1) m_zeroEncounters++; m_MyCounter--; } virtual int getCounterValue() { return m_MyCounter; } const }; The object has it’s data m_MyCounter m_zeroEncounters. -Mostly data members are made private. Can you guess why ? This class has a public interface, what a user of Counter would want to be able to do with this object. Is This useful yet ? 7

08/25/2011CS – Lecture 8 Object-Oriented Programming Abstractions in programming are often described in terms of data, and functions that operate on that data. So, put them together in the language -Several languages have adopted this paradigm -C++, Java, Python, F90, C#, Modula-2 -Some had it from the start, some found a way to make it work -You can do OOP in any language, just some make it easier The main benefits that makes bundling the two ideas together desirable. -Encapsulation -Modularity -Inheritance Primary object in C++ is the class 8

08/25/2011CS – Lecture 8 Encapsulation Hiding the internals of the object protects its integrity by preventing users from setting the internal data of the component into an invalid or inconsistent state. -In Counter, how might a public data access result in an inconsistent state ? A benefit of encapsulation is that it can reduce system complexity, and thus increases robustness, by allowing the developer to limit the interdependencies between software components.robustness -The code presents a contract for the programmer -If private data has gotten messed up, you don’t have to look over your whole code base to determine where the mistake occurred. -The compiler has enforced this contract for you. -Except when you use ‘friend’ declarations. -Sleeping with friends is sleeping with every one of their friends. 9

08/25/2011CS – Lecture 8 Modularity Separation of Concerns -There is a small number of people that need to know how LevelFluxRegister is implemented. -There are a dozen people that need to know to use this class -There are several dozen people that just need to know this class is doing it’s job. Improve maintainability by enforcing logical boundaries between components.maintainability Modules are typically incorporated into the program through interfaces.interfaces -C++ makes this explicit with the public interface 10

08/25/2011CS – Lecture 8 Inheritance “is a” One of two primary methods of abstraction in C++ (the other being templates) ‘is-a’ vs ‘has-a’ -inheriting from an object is telling a person that this object ‘is-a’ another object -‘has-a’ is done in C++ by giving a class a member data member -numerous failings in class design come from confusing these ideas. -Imagine thinking of Counter as an ‘is-a’ relationship…. Good rule of thumb: -Does EVERY member function of the base class make sense for the derived class ? How about an example 11

08/25/2011CS – Lecture 8 Simple Inheritance class MonitoredCounter : public Counter { private: int m_accessCount; public: MonitoredCounter():m_accessCount(0) {} virtual int getCounterValue() { m_accessCount++; return Counter::getCounterValue(); } virtual int accessCount(){return m_accessCount;} }; What compiler errors will occur if I try to compile this.cpp file ? Have we satisfied our rule of thumb for an ‘is-a’ relationship? 12

08/25/2011CS – Lecture 8 Simple Inheritance with fixes! #ifndef MONITOREDCOUNTER_H #define MONITOREDCOUNTER_H #include “Counter.H” #include “NamespaceHeader.H” class MonitoredCounter : public Counter { private: mutable int m_accessCount; public: MonitoredCounter():m_accessCount(0) {} virtual int getCounterValue() { m_accessCount++; return Counter::getCounterValue();} const virtual int accessCount(){return m_accessCount;} const }; #include “NamespaceFooter.H #endif 13

08/25/2011CS – Lecture 8 Constructors are special What happens when I declare a new MonitoredCounter object ? #include “MonitoredCounter.H” int main(int argc, char* argv[]) { MonitoredCounter c1, c2[2]; int checkValue = c1.getCounterValue(); checkValue = c2[0].getCounterValue(); int howBig = sizeof(MonitoredCounter); // care to guess? return checkValue; } Compiler will build a.o file -make room on the stack for one int and one char* object -make room on the stack, using the D declaration, for three MonitoredCounter objects and two int object. -Call the inheritance tree of constructor objects -call Counter’s null constructor, then MonitoredCounter’s null constructor 14