1 / 22

CS3340 – OOP and C++

CS3340 – OOP and C++. L. Grewe. C++ is an object-oriented programming language. Who created C++? Bjarne Stroustrup, while at AT&T. (now, a professor in Computer Science at Texas A&M University) http://www.research.att.com/~bs/. C++. Overview. C++ basics (see course website)

Télécharger la présentation

CS3340 – OOP and C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS3340 – OOP and C++ L. Grewe

  2. C++ is an object-oriented programming language. • Who created C++? • Bjarne Stroustrup, while at AT&T. • (now, a professor in Computer Science at Texas A&M University) • http://www.research.att.com/~bs/ C++

  3. Overview C++ basics (see course website) C++ and OOP concepts revisited

  4. Code Reuse Two ways of code reuse: composition & inheritance Composition (“has-a” relationship): An object of a class has an object of another class in it. Use predefined code of a class by creating an object of the class. Inheritance (“is-a” relationship): A child class inherits everything from its parent class. Use predefined code of a class by sub-classing from it.

  5. composition(has-a) class CWheel { private: float air-pressure; public: void inflate(); … }; // predefined class class CPassengerCar { private: int number-of-seats; CWheel fl-wheel, fr-wheel; CWheel rl-wheel, rr-wheel; public: … };

  6. inheritance (is-a) “a passenger car is a car” “a truck is a car” “a race car is a car” class CCar { private: CEngine engine; CBreak break; public: void accelerate(); void stop(); … }; // predefined class class CPassengerCar : public CCar { private: int number-of-seats; CWheel fl-wheel, fr-wheel; CWheel rl-wheel, rr-wheel; public: … };

  7. Encapsulation Grouping data and operations (functions/methods) together to facilitate code reuse. Controlling the access of the members. (Private members are hidden and can only be accessed by the member functions of the class. Users can only use the given public functions to alter the private data members). class CCar { private: CEngine engine; CBreak break; public: void accelerate(); void stop(); … };

  8. Encapsulation class CCar { protected: CEngine engine; CBreak break; public: void accelerate(); void stop(); … }; Protected members are hidden and can only be accessed by the member functions of the class and its decedent classes.

  9. One method behaves differently for different objects. Polymorphism class CShape { private: int color; public:virtual float area() { }; // a virtual function can be overridden by the children }; class CCircle : public CShape{ public: float area() {return (pi * r * r)}; }; class CRect : public CShape{ public: float area() {return (w*h)}; };

  10. One function behaves differently for different objects. Polymorphism CShape *shapes[4]; CCircle *c1 = new CCircle(5); CCircle *c2 = new CCircle(10); CRect *r1 = new CRect(4,5); CRect *r2 = new CRect(8,5); shapes[0] = (CShape *) c1; // cast to parent allowed shapes[1] = (CShape *) c2; // cast to parent allowed shapes[2] = (CShape *) r1; // cast to parent allowed shapes[3] = (CShape *) r2; // cast to parent allowed for (I=0; I<4; I++) cout << shapes[I]->area() << endl;

  11. Polymorphism…. interfaces • There is no “Interface” in C++. • The workaround is to define abstract classes with pure virtual functions. • Pure virtual functions: have only function headers (APIs), but no function bodies. • virtual return-type function-name (argument list) = 0; • Abstract classes: have one or more pure virtual functions; can be used for inheritance by child classes, can not be used for instantiation of objects. The child classes are required to implement all pure virtual functions with predefined APIs. Interface in C++ ……Use Abstract Class

  12. Polymorphism Setting up standard interfaces Polymorphism makes it possible for objects of different child classes to have the same interface. To enforce a standard interface, one can make the parent class an abstract class and subclass from it. An abstract class is a class containing a pure virtual function. One can’t instantiated an abstract class. It is just for setting up standard interfaces. Its child classes must override and implement the pure virtual function. (A regular virtual function is not required to be overridden by the child classes and it is inherited if it is not overridden.)

  13. Polymorphism class CShape { public: virtual float area() = 0; // a pure virtual function }; // CShape a-shape; not allowed. class CCircle : public CShape{ public: float area(); // must be implemented. }; class CRect : public CShape{ public: float area(); // must be implemented. };

  14. Collections, Iterators --- and templates • traversea collection of objects from the beginning to the end. • iterators have been implemented for all C++ collection classes (e.g. vector). • Have templates (like generics in Java) • vector<Shape *> shapes; • … • vector<Shape *>::iterator it = shapes.begin(); •     while(it != shapes.end()) (*it++)->display(); Common OOP need - Iterators

  15. The actual sequence will depend on your version of MS Visual Studio Using Start->Program Files-> MS Visual Studio 2XXX-> MS Visual Studio XXXX Select C++ as default environment if prompted to. File->New->Project-> Other Languages->Visual C++->Win32->Win32 Console Application Name: test Location: My Documents\Visual Studio 2XXX\Projects (fix this to a location…like your USB fob that you will remember to take with you) Coding with an IDE (MS Visual Studio 2XXX)

  16. Some Review and other things I/O , file I/O Templates Vector Protection types pointers

  17. Reading Input: cin • console input • an object of iostreams • only supports one operator >> • reads input for its arguments, one at a time • auto converts input to the type of the arguments • Displaying Output: cout • console output • an object of iostreams • only supports one operator << • sends output to the console for its arguments, one at a time • auto converts output of the type of the arguments for display • display format can be specified Console IO

  18. #include <string> #include <fstream> // file io streams #include <iostream> // file io streams using namespace std; int main() { string infile, outfile; cout << "Enter input file name: \n"; cin >> infile; cout << "Enter output file name: \n"; cin >> outfile; ifstream in(infile.data()); // Open for reading ofstream out(outfile.data()); // Open for writing string s; while(getline(in, s)) // Discards newline char out << s << "\n"; // ... must add it back } File IO

  19. Protection Type public, private and protected public members are accessible to all. protected members are accessible to derived class. private accessible only to same class.

  20. To use: #include <vector> To define: vector <type> v; To add an element: v.push_back(object); To access an element: v[i] (just like an array) Eaxmples: vector<int> vi; vector<string> vs; int i; string s; cin >> i >> s; vi.push_back(i); vs.push_back(s); cout << vi[0] << vs[0]; vector (smart array)

  21. & • Operator & returns the address of an variable • e.g.: &i, &ia[0], &ia[1] • The address of a memory is expressed as a hex number • e.g.: 0xffaabbcc Address of a Variables

  22. Pointer • A pointer is a named memory space. • The size of the memory is determined by the OS (32 bits, 64 bits, …), and it determines the maximum addressing space of the OS. • The memory space of a pointer stores the address of another memory space. We usually say, the pointer points to the memory space. • The size of the pointed memory space is determined by the type of the pointer. • A pointer is declared as • type * name; Pointers in C and C++ Example: int * ip //ip(4 bytes of memory for a 32bit OS) 0xffaabbcc int i = 8; ip = &i; i 8

More Related