Looking toward C++ Object-Oriented Programming Traditional Programming Procedure-Oriented...
-
Upload
theodore-potter -
Category
Documents
-
view
218 -
download
1
Transcript of Looking toward C++ Object-Oriented Programming Traditional Programming Procedure-Oriented...
Looking toward C++
Object-Oriented Programming
Traditional ProgrammingProcedure-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
objectsContains data as well as functions
classesAbstract characterization of a set of objects
inheritanceNew class from old class
polymorphismIdentical 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 1989. • 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 == circledraw circleif parameter == linedraw 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 <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
Multiple inheritance
class DrawableString: public Point, public String {
Inherits data and methods from multiple base class
// 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; }
Polymorphism (many forms)binds methods to objects at run timeIdentically 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 operatorfor 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 <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(); }
ConstructorsUsing 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
// 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 *);
};
// 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 <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));
}
#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