1 / 19

CSE459.22 Programming in C++

CSE459.22 Programming in C++. Bin Ren Email: ren@cse.ohio-state.edu Office: DL 778. Course Information. Time: Th 8:30 – 9:18am Location: DL 264 Office hours: Tu & Th 9:30~11:30 am, DL 778 Webpage: http://www.cse.ohio-state.edu/~ren/45922/. What is Programming.

Download Presentation

CSE459.22 Programming in 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. CSE459.22 Programming in C++ Bin Ren Email: ren@cse.ohio-state.edu Office: DL 778

  2. Course Information Time: Th 8:30 – 9:18am Location: DL 264 Office hours: Tu & Th 9:30~11:30 am, DL 778 Webpage: http://www.cse.ohio-state.edu/~ren/45922/

  3. What is Programming Programming is taking A problem Find the area of a rectangle A set of data length width A set of functions area = length * width Then, Applying functions to data to solve the problem

  4. Programming Concept Evolution Unstructured Example: BASIC (early version), machine-level code… Procedural Example: C, Fortran, BASIC, Pascal… Object-Oriented Example: C++, Java, Smalltalk, Ruby, Python…

  5. Procedural Programming The main program coordinates calls to procedures and hands over appropriate data as parameters

  6. Procedural Languages – cont’d Procedural Languages Facilities to Pass arguments to functions Return values from functions Example Problem: to calculate the area of a rectangle Function int compute_area (int l, int w){ return ( l * w ); }

  7. Object-Oriented Concept • Objects of the program interact by sending messages to each other

  8. Objects An object is an encapsulation of both functions and data Objects are an Abstraction represent real world entities Classes are data types that define shared common properties or attributes Objects are instances of a class Objects have State have a value at a particular time Objects have Operations associated set of operations called methods that describe how to carry out operations Objects have Messages request an object to carry out one of its operations by sending it a message messages are the means by which we exchange data between objects

  9. Object-oriented Perspective Problem: to calculate the area of a rectangle Define a new type Rectangle (a class) Data width, length Function area() Create an instance of the class (an object) Request the object to return its area

  10. C++ Code for the Rectangle Example class Rectangle { private: int width, length; public: Rectangle (int w, int l) { width = w; length = l; } int compute_area() { return width*length; } } main() { Rectangle rect1(3,5); int x; x = rect1.compute_area(); cout<<x<<endl; }

  11. Characteristics of OOP Language Encapsulation Data structure: represents the properties, the states, or characteristics of objects Actions: permissible behaviors that are controlled through the member functions Inheritance Permit objects of a more specific class to inherit the properties (data) and behaviors (functions) of a more general/base class Be able to define a hierarchical relationship between objects Polymorphism Be able to interpret functions differently for different objects

  12. class Circle { private: int radius public: Circle(int r); // The area of a circle int compute_area(); }; class Triangle { private: int edgea, edgeb, edgec; public: Triangle (int a, int b, int c); // The area of a triangle int compute_area(); }; Encapsulation

  13. Abstract Types Decouple the interface from the representation virtual means “may be redefined later in a class derived from this one” “= 0” means pure virtual class Shape { public: Shape(); // Calculate the area for // this shape virtual int compute_area() = 0; };

  14. class Circle : public Shape { private: int radius; public: Circle (int r); int compute_area(); }; class Rectangle : public Shape { private: int length, width; public: Rectangle (int l, int w); int compute_area(); }; int sum_area(Shape s1, Shape s2) { return s1.compute_area() + s2.compute_area(); // Example of polymorphism } Inheritance and Polymorphism

  15. Basic C++ Inherit all ANSI C directives Inherit all C functions You don’t have to write OOP in C++. But C++ is a good language to do OOP

  16. Basic C++ Extension from C Comments // It's best to use this style for 1 line or partial lines /* And use this style when your comment consists of multiple lines */ cin and cout (#include <iostream.h>) cout << "hey"; char name[10]; cin >> name; cout << "Hey " << name << ", nice name." << endl; cout << endl; // print a blank line Declare variables almost anywhere // declare a variable when you need it for (int k = 1; k < 5; k++){ cout << k; }

  17. Basic C++ Extension from C – cont’d const In C, #define statements are handled by the preprocessor, so there is no type checking. In C++, the const specifier is interpreted by the compiler, and type checking is applied. New data type Reference data type “&”. Much likes pointer int ix; /* ix is "real" variable */ int & rx = ix; /* rx is "alias" for ix */ ix = 1; /* also rx == 1 */ rx = 2; /* also ix == 2 */

  18. C++ - Advance Extension C++ allow function overloading In C++, functions can use the same names, within the same scope, if each can be distinguished by its name and signature The signature specifies the number, type, and order of the parameters The name plus signature, then, uniquely identifies a function

  19. Take Home Message There are many different kinds of programming paradigms, OOP is one among them. In OOP, programmers see the execution of the program as a collection of dialoging objects. The main characteristics of OOPL include encapsulation, inheritance, and polymorphism. Not only OOPL can do OOP, but also others.

More Related