Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS 294-73 (CCN 27241) Software Engineering for Scientific Computing Lecture 5: C++ Key Concepts."— Presentation transcript:

1 CS 294-73 (CCN 27241) Software Engineering for Scientific Computing http://www.cs.berkeley.edu/~colella/CS294 Lecture 5: C++ Key Concepts

2 08/25/2011CS294-73 – 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

3 08/25/2011CS294-73 – 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

4 08/25/2011CS294-73 – 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

5 08/25/2011CS294-73 – 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

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

7 08/25/2011CS294-73 – 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

8 08/25/2011CS294-73 – 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

9 08/25/2011CS294-73 – 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

10 08/25/2011CS294-73 – 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

11 08/25/2011CS294-73 – 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

12 08/25/2011CS294-73 – 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

13 08/25/2011CS294-73 – 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

14 08/25/2011CS294-73 – 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


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

Similar presentations


Ads by Google