Use Classes with Object-Oriented Programming in C - PowerPoint PPT Presentation

About This Presentation
Title:

Use Classes with Object-Oriented Programming in C

Description:

By Wayne Cheng – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 19
Provided by: Way112
Learn more at: https://www.cs.kent.edu
Category:

less

Transcript and Presenter's Notes

Title: Use Classes with Object-Oriented Programming in C


1
Use Classes with Object-Oriented Programming in
C
  • By Wayne Cheng

2
Object-Oriented Programming
  • Introduction
  • Five Tenets
  • Terminology
  • The foundation of C Classes

3
Introduction
  • It is the name given to a specific paradigm in
    the field of programming.
  • With objects we are creating pieces of code that
    can stand alone.
  • You know the box has inputs and outputs but you
    dont know what is inside it.

4
Five Tenets
  • Encapsulation
  • Data Abstraction
  • Information Hiding
  • Inheritance
  • Polymorphism

5
Encapsulation
  • The goal is to bind data and the functions that
    operate on that data together in one object.
  • The functions will be our interface while the
    data will be hidden in the black box.
  • Classes in C bind data and functions together
    in objects.

6
Data Abstraction
  • The goal is to force users to interface with the
    objects functions rather than accessing data
    directly.
  • Data is in the box.
  • The user can not access the data, he/she must use
    the defined interface functions to manipulate the
    data.
  • Public elements of class are accessible from
    outside of class.
  • Private restricts access to these functions and
    data members to this class alone. (as default)
  • Protected restricts access to these functions
    and data member to this class and its children.
    (Children are classes derived from the current
    class)

7
Information Hiding
  • The goal is to prevent the user from seeing how
    our functions are implemented and exactly what
    they do.
  • All the user will see are function prototypes,
    but no actual implementation.
  • Multiple file projects by storing each class
    implementation in its own file, we can compile
    and distribute them without allowing users to see
    the internals of functions.

8
C Class definition
  • The syntax for a class definition is
  • class Class_Name public Member_Specificati
    on 1 Member_Specification 2 Member_Specif
    ication 3 private Member_Specification_n1 M
    ember_Specification_n2

9
Inheritance
  • It is a mechanism that allows us to include
    Parent class into a Child class.
  • It just another name for the topic of derived
    classes.
  • The goal is code reusability and extensibility.
  • With it we can reuse code without having to cut
    and paste it out of our old programs.
  • It is a major stepping stone on the path to
    polymorphism.

10
Polymorphism
  • The goal is to write generic code that can handle
    multiple objects in the same way.
  • In code, we can pass multiple objects all as the
    same base object.
  • It is refers to the ability to associate multiple
    meaning to one function name by means of a
    special mechanism known as Late Binding.
  • We use class hierarchies to allow us to treat
    child classes as their parent class or
    grandparent, etc.

11
Terminology
  • Object A collection of related data and
    functions bound together.
  • Method A function of an object.
  • Message A call to an object method. The term
    message passing in reference to OOP you are
    dealing with method calls.
  • Member A method or variable that is part of an
    object (or class).
  • Association an object using another object that
    exists outside of the first. (Must pass by
    address or reference)
  • Aggregation An object that contains another
    object.
  • Generalization An object that publicly inherits
    its parent. (Inheritance)
  • Instance variable of data type (class or
    struct).

12
The foundation of C Classes
  • The class is how we are able to implement OOP
    concepts in C.
  • The class by itself gives us three of our five
    tenets OOP such are Encapsulation, Data
    Abstraction and Information Hiding.
  • It is a technique used in binding functions to
    the data that they manipulate. (Encapsulation)
  • It allows us to protect the data from the users
    direct manipulation. (Data Abstraction)
  • It allows us to hide the implementation of
    functions from the user. (Information Hiding)
  • It defines all its members to be private by
    default.

13
Classes
  • A class is a data type whose variables are
    objects
  • The definition of a class includes
  • - Description of the kinds of values of the
    member variables
  • - Description of the member functions
  • A class description is somewhat like a structure
    definition plus the member variables

14
C Class Example
  • Examine a stack class two functions and two
    pieces of data.
  • Class stack
  • Functions
  • Push it will have one parameter and will
    return a Boolean value indicating success or
    failure. The data passed in will be placed at
    the into the stack at the top.
  • Pop it will have one parameter, a reference,
    and will return a Boolean value indicating
    success or failure. The value of the reference
    will be set to the value at the top of the
    stack.
  • Data
  • The first element of data will be the stack
    itself.
  • The second element of data will be called top and
    will mark the current position in the stack.

15
C Class Code
  • //////////////////////////////////////////////////
    ///
  • //File IntStack.h
  • // This is an integer stack class.
  • //////////////////////////////////////////////////
    ///
  • class IntStack
  • public
  • IntStack() //the class
    constructors
  • int push(int) //the push function prototype
  • int pop(int) //the pop function prototype
  • private
  • int stack10, top //the definition of calss
    variables

16
Class Code
  • ///////////////////////////////////////////////
  • //File IntStack.cpp
  • ///////////////////////////////////////////////
  • include IntStack.h //Import class
    specification
  • IntStackIntStack() //The constructor
  • top -1
  • int IntStackpush(int x) //The push function
    implementation
  • if(top 9) return 0
  • top
  • stacktop x
  • return 1
  • int IntStackpop(int x) //The pop function
    implementation
  • if(top -1) return 0
  • x stacktop
  • top--
  • return 1

17
Class Code
  • /////////////////////////////////////////////
  • //pgm.cpp
  • /////////////////////////////////////////////
  • include IntStack.h
  • include ltiostream.hgt
  • void main ( )
  • IntStack stack
  • int back
  • for (int x 0 x lt 12 x)
  • cout ltlt Pushing ltlt x ltlt
  • cout ltlt (stack.push(x)?Success Fail) ltlt
    endl
  • for (x 0 x lt 12 x)
  • cout ltlt Poping
  • (stack.pop(back)? (cout ltlt back) (cout ltlt
    Fail)) ltlt endl

18
Questions ?
Write a Comment
User Comments (0)
About PowerShow.com