1 / 34

Looking toward C++

Looking toward C++. Object-Oriented Programming Traditional Programming Procedure-Oriented Programming Task-Based Programming. circle. data. draw. method. C.draw(). draw(CIR). Object. Part of a large program is viewed as an object Contains data and functions that operate on these data.

alyson
Download Presentation

Looking toward 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. Looking toward C++ Object-Oriented Programming Traditional Programming Procedure-Oriented Programming Task-Based Programming circle data draw method C.draw() draw(CIR)

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

  3. 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?

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

  5. A Brief History of C++ The C++ Programming Language is basically an extension of the C Programming Language. • 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 1989. • In 1983 Bjarne Stroustrup at Bell Labs • created C++. C++ was designed for the • UNIX system environment, Bjarne Stroustrup

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

  7. function draw circle object line

  8. Class – abstract characterization of a set of objects class string { char data[80]; public: void store (char *); int length( ); }; Data (variable) members 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

  9. class class_name{ permission_label_1: member1; permission_label_2: member2; ... }object_name; Permission: • 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.

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

  11. Class declaration does not create any objects/allocate storage string string1,string2; • Particular object of a class is referred as an instance • 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( );

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

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

  14. // 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

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

  16. class derived_class_name: publicbase_class_name; class ctriangle: public cpolygon { public: int area( ); }

  17. // derived classes # include <iostream.h> 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

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

  19. // multiple inheritance #include <iostream.h> 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; }

  20. 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 base Circle draw Line draw derived shape* compo_fig(100) /* pointers to either circles or lines */ for ( I=0; I<100; I++) compo_fig[I] -> draw();

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

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

  23. // classes example #include <iostream.h> 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(); }

  24. Constructors Using the same name as class // classes example #include <iostream.h> 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

  25. // comments // this is stored as poinr2d.h #include <stdio.h> 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 *); }; 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

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

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

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

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

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

  31. #include <math.h> 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)); }

  32. #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));

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

  34. Looking toward final exam

More Related