18
By Wayne Cheng

By Wayne Cheng. Introduction Five Tenets Terminology The foundation of C++: Classes

Embed Size (px)

Citation preview

Page 1: By Wayne Cheng.  Introduction  Five Tenets  Terminology  The foundation of C++: Classes

By Wayne Cheng

Page 2: By Wayne Cheng.  Introduction  Five Tenets  Terminology  The foundation of C++: Classes

Introduction

Five Tenets

Terminology

The foundation of C++: Classes

Page 3: By Wayne Cheng.  Introduction  Five Tenets  Terminology  The foundation of C++: Classes

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 don’t know what is inside it.

Page 4: By Wayne Cheng.  Introduction  Five Tenets  Terminology  The foundation of C++: Classes

Encapsulation

Data Abstraction

Information Hiding

Inheritance

Polymorphism

Page 5: By Wayne Cheng.  Introduction  Five Tenets  Terminology  The foundation of C++: Classes

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.

Page 6: By Wayne Cheng.  Introduction  Five Tenets  Terminology  The foundation of C++: Classes

The goal is to force users to interface with the object’s 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)

Page 7: By Wayne Cheng.  Introduction  Five Tenets  Terminology  The foundation of C++: Classes

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.

Page 8: By Wayne Cheng.  Introduction  Five Tenets  Terminology  The foundation of C++: Classes

The syntax for a class definition iso class Class_Name

{ public: Member_Specification 1 Member_Specification 2

…Member_Specification 3

private:Member_Specification_n+1Member_Specification_n+2…

};

Page 9: By Wayne Cheng.  Introduction  Five Tenets  Terminology  The foundation of C++: Classes

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.

Page 10: By Wayne Cheng.  Introduction  Five Tenets  Terminology  The foundation of C++: Classes

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.

Page 11: By Wayne Cheng.  Introduction  Five Tenets  Terminology  The foundation of C++: Classes

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

Page 12: By Wayne Cheng.  Introduction  Five Tenets  Terminology  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.

Page 13: By Wayne Cheng.  Introduction  Five Tenets  Terminology  The foundation of C++: 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

Page 14: By Wayne Cheng.  Introduction  Five Tenets  Terminology  The foundation of C++: Classes

Examine a stack class: two functions and two pieces of data.Class: stackFunctions1. “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.

2. “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:1. The first element of data will be the stack itself.2. The second element of data will be called top and will mark the current

position in the stack.

Page 15: By Wayne Cheng.  Introduction  Five Tenets  Terminology  The foundation of C++: Classes

/////////////////////////////////////////////////////

//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 stack[10], top; //the definition of calss variables

}

Page 16: By Wayne Cheng.  Introduction  Five Tenets  Terminology  The foundation of C++: Classes

///////////////////////////////////////////////

//File: IntStack.cpp

///////////////////////////////////////////////

#include “IntStack.h” //Import class specification

IntStack::IntStack() //The constructor

{ top = -1; }

int IntStack::push(int x) //The push function implementation

{ if(top == 9) return 0;

top++;

stack[top] = x;

return 1; }

int IntStack::pop(int & x) //The pop function implementation

{ if(top == -1) return 0;

x = stack[top];

top--;

return 1; }

Page 17: By Wayne Cheng.  Introduction  Five Tenets  Terminology  The foundation of C++: Classes

///////////////////////////////////////////////pgm.cpp/////////////////////////////////////////////#include “IntStack.h”#include <iostream.h>void main ( ){ IntStack stack;

int back;for (int x = 0; x < 12; x++){ cout << “Pushing “ << x << “…”;

cout << (stack.push(x)?”Success” : ”Fail”) << endl; }for (x = 0; x < 12; x++){ cout << “Poping…”;

(stack.pop(back)? (cout << back) : (cout << “Fail”)) << endl; }}

Page 18: By Wayne Cheng.  Introduction  Five Tenets  Terminology  The foundation of C++: Classes