Introduction to C - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to C

Description:

m1.next = &e1; // put employee e1 on elist. e1.next = &m2 // put ... Class hierarchies. C can express a directed acyclic graph of classes. class employee ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 57
Provided by: thearbe
Category:

less

Transcript and Presenter's Notes

Title: Introduction to C


1
Introduction to C
2
Overview
  • Enhanced C.
  • OOP classes.
  • Inheritance.

3
Enhanced C
  • Includes C and more.
  • Compilation using g.
  • Has a standard library for added functionality.

4
Overloaded function names
  • Using the same name for operations on different
    types is called overloading.
  • int max(int, int)
  • double max(double, double)
  • long max(long, long)

5
Overloaded function names
  • Finding the right version to call from a set of
    overloaded functions is done by looking for a
    best match between the type of the argument
    expression and the parameters of the functions
  • Exact match
  • Match using promotions bool,char,short to int
    float to double.
  • Match using standard conversions
  • int to double, double to int

6
Overloaded function names
  • void print(char)
  • void print(int)
  • void h(char c, int i, short s, double d)
  • print(c) // exact match invoke print(char)
  • print(i) // exact match invoke print(int)
  • print(s) // promotion invoke print(int)
  • print(d) // conversion double to int

7
Default arguments
  • A general function often needs more arguments
    than are necessary to handle simple cases.
  • For example, giving the user an option of what
    base to print in seems reasonable, but in most
    programs integers will be printed as decimal
    integer values.

8
Default arguments
  • void print(int value, int base10)
  • void f()
  • print(31) // produce output 31
  • print(31, 16) // 1f
  • print(31, 2) // 11111
  • print() // error
  • print(31,5,6) // error
  • Default values may be provided for trailing
    arguments only.

9
Output
  • The iostream library defines output for every
    built-in type. Values output to cout are
    converted to a sequence of characters.
  • void f(int i)
  • cout ltlt the value of i is "
  • cout ltlt i
  • cout ltlt \n

10
Output
  • The result of an output expression can itself be
    used for further output.
  • void f(int i)
  • cout ltlt the value of i is ltlt i ltlt endl
  • Equivalent to previous function.

11
Input
  • void f()
  • int i
  • cin gtgt i // read an integer into i

12
new and delete
  • The new operator creates an object, and it
    exists until destroyed by the delete operator.
  • struct S
  • // ..
  • S f()
  • S p new S
  • return p
  • void g(S p) delete p
  • int q new int20
  • delete q

13
Inline function
  • A direction for the compiler to generate code for
    a call inline.
  • C macro
  • define max(a,b) (((a) gt (b) ? (a) (b))
  • C inline function
  • inline int max(int a, int b) return agtb ? a b

14
Reference
  • A reference is an alternative name for an object.
    The notation X means reference to X. A reference
    must be initialized.
  • void f()
  • int i 1
  • int r i // r and i refer to same int
  • int x r // x 1
  • r 2 // i 2

15
Argument passing
  • void f(int val, int ref)
  • val
  • ref
  • When f() is called, val increments a local copy
    of the 1st argument, whereas ref increments the
    2nd argument.

16
Argument passing
  • void g()
  • int i 1
  • int j 1
  • f(i, j) // increments j but not i
  • The 1st argument, i, is passed by value, the 2nd
    argument, j, is passed by reference.

17
Argument passing
  • It can be more efficient to pass a large object
    by reference than to pass it by value.
  • Declaring an argument const does not enable the
    called function to change the object value.
  • void f(const Large arg)
  • // the value of arg cannot be changed

18
Example code
  • include ltiostreamgt
  • using namespace std
  • const double m_to_k 1.609 // miles to
    kilometers
  • inline int convert(int mi) return (mi
    m_to_k)
  • int main()
  • int miles
  • do
  • cout ltlt "Input distance in miles "
  • cin gtgt miles
  • cout ltlt "\nDistance is " ltlt convert(miles) ltlt "
    km." ltlt endl
  • while (miles gt 0)
  • return 0

19
Object Oriented Programming
  • Encapsulation of a set of data types and their
    operations the class construct.
  • Data hiding.
  • Data type hierarchy code reuse via inheritance
    deriving a new class from an existing one.
  • Design is crucial!

20
Classes
  • A class is a user-defined type, which allows
    encapsulation.
  • The construct
  • class X ...
  • is a class definition.
  • Contains data and function members.
  • Access control mechanisms (private vs. public
    see below).

21
Class example 1
  • class Point
  • private
  • int x, y, visible // private part
  • // can be used only by member functions
  • public // public part, interface
  • void setpoint(int xx, int yy) xxx yyy
    visible 1
  • int getx() return x
  • int gety() return y
  • void draw()
  • gotoxy(x,y)
  • putchar('')
  • visible 1
  • void hide()
  • gotoxy(x,y)
  • putchar(' ')
  • visible 0
  • // member functions defined within the class
    definition,

22
Class example 1
  • Point p1, p2
  • p1.init(10,20)
  • p1.show()
  • p2.init(15,15)
  • p2.show()
  • p1.hide()

23
Member functions
  • A member function declaration specifies
  • The function can access the private part of the
    class declaration.
  • The function is in the scope of the class.
  • The function must be invoked on an object.
  • In a member function, member names can be used
    without explicit reference to an object.

24
Class example 2
  • class Array
  • private
  • int parray
  • int size
  • public
  • void init()
  • int get(int indx)
  • void print()
  • void set(int indx, int value)
  • // is the scope resolution operator
  • void Arrayinit() parray 0 size 0
  • int Arrayget(int i) return parrayi
  • void Arrayprint()
  • for (int i 0 i lt size i)
  • cout ltlt endl ltlt array ltlt i ltlt ltlt
    parrayi

25
Class example 2
  • void Arrayset(int indx, int value)
  • if (indx gt size)
  • int p new intindx1
  • for (int i 0 i lt size i)
  • pi parrayi
  • delete parray
  • size indx
  • parray p
  • parrayindx value
  • Array a1
  • a1.init()
  • a1.set(3,50)
  • a1.set(1,100)
  • a1.set(2,70)
  • a1.print()

26
Constructors
  • Using functions such as init() to initialize
    class objects is error prone and complicates the
    code.
  • Constructors are member functions with the
    explicit purpose of constructing values of a
    given type, recognized by having the same name as
    the class itself.

27
Destructors
  • A constructor initializes an object, creating the
    environment in which the member functions
    operate. This may involve acquiring a resource
    such as a file, lock, and usually memory, that
    must be released after use.
  • Destructor is a function that is guaranteed to be
    invoked when an object is destroyed, cleans up
    and releases resources.

28
Constructors and destructors
  • class Array
  • Array() // default constructor
  • Array(int) // constructor
  • Array() // destructor
  • ArrayArray()
  • parray 0
  • size 0
  • ArrayArray(int len)
  • parray new intlen
  • size len
  • ArrayArray()
  • delete parray

29
Constructors
  • Array a
  • Array b(10)

30
Constructors and destructors
  • class String
  • char str //private data
  • public
  • String() str new char1 str0 0
  • String(const char s)
  • String()
  • int length()
  • StringString(const char s)
  • str new charstrlen(s)1
  • strcpy(str, s) // destination, source
  • StringString() delete str

31
Copy constructor
  • If x is an object of class X, X yx (or,
    equivalently X y(x)) by default means
    member-wise copy of x into y. This can cause
    undesired effects when used on objects of a class
    with pointer members.
  • The programmer can define a suitable meaning for
    copy operations by a copy constructor (and
    similarly for the assignment op.)
  • TableTable(const Table t)
  • p new Namesize t.size
  • for (int i 0 i lt size i) pi t.pi
  • StringString(const String s)
  • str new chars.length()1
  • strcpy(str, s)

32
Self-reference, this
  • Each (non-static) member function knows what
    object it was invoked for and can explicitly
    refer to it.
  • Implicitly declared as X const this
  • Used to return the object or to manipulate a
    self-referential data structure.
  • Date Dateset_date(int dd, int mm, int yy)
  • d dd
  • m mm
  • y yy
  • return this // enables cascading
  • d.set_date( 20, 1, 2000 ).print()

33
Friends
  • A friend function declaration specifies the
    function can access the private part of the class
    declaration.
  • Useful for object output (see below).

34
Friends example
  • class Vector
  • int v4
  • public
  • Vector() v0 v1 v2 v3 0
  • int elem(int i) return vi
  • class Matrix
  • Vector v4
  • public
  • int elem(int i, int j) return vi.elem(j)
  • // multiply cannot be a member of both Matrix and
    Vector
  • Vector multiply(const Matrix m, const Vector
    v)
  • Vector r
  • for (int i 0 i lt 4 i)
  • for(int j 0 j lt 4 j)
  • r.elem(i) m.elem(i,j) v.elem(j)
  • return r

35
Friends example
  • class Matrix
  • class Vector
  • int v4
  • public
  • Vector() v0 v1 v2 v3 0
  • int elem(int i) return vi
  • friend Vector multiply(const Matrix , const
    Vector )
  • class Matrix
  • Vector v4
  • public
  • int elem(int i, int j) return vi.elem(j)
  • friend Vector multiply(const Matrix , const
    Vector )
  • // multiply can be a friend of both Matrix and
    Vector
  • Vector multiply(const Matrix m, const Vector
    v)
  • Vector r
  • for (int i 0 i lt 4 i)
  • for(int j 0 j lt 4 j)

36
Operator overloading
  • Conventional shorthand
  • xyz is clearer than
  • multiply y by z and add the result to x
  • C supports a set of operators for its built-in
    types, and allows to provide operators for user
    defined types.

37
Operator overloading
  • class complex
  • double re, im
  • Public
  • //member initialization list
  • complex(double r, double i) re(r), im(i)
  • complex operator(complex)
  • complex operator(complex)
  • void f()
  • complex a complex(1, 3.1)
  • complex b complex(1.2,2)
  • complex c a b // shorthand
  • complex d a.operator(b) // explicit call
  • complex e b c d // usual precedence

38
Operator overloading example
  • class Point
  • int x, y
  • public
  • Point()
  • Point(int xx, int yy) x xx y yy
  • Point operator(Point p)
  • Point operator()
  • void show() gotoxy(x, y) putchar('')
  • Point Pointoperator(Point p)
  • Point tmp
  • tmp.x x p.x
  • tmp.y y p.y
  • return tmp
  • Point Pointoperator()
  • x y
  • return this

39
Operator overloading example
  • int main()
  • Point p1(10,20), p2(2,10), p3
  • p3 p1 p2
  • p3.show()
  • (p1p2).show()

40
Derived classes
employee
manager
name age
name age group level
41
  • struct employee // base
  • char name
  • short age
  • short department
  • int salary
  • employee next
  • //...
  • struct manager
  • employee emp // manager's employee record
  • employee group // people managed
  • short level
  • Cannot put manager onto a list of employees
    without writing special code

42
Derived classes
  • // a manager is an employee
  • // derived
  • struct manager public employee
  • employee group // people managed
  • short level

employee
manager
43
Derived classes
  • void f()
  • manager m1, m2
  • employee e1, e2
  • employee elist // heterogonous list
  • elist m1 // put manager m1 on elist
  • m1.next e1 // put employee e1 on elist
  • e1.next m2 // put manager m2 on elist
  • m2.next e2 // put employee e2 on elist
  • e2.next 0

44
Derived classes
  • class employee
  • string name
  • public
  • employee next // public to allow list
    manipulation
  • void print() const
  • string getname() return name
  • class manager public employee
  • int level
  • public
  • void print() const //function overriding
  • void managerprint() const
  • cout ltlt name is ltlt getname() ltlt endl
  • cout ltlt name is ltlt name ltlt endl // error
  • employeeprint() // print employee
    information
  • cout ltlt level // print manager information

45
Access control
  • A member of a class can be private, protected, or
    public.
  • 1. If its private, its name can be used only by
    member functions and friends of the class in
    which it is declared.
  • 2. If its protected, its name can be used only
    by member functions and friends of the class in
    which its declared and by member function and
    friends of classes derived from this class.
  • 3. If its public, its name can be used by any
    function.

46
Access control
  • class Base1
  • private
  • int i
  • protected
  • int j
  • public
  • int k
  • main ()
  • Base1 b
  • int x
  • x b.i // error
  • x b.j // error
  • x b.k // ok

47
  • Access control
  • There are three kinds of functions accessing a
    class
  • Functions implementing the class (its friends and
    members)
  • Function implementing a derived class (the
    derived class friends and members)
  • Other functions.

general users
derived class member functions and friends
own member functions and friends
public
protected
private
48
Access control derived classes
  • Under public inheritance
  • Public member remains public in derived class.
  • Protected member remains protected in derived
    class.
  • Private member is hidden in derived class.

49
Constructors
  • Some derived classes need constructors. If the
    base class has a constructor then it must be
    called.
  • class employee
  • string name
  • int department
  • public
  • employee(const string n, int d)
  • class manager public employee
  • int level
  • public
  • manager(const string n, int d, int lvl)

50
Constructors
  • Arguments for base class constructor are
    specified in the definition of a derived class
    constructor.
  • A derived class constructor can specify
    initializers of its own members and immediate
    bases only, it cannot directly initialize members
    of a base.
  • Ctors are called bottom-up and dtors top-down.
  • employeeemployee(const string s, int d)
  • name(n), department(d) // initialize members
  • managermanager(const string s, int d, int lvl)
  • employee(n, d), // initialize base class
  • level(lvl) // initialize members
  • ,name(n) // error

51
Class hierarchies
  • C can express a directed acyclic graph of
    classes.
  • class employee /.../
  • class manager public employee /.../
  • class director public manager /.../
  • class temporary /.../
  • class secretary public employee /.../
  • class tsec public temporary, public secretary
    /.../
  • class consultant public temporary, public
    manager /.../

temporary
employee
secretary
manager
tsec
consultant
director
52
Polymorphism virtual functions
  • Functions in a base class that can be redefined
    in a derived class and have a polymorphic
    behavior the version used depends on the
    objects type, which is decided in run time.
  • class employee
  • virtual void print() const
  • class manager public employee
  • virtual void print() const
  • employee my_employee
  • my_employee-gtprint()

53
Abstract class
  • Some classes represent abstract concepts for
    which object cannot exist. For example, a shape
    makes sense only as the base of some class
    derived from it.
  • class shape
  • public
  • virtual void rotate(int)
  • error("shaperotate") // not elegant
  • virtual void draw() error("shapedraw")
  • shape s // legal but silly shapeless
    shape''

54
Abstract class
  • A class with one or more pure virtual functions
    is an abstract class
  • No objects of an abstract class can be created.
  • class shape
  • public
  • virtual void rotate(int) 0 // pure virtual
  • virtual void draw() 0
  • //...
  • shape s // error variable of abstract class
    shape

55
Abstract class
  • An abstract class can be used only as an
    interface and a base for other classes.
  • class point / ... /
  • class circle public shape
  • public
  • void rotate(int) // overrides shaperotate
  • void draw() // overrides shapedraw
  • circle(point p, int r)
  • private
  • int radius

56
References
  • The C programming language, Bjarne Stroustrup,
    Addison Wesley, 3rd Ed., 1997.
Write a Comment
User Comments (0)
About PowerShow.com