Docstoc

Looking toward C++

Document Sample
Looking toward C++ Powered By Docstoc
					             Looking toward C++

Object-Oriented Programming
    circle              data



                                    C.draw()
    draw                method



Traditional Programming
                                    draw(CIR)
   Procedure-Oriented Programming
   Task-Based Programming
                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
                            Top-down design
problem specification
                            • Problem specific
algorithm design            • Easy programming for
                              new programmers
functions
                            • Reusable ?
coding                      • Large system
                              implementation?
testing

program
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++

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
                   Example of draw a pattern

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

                  draw



         circle
object
          line
 Class – abstract characterization of a set of objects


           class string {
                                                   Data (variable)
             char data[80];
           public:
members      void store (char *);
             int length( );                      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
       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.
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;
• 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( );
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);
                                              Functions
          void set_loca(int,int);             defined later
};
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 */
// 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
pen
            y
            status
                                  Base class
            set_status
            set_loca



color_pen       X
                y                    Derived class
                status
                set_status
                set_loca
                color set_color
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); }                 cout c++ print
};                                                           endl end line
class CTriangle: public CPolygon
{ public:
int area (void) { return (width * height / 2); }
};
Multiple inheritance

      Inherits data and methods from multiple base class




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

class CRectangle: public CPolygon, public Coutput
 { public: int area (void)
{ return (width * height); }
};
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                        Line                    derived
 draw                          draw

   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:                                main(){
point2d(int,int);                      piont2d a(1,2),b(2,3);
point2d operator+(point2d);            point2d c(1,1);
}                                      c=a+b;
                                       …
point2d point2d::operator+(poin2d b)   }
{poin2d total;
  total.x=x+b.x;
  total.y=y+b.y;
  return(total);
}
// 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();
}
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; }
                                                   constructor
     main () {
     CRectangle rect (3,4);
     CRectangle rectb (5,6);
     cout << "rect area: " << rect.area() << endl;
     cout << "rectb area: " << rectb.area() << endl; }
                                                    // comments
           // this is stored as poinr2d.h
           #include <stdio.h>
           class point2d{
                     double x;                         Private members
                     double y;                         coord. of a point
           public:
                                                   Constructors, same name as class
                     point2d(double,double);
                                                   standard constructor
                     point2d(void);
                                                   default constructor (overload)
                     void show(FILE *f=stdout);
                                                    Prints out coord. of a point
                     point2d operator+(point2d);
                                                       Overload functions
Not a member of      point2d operator=(point2d);
class but can                                         (re-define operation)
access x,y           friend double length(point2d *,point2d *);
           };
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

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:0
posted:5/8/2013
language:Unknown
pages:34