Looking toward C++ Object-Oriented Programming Traditional Programming Procedure-Oriented Programming Task-Based Programming circle method draw data C.draw()

Slides:



Advertisements
Similar presentations
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Reusable Classes.  Motivation: Write less code!
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
1 Introduction to C++ Programming Concept Basic C++ C++ Extension from C.
Reconfigurable Computing S. Reda, Brown University Reconfigurable Computing (EN2911X, Fall07) Lecture 13: SystemC (1/3) Prof. Sherief Reda Division of.
C++ fundamentals.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
Programming Languages and Paradigms Object-Oriented Programming.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
C++ Programming. Table of Contents History What is C++? Development of C++ Standardized C++ What are the features of C++? What is Object Orientation?
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
C++ Classes CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011
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.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
C++ Review (3) Structs, Classes, Data Abstraction.
C++ language first designed or implemented In 1980 by Bjarne Stroustrup, from Bell labs. that would receive formally this name at the end of 1983.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
Prepared by: Elsy Torres Shajida Berry Siobhan Westby.
Design.ppt1 Top-down designs: 1. Define the Problem IPO 2. Identify tasks, Modularize 3. Use structure chart 4. Pseudocode for Mainline 5. Construct pseudocode.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
Chapter 10 Inheritance and Polymorphism
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Solution to Midterm Exam
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
1 OOP - An Introduction ISQS 6337 John R. Durrett.
Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.
CSC 143F 1 CSC 143 Constructors Revisited. CSC 143F 2 Constructors In C++, the constructor is a special function automatically called when a class instance.
Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits.
Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-1 Concordia University Department of Computer Science and Software Engineering COMP345.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
1 CSE Programming in C++. 2 Overview Sign roster list Syllabus and Course Policies Introduction to C++ About Lab 1 Fill Questionnaire.
From C to C++. What is C++ C++ is the work of Bjarne Stroustrup of AT&T Bell Labs. C++ is a mostly upward compatible extension of C that provides: A better.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
CSIS 123A Lecture 10 Inheritance. Organizing Code Why should programs be organized? –Humans are "complexity challenged" Need simplicity, clarity, efficiency.
1 Introduction to Object Oriented Programming Chapter 10.
Classes Sujana Jyothi C++ Workshop Day 2. A class in C++ is an encapsulation of data members and functions that manipulate the data. A class is a mechanism.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
1 n Object Oriented Programming. 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions.
A First Book of C++ Chapter 12 Extending Your Classes.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Seventh step for Learning C++ Programming CLASS Declaration Public & Private Constructor & Destructor This pointer Inheritance.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Introduction to Programming
Advanced Program Design with C++
Review: Two Programming Paradigms
Chapter 5 Classes.
Implementing Classes Chapter 3.
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Introduction to Classes and Objects
CIS 199 Final Review.
Lecture 8 Object Oriented Programming (OOP)
Computer Science II for Majors
Presentation transcript:

Looking toward C++ Object-Oriented Programming Traditional Programming Procedure-Oriented Programming Task-Based Programming circle method draw data C.draw() draw(CIR)

Object Part of a large program is viewed as an object Contains data and functions that operate on these data

Task(s) based programming Problem solving problem specification algorithm design functions coding testing program Top-down design Problem specific Easy programming for new programmers Reusable ? Large system implementation?

Object-oriented design  objects Contains data as well as functions  classes Abstract characterization of a set of objects  inheritance New class from old class  polymorphism Identical named methods for different functions

A Brief History of C++ In 60s, Language B was developed by Ken Thompson as a systems programming language 1971, at Bell Labs extended the B language into what he called NB, for "New B“  C In 1983, with various versions of C floating around the computer world, ANSI established a committee that eventually published a standard for C in In 1983 Bjarne Stroustrup at Bell Labs created C++. C++ was designed for the UNIX system environment, Bjarne Stroustrup The C++ Programming Language is basically an extension of the C Programming Language.

void draw(parameter) { if parameter == circle draw circle if parameter == line draw line … } Example of draw a pattern Top-down design … draw(circle); … OO design Objects: circle, line, … circle.draw( ) line.draw( ) Objects contains: Data (describe the objects) Operations(functions )

function object draw circle line

Class – abstract characterization of a set of objects class string { char data[80]; public: void store (char *); int length( ); }; Data (variable) Methods (operations) Information hiding … data is not available to public Public interface … store and length publicly available names of methods and variables of a class are local to the class members

class class_name { permission_label_1: member1; permission_label_2: member2;... } object_name; private members of a class are accessible only from other members of its same class or from its "friend" classes. protected members are accessible, in addition to from members of the same class and friend classes, also from members of its derived classes. public members are accessible from anywhere where the class is visible. Permission:

class string { char data[80]; public: void store (char *); int length( ); }; string s,t; /*defines 2 objects s and t belonging to the class string. Storage is allocated */ s.store( “Work hard”); /*stores these chars in the object s */ len=s.length(); /* stores the length of s in the variable len */ store(...) and length() are functions defined later

Class declaration does not create any objects/allocate storage string string1,string2; Accessing data member and method similar to structure string1.x  access the data member x string1.f (arguments)  access the method f ptr->x ptr pointer to string1 ptr->f (arguments) ptr pointer to string1 string1.store(“hello”); len=string1.length( ); Particular object of a class is referred as an instance

Inheritance: new class inherits variables and methods from existing class Base class/superclass Derived class/subclass inherits all data and methods from base class may add new member and methods

class pen { int x; int y; int status; public: void set_status(int); void set_loca(int,int); }; pen p; /*p is an object of class pen */ p.set_loca(x,y) /*positions p at (x,y) coordinates */ p.set_status (1) /*turns the pen on */ p.set_status (0) /*turns the pen off */ Functions defined later

// Inherits class pen and add new features class color_pen : public pen { int color; public: void set_color(int); }; color_pen class inherits all data and methods from class pen adds data member color and method set_color public pen statement makes public members in pen public

X y status set_status set_loca pen Base class X y status set_status set_loca color set_color Derived class color_pen

class derived_class_name: public base_class_name; class ctriangle: public cpolygon { public: int area( ); }

// derived classes # include main () { CRectangle rect; CTriangle trgl; rect.set_values (4,5); class CPolygon trgl.set_values (4,5); {int width, height; cout << rect.area() << endl; public: cout << trgl.area() << endl; void set_values (int a, int b) return 0; } { width=a; height=b;} }; class CRectangle: public CPolygon { public: int area (void) { return (width * height); } }; class CTriangle: public CPolygon { public: int area (void) { return (width * height / 2); } }; cout c++ print endl end line

Multiple inheritance class DrawableString: public Point, public String { Inherits data and methods from multiple base class

// multiple inheritance #include class CPolygon { int width, height; public: void set_values (int a, int b) { width=a; height=b;} }; class COutput { public: void output (int i); }; void COutput::output (int i) { cout << i << endl; } class CRectangle: public CPolygon, public Coutput { public: int area (void) { return (width * height); } }; class CTriangle: public CPolygon, public COutput { public: int area (void) { return (width * height / 2); } }; main () { CRectangle rect; CTriangle trgl; rect.set_values (4,5); trgl.set_values (4,5); rect.output (rect.area()); trgl.output (trgl.area()); return 0; }

Polymorphism (many forms) binds methods to objects at run time Identically named methods that have different behavior depending on the type of object that they reference shape Circle draw Line draw base derived shape* compo_fig(100) /* pointers to either circles or lines */ for ( I=0; I<100; I++) compo_fig[I] -> draw();

Function overloading:  distinct by arguments void print (int n) {… }; void print (float a) {… }; void print (char * s) {… };

Operator overloading  defined operator for different objects class point2d{ int x;int y; public: point2d(int,int); point2d operator+(point2d); } point2d point2d::operator+(poin2d b) {poin2d total; total.x=x+b.x; total.y=y+b.y; return(total); } main(){ piont2d a(1,2),b(2,3); point2d c(1,1); c=a+b; … }

// classes example #include class CRectangle { int x, y; public: void set_values (int,int); int area (void) {return (x*y);} }; void CRectangle::set_values (int a, int b) { x = a; y = b; } main () { CRectangle rect; rect.set_values (3,4); cout << "area: " << rect.area(); }

Constructors Using the same name as class // classes example #include class CRectangle { int width, height; public: CRectangle (int,int); int area (void) {return (width*height);} }; CRectangle::CRectangle (int a, int b) { width = a; height = b; } main () { CRectangle rect (3,4); CRectangle rectb (5,6); cout << "rect area: " << rect.area() << endl; cout << "rectb area: " << rectb.area() << endl; } constructor

// this is stored as poinr2d.h #include class point2d{ double x; double y; public: point2d(double,double); point2d(void); void show(FILE *f=stdout); point2d operator+(point2d); point2d operator=(point2d); friend double length(point2d *,point2d *); }; // comments Private members coord. of a point Constructors, same name as class standard constructor default constructor (overload) Prints out coord. of a point Overload functions (re-define operation) Not a member of class but can access x,y

point2d p1(1.0,2.0); // the first constructor is used point2d p2; // the second constructor is used FILE *fout fout = fopen(“my_result”,”w”); p1.show(); // values for p1 written on standard output p2.show(fout); // values for p2 written on “my_result” q -> show(fout); // writes point2d q points to (q is a pointer) point2d p1(1.0,2.0), p2(3.0,4.0) p1+p2 means p2.operator+(p2) is called p1=p2 invokes p2.operator=(p1)

friend double length(point2d *,point2d *); 2 pointers as arguments returns a double

#include “point2d.h” point2d :: point2d(double a,double b) { x=a; y=b; } point2d :: point2d(void) { x=0.0; y=0.0; }

void point2d :: show(FILE *f) { fprintf(f, ”point is (%lf,%lf)\n”,x,y); } point2d point2d ::operator+(point2d b) { point2d total; total.x = x+b. x; total.y = y+b.y; return (total); }

point2d point2d :: operator=(point2d b) { if (this != &b) { x = b.x; y =b.y; } return (b); } The variable this is created automatically in every member function. It’s a pointer to a in expression a=b.

#include double length(point2d *p0, point2d *p1) { double temp=(p0 ->x - p1 ->x)*(p0 ->x - p1 ->x)+ (p0 ->y - p1 ->y)*(p0 ->y - p1 ->y); return (sqrt(temp)); }

#include “point2d.h” main() { point2d a,b(1.5,2.5); point2d c=b; FILE* fout; fout = fopen(”example.txt”,”w”); a.show(); b.show(); c.show(); printf(“dist a-c is %lf\n,length (&a,&c));

a=b; a.show(); c = a+b; c.show(); a=b=c; a.show(); b.show(); c.show(fout); )

Looking toward final exam