CS3340 – OOP and C++ L. Grewe.

Slides:



Advertisements
Similar presentations
C++ Language Fundamentals. 2 Contents 1. Introduction to C++ 2. Basic syntax rules 3. Declaring and using variables.
Advertisements

Inheritance and Composition (aka containment) Textbook Chapter –
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
Introduction to Data Structures, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Object Oriented Programming Concepts.
C++ fundamentals.
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
Getting Started with C++ Yingcai Xiao 09/03/08. Outline What (is C++) Who (created C++) How (to write a C++ program)
Object Oriented Programming using C++. Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms.
C++ C++ Overview (I) What is Object Orientated Programming? Approach: Break problem into subgroups of related parts that take into account code and data;
Stream Handling Streams - means flow of data to and from program variables. - We declare the variables in our C++ for holding data temporarily in the memory.
Chapter 11 and 12: Abstract Data Types and Encapsulation Constructs; Support for Object Orientation Lesson 11.
Object-Oriented Programming with C++ Yingcai Xiao.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 24P. 1Winter Quarter C++ Lecture 24.
File I/O ifstreams and ofstreams Sections 11.1 &
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
Fundamentals of C++ Yingcai Xiao 09/03/08. Outline Class Definition IO Template vector C Pointer Dynamic Memory Allocation.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Lecture 19 CIS 208 Wednesday, April 06, Welcome to C++ Basic program style and I/O Class Creation Templates.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
CS116 SENEM KUMOVA METİN. Outline What is C++ ? Some features of C++ Input and Output Operations Manipulators C++ header files Namespaces and scope resolution.
Computer Science Department Inheritance & Polymorphism.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
Hank Childs, University of Oregon
Motivation for Generic Programming in C++
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Introduction to C++ (Extensions to C)
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Ch 10- Advanced Object-Oriented Programming Features
7. Inheritance and Polymorphism
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Objectives Identify the built-in data types in C++
Polymorphism.
Inheritance & Polymorphism
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Basic Input and Output Operations
Programming Language Concepts (CIS 635)
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Friday, January 26, 2018 Announcements… For Today… For Next Time…
Parallel Arrays Parallel array =>Two or more arrays with the same number of elements used for storing related information about a collection of data. Example:
(5 - 1) Object-Oriented Programming (OOP) and C++
OOP’S Concepts in C#.Net
3.3 Abstract Classes, Assignment, and Casting in a Hierarchy
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Programming with Data Files
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
(5 - 1) Object-Oriented Programming (OOP) and C++
CS150 Introduction to Computer Science 1
File I/O in C++ I.
CS150 Introduction to Computer Science 1
From C to C++: Summary of weeks 1 - 4
Session 2: Introduction to Object Oriented Programming
Lecture 8-2 : STL Iterators and Algorithms
CHAPTER 4 File Processing.
Final and Abstract Classes
Topics OOP Review Inheritance Review Abstract Classes
File I/O in C++ I.
CECS 130 Final Exam Review Session
Lecture 6: Polymorphism
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

CS3340 – OOP and C++ L. Grewe

C++ 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/

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

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.

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: … };

inheritance (is-a) “a passenger car is a car” class CCar { “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: … };

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(); … };

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.

Polymorphism One method behaves differently for different objects. 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)}; };

Polymorphism One function behaves differently for different objects. 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;

Interface in C++ ……Use Abstract Class 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.

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.)

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. };

Common OOP need - Iterators Collections, Iterators --- and templates traverse a 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();

Coding with an IDE (MS Visual Studio 2XXX) 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)

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

Console IO console input an object of iostreams 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 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

File IO #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 }

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.

vector (smart array) 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];

Address of a Variables & 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

Pointers in C and C++ 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; Example: int * ip //ip (4 bytes of memory for a 32bit OS) 0xffaabbcc int i = 8; ip = &i; i 8