SlideShare a Scribd company logo
1 of 41
 Chapter 2 introduces Object
Oriented Programming.
 OOP is a relatively new
approach to programming
which supports the creation
of new data types and
operations to manipulate
those types.
 This presentation introduces
OOP.
Object Oriented
Programming
Data Structures
and Other Objects
Using C++
What is this Object ?
 There is no real
answer to the question,
but we’ll call it a
“thinking cap”.
 The plan is to describe
a thinking cap by
telling you what
actions can be done to
it.
Using the Object’s Slots
 You may put a piece of
paper in each of the two
slots (green and red), with a
sentence written on each.
 You may push the green
button and the thinking cap
will speak the sentence
from the green slot’s paper.
 And same for the red
button.
Example
Example
That test was
a breeze !
Example
I should
study harder !
Thinking Cap Implementation
 We can implement the
thinking cap using a
data type called a
class.
class thinking_cap
{
. . .
};
Thinking Cap Implementation
 The class will have
two components called
green_string and
red_string. These
compnents are strings
which hold the
information that is
placed in the two slots.
 Using a class permits
two new features . . .
class thinking_cap
{
. . .
char green_string[50];
char red_string[50];
};
Thinking Cap Implementation
 The two components
will be private
member variables.
This ensures that
nobody can directly
access this
information. The
only access is through
functions that we
provide for the class.
class thinking_cap
{
. . .
private:
char green_string[50];
char red_string[50];
};
Thinking Cap Implementation
 In a class, the
functions which
manipulate the class
are also listed.
class thinking_cap
{
public:
. . .
private:
char green_string[50];
char red_string[50];
};
Prototypes for the
thinking cap
functions go here,
after the word
public:
Thinking Cap Implementation
 In a class, the
functions which
manipulate the class
are also listed.
class thinking_cap
{
public:
. . .
private:
char green_string[50];
char red_string[50];
};
Prototypes for the
thinking cap
member functions
go here
Thinking Cap Implementation
class thinking_cap
{
public:
void slots(char new_green[ ], char new_red[ ]);
void push_green( ) const;
void push_red( ) const;
private:
char green_string[50];
char red_string[50];
};
Our thinking cap has at least three member functions:
Thinking Cap Implementation
class thinking_cap
{
public:
void slots(char new_green[ ], char new_red[ ]);
void push_green( ) const;
void push_red( ) const;
private:
char green_string[50];
char red_string[50];
};
The keyword const appears after two prototypes:
Files for the Thinking Cap
 The thinking_cap class
definition, which we have
just seen, is placed with
documentation in a file called
thinker.h, outlined here.
 The implementations of the
three member functions will
be placed in a separate file
called thinker.cxx, which we
will examine in a few
minutes.
Documentation
Class definition:
•thinking_cap class
definition which we
have already seen
Using the Thinking Cap
 A program that
wants to use the
thinking cap
must include the
thinker header
file (along with
its other header
inclusions).
#include <iostream.h>
#include <stdlib.h>
#include "thinker.h"
...
Using the Thinking Cap
 Just for fun, the
example program
will declare two
thinking_cap
variables named
student and fan.
#include <iostream.h>
#include <stdlib.h>
#include "thinker.h"
int main( )
{
thinking_cap student:
thinking_cap fan;
Using the Thinking Cap
 Just for fun, the
example program
will declare two
thinking_cap
objects named
student and fan.
#include <iostream.h>
#include <stdlib.h>
#include "thinker.h"
int main( )
{
thinking_cap student;
thinking_cap fan;
Using the Thinking Cap
 The program
starts by
calling the
slots member
function for
student.
#include <iostream.h>
#include <stdlib.h>
#include "thinker.h"
int main( )
{
thinking_cap student;
thinking_cap fan;
student.slots( "Hello", "Goodbye");
Using the Thinking Cap
 The program
starts by
activating the
slots member
function for
student.
#include <iostream.h>
#include <stdlib.h>
#include "thinker.h"
int main( )
{
thinking_cap student:
thinking_cap fan;
student.slots( "Hello", "Goodbye");
Using the Thinking Cap
 The member
function
activation
consists of four
parts, starting
with the object
name.
int main( )
{
thinking_cap student;
thinking_cap fan;
student.slots( "Hello", "Goodbye");
Using the Thinking Cap
 The instance
name is followed
by a period. int main( )
{
thinking_cap student;
thinking_cap fan;
student.slots( "Hello", "Goodbye");
Using the Thinking Cap
 After the period
is the name of
the member
function that you
are activating.
int main( ) {
thinking_cap student;
thinking_cap fan;
student.slots( "Hello", "Goodbye");
Using the Thinking Cap
 Finally, the
arguments for
the member
function. In this
example the first
argument
(new_green) is
"Hello" and the
second argument
(new_red) is
"Goodbye".
#include "thinker.h"
int main( ) {
thinking_cap student;
thinking_cap fan;
student.slots( "Hello", "Goodbye");
A Quiz
How would you
activate student's
push_green
member function ?
What would be the
output of student's
push_green
member function
at this point in the
program ?
int main( )
{
thinking_cap student;
thinking_cap fan;
student.slots( "Hello", "Goodbye");
A Quiz
Notice that the
push_green member
function has no
arguments.
At this point,
activating
student.push_green
will print the string
Hello.
int main( ) {
thinking_cap student;
thinking_cap fan;
student.slots( "Hello", "Goodbye");
student.push_green( );
A Quiz
Trace through this
program, and tell
me the complete
output.
int main( )
{
thinking_cap student;
thinking_cap fan;
student.slots( "Hello", "Goodbye");
fan.slots( "Go Cougars!", "Boo!");
student.push_green( );
fan.push_green( );
student.push_red( );
. . .
A Quiz
Hello
Go Cougars!
Goodbye
int main( )
{
thinking_cap student;
thinking_cap fan;
student.slots( "Hello", "Goodbye");
fan.slots( "Go Cougars!", "Boo!");
student.push_green( );
fan.push_green( );
student.push_red( );
. . .
What you know about Objects
 Class = Data + Member Functions.
 You know how to define a new class type, and
place the definition in a header file.
 You know how to use the header file in a
program which declares instances of the class
type.
 You know how to activate member functions.
 But you still need to learn how to write the
bodies of a class’s member functions.
Thinking Cap Implementation
class thinking_cap
{
public:
void slots(char new_green[ ], char new_red[ ]);
void push_green( );
void push_red( );
private:
char green_string[50];
char red_string[50];
};
Remember that the member function’s bodies
generally appear in a separate .cxx file.
Thinking Cap Implementation
class thinking_cap
{
public:
void slots(char new_green[ ], char new_red[ ]);
void push_green( );
void push_red( );
private:
char green_string[50];
char red_string[50];
};
We will look at the body of slots, which must copy its
two arguments to the two private member variables.
Thinking Cap Implementation
void thinking_cap::slots(char new_green[ ], char new_red[ ])
{
assert(strlen(new_green) < 50);
assert(strlen(new_red) < 50);
strcpy(green_string, new_green);
strcpy(red_string, new_red);
}
For the most part, the function’s body is no different
than any other function body.
But there are two special features about a
member function’s body . . .
Thinking Cap Implementation
 In the heading, the function's name is preceded by the
class name and :: - otherwise C++ won't realize this
is a class’s member function.
void thinking_cap::slots(char new_green[ ], char new_red[ ])
{
assert(strlen(new_green) < 50);
assert(strlen(new_red) < 50);
strcpy(green_string, new_green);
strcpy(red_string, new_red);
}
Thinking Cap Implementation
 Within the body of the function, the class’s member
variables and other member functions may all be
accessed.
void thinking_cap::slots(char new_green[ ], char new_red[ ])
{
assert(strlen(new_green) < 50);
assert(strlen(new_red) < 50);
strcpy(green_string, new_green);
strcpy(red_string, new_red);
}
Thinking Cap Implementation
 Within the body of the function, the class’s member
variables and other member functions may all be
accessed.
void thinking_cap::slots(char new_green[ ], char new_red[ ])
{
assert(strlen(new_green) < 50);
assert(strlen(new_red) < 50);
strcpy(green_string, new_green);
strcpy(red_string, new_red);
}
But, whose member
variables are
these? Are they
student.green_string
student.red_string
fan.green_string
fan.red_string
?
Thinking Cap Implementation
 Within the body of the function, the class’s member
variables and other member functions may all be
accessed.
void thinking_cap::slots(char new_green[ ], char new_red[ ])
{
assert(strlen(new_green) < 50);
assert(strlen(new_red) < 50);
strcpy(green_string, new_green);
strcpy(red_string, new_red);
}
If we activate student.slots:
student.green_string
student.red_string
Thinking Cap Implementation
 Within the body of the function, the class’s member
variables and other member functions may all be
accessed.
void thinking_cap::slots(char new_green[ ], char new_red[ ])
{
assert(strlen(new_green) < 50);
assert(strlen(new_red) < 50);
strcpy(green_string, new_green);
strcpy(red_string, new_red);
}
If we activate
fan.slots:
fan.green_string
fan.red_string
Thinking Cap Implementation
void thinking_cap::push_green
{
cout << green_string << endl;
}
Here is the implementation of the push_green
member function, which prints the green message:
Thinking Cap Implementation
void thinking_cap::push_green
{
cout << green_string << endl;
}
Here is the implementation of the push_green
member function, which prints the green message:
Notice how this member function implementation
uses the green_string member variable of the object.
A Common Pattern
 Often, one or more member functions will
place data in the member variables...
class thinking_cap {
public:
void slots(char new_green[ ], char new_red[ ]);
void push_green( ) const;
void push_red( ) const;
private:
char green_string[50];
char red_string[50];
};
 ...so that other member functions may use that
data.
slots push_green & push_red
 Classes have member variables and member
functions. An object is a variable where the data
type is a class.
 You should know how to declare a new class type,
how to implement its member functions, how to
use the class type.
 Frequently, the member functions of an class type
place information in the member variables, or use
information that's already in the member variables.
 In the future we will see more features of OOP.
Summary
THE END
Presentation copyright 2010, Addison Wesley Longman
For use with Data Structures and Other Objects Using C++
by Michael Main and Walter Savitch.
Some artwork in the presentation is used with permission from Presentation Task Force
(copyright New Vision Technologies Inc.) and Corel Gallery Clipart Catalog (copyright
Corel Corporation, 3G Graphics Inc., Archive Arts, Cartesia Software, Image Club
Graphics Inc., One Mile Up Inc., TechPool Studios, Totem Graphics Inc.).
Students and instructors who use Data Structures and Other Objects Using C++ are
welcome to use this presentation however they see fit, so long as this copyright notice
remains intact.

More Related Content

Similar to chapt02 (1).ppt

Similar to chapt02 (1).ppt (20)

CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Python classes objects
Python classes objectsPython classes objects
Python classes objects
 
classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptx
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rd
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
 
Chap 3 Python Object Oriented Programming - Copy.ppt
Chap 3 Python Object Oriented Programming - Copy.pptChap 3 Python Object Oriented Programming - Copy.ppt
Chap 3 Python Object Oriented Programming - Copy.ppt
 
Programming in Java: Why Object-Orientation?
Programming in Java: Why Object-Orientation?Programming in Java: Why Object-Orientation?
Programming in Java: Why Object-Orientation?
 
Classes and Inheritance
Classes and InheritanceClasses and Inheritance
Classes and Inheritance
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
Pythonclass
PythonclassPythonclass
Pythonclass
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
oops-1
oops-1oops-1
oops-1
 
Unit i
Unit iUnit i
Unit i
 
Start machine learning in 5 simple steps
Start machine learning in 5 simple stepsStart machine learning in 5 simple steps
Start machine learning in 5 simple steps
 
Objective c
Objective cObjective c
Objective c
 
3 functions and class
3   functions and class3   functions and class
3 functions and class
 
Cocoa for Web Developers
Cocoa for Web DevelopersCocoa for Web Developers
Cocoa for Web Developers
 
The power of CSS pseudo-elements
The power of CSS pseudo-elementsThe power of CSS pseudo-elements
The power of CSS pseudo-elements
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
 

Recently uploaded

Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 

Recently uploaded (20)

Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 

chapt02 (1).ppt

  • 1.  Chapter 2 introduces Object Oriented Programming.  OOP is a relatively new approach to programming which supports the creation of new data types and operations to manipulate those types.  This presentation introduces OOP. Object Oriented Programming Data Structures and Other Objects Using C++
  • 2. What is this Object ?  There is no real answer to the question, but we’ll call it a “thinking cap”.  The plan is to describe a thinking cap by telling you what actions can be done to it.
  • 3. Using the Object’s Slots  You may put a piece of paper in each of the two slots (green and red), with a sentence written on each.  You may push the green button and the thinking cap will speak the sentence from the green slot’s paper.  And same for the red button.
  • 7. Thinking Cap Implementation  We can implement the thinking cap using a data type called a class. class thinking_cap { . . . };
  • 8. Thinking Cap Implementation  The class will have two components called green_string and red_string. These compnents are strings which hold the information that is placed in the two slots.  Using a class permits two new features . . . class thinking_cap { . . . char green_string[50]; char red_string[50]; };
  • 9. Thinking Cap Implementation  The two components will be private member variables. This ensures that nobody can directly access this information. The only access is through functions that we provide for the class. class thinking_cap { . . . private: char green_string[50]; char red_string[50]; };
  • 10. Thinking Cap Implementation  In a class, the functions which manipulate the class are also listed. class thinking_cap { public: . . . private: char green_string[50]; char red_string[50]; }; Prototypes for the thinking cap functions go here, after the word public:
  • 11. Thinking Cap Implementation  In a class, the functions which manipulate the class are also listed. class thinking_cap { public: . . . private: char green_string[50]; char red_string[50]; }; Prototypes for the thinking cap member functions go here
  • 12. Thinking Cap Implementation class thinking_cap { public: void slots(char new_green[ ], char new_red[ ]); void push_green( ) const; void push_red( ) const; private: char green_string[50]; char red_string[50]; }; Our thinking cap has at least three member functions:
  • 13. Thinking Cap Implementation class thinking_cap { public: void slots(char new_green[ ], char new_red[ ]); void push_green( ) const; void push_red( ) const; private: char green_string[50]; char red_string[50]; }; The keyword const appears after two prototypes:
  • 14. Files for the Thinking Cap  The thinking_cap class definition, which we have just seen, is placed with documentation in a file called thinker.h, outlined here.  The implementations of the three member functions will be placed in a separate file called thinker.cxx, which we will examine in a few minutes. Documentation Class definition: •thinking_cap class definition which we have already seen
  • 15. Using the Thinking Cap  A program that wants to use the thinking cap must include the thinker header file (along with its other header inclusions). #include <iostream.h> #include <stdlib.h> #include "thinker.h" ...
  • 16. Using the Thinking Cap  Just for fun, the example program will declare two thinking_cap variables named student and fan. #include <iostream.h> #include <stdlib.h> #include "thinker.h" int main( ) { thinking_cap student: thinking_cap fan;
  • 17. Using the Thinking Cap  Just for fun, the example program will declare two thinking_cap objects named student and fan. #include <iostream.h> #include <stdlib.h> #include "thinker.h" int main( ) { thinking_cap student; thinking_cap fan;
  • 18. Using the Thinking Cap  The program starts by calling the slots member function for student. #include <iostream.h> #include <stdlib.h> #include "thinker.h" int main( ) { thinking_cap student; thinking_cap fan; student.slots( "Hello", "Goodbye");
  • 19. Using the Thinking Cap  The program starts by activating the slots member function for student. #include <iostream.h> #include <stdlib.h> #include "thinker.h" int main( ) { thinking_cap student: thinking_cap fan; student.slots( "Hello", "Goodbye");
  • 20. Using the Thinking Cap  The member function activation consists of four parts, starting with the object name. int main( ) { thinking_cap student; thinking_cap fan; student.slots( "Hello", "Goodbye");
  • 21. Using the Thinking Cap  The instance name is followed by a period. int main( ) { thinking_cap student; thinking_cap fan; student.slots( "Hello", "Goodbye");
  • 22. Using the Thinking Cap  After the period is the name of the member function that you are activating. int main( ) { thinking_cap student; thinking_cap fan; student.slots( "Hello", "Goodbye");
  • 23. Using the Thinking Cap  Finally, the arguments for the member function. In this example the first argument (new_green) is "Hello" and the second argument (new_red) is "Goodbye". #include "thinker.h" int main( ) { thinking_cap student; thinking_cap fan; student.slots( "Hello", "Goodbye");
  • 24. A Quiz How would you activate student's push_green member function ? What would be the output of student's push_green member function at this point in the program ? int main( ) { thinking_cap student; thinking_cap fan; student.slots( "Hello", "Goodbye");
  • 25. A Quiz Notice that the push_green member function has no arguments. At this point, activating student.push_green will print the string Hello. int main( ) { thinking_cap student; thinking_cap fan; student.slots( "Hello", "Goodbye"); student.push_green( );
  • 26. A Quiz Trace through this program, and tell me the complete output. int main( ) { thinking_cap student; thinking_cap fan; student.slots( "Hello", "Goodbye"); fan.slots( "Go Cougars!", "Boo!"); student.push_green( ); fan.push_green( ); student.push_red( ); . . .
  • 27. A Quiz Hello Go Cougars! Goodbye int main( ) { thinking_cap student; thinking_cap fan; student.slots( "Hello", "Goodbye"); fan.slots( "Go Cougars!", "Boo!"); student.push_green( ); fan.push_green( ); student.push_red( ); . . .
  • 28. What you know about Objects  Class = Data + Member Functions.  You know how to define a new class type, and place the definition in a header file.  You know how to use the header file in a program which declares instances of the class type.  You know how to activate member functions.  But you still need to learn how to write the bodies of a class’s member functions.
  • 29. Thinking Cap Implementation class thinking_cap { public: void slots(char new_green[ ], char new_red[ ]); void push_green( ); void push_red( ); private: char green_string[50]; char red_string[50]; }; Remember that the member function’s bodies generally appear in a separate .cxx file.
  • 30. Thinking Cap Implementation class thinking_cap { public: void slots(char new_green[ ], char new_red[ ]); void push_green( ); void push_red( ); private: char green_string[50]; char red_string[50]; }; We will look at the body of slots, which must copy its two arguments to the two private member variables.
  • 31. Thinking Cap Implementation void thinking_cap::slots(char new_green[ ], char new_red[ ]) { assert(strlen(new_green) < 50); assert(strlen(new_red) < 50); strcpy(green_string, new_green); strcpy(red_string, new_red); } For the most part, the function’s body is no different than any other function body. But there are two special features about a member function’s body . . .
  • 32. Thinking Cap Implementation  In the heading, the function's name is preceded by the class name and :: - otherwise C++ won't realize this is a class’s member function. void thinking_cap::slots(char new_green[ ], char new_red[ ]) { assert(strlen(new_green) < 50); assert(strlen(new_red) < 50); strcpy(green_string, new_green); strcpy(red_string, new_red); }
  • 33. Thinking Cap Implementation  Within the body of the function, the class’s member variables and other member functions may all be accessed. void thinking_cap::slots(char new_green[ ], char new_red[ ]) { assert(strlen(new_green) < 50); assert(strlen(new_red) < 50); strcpy(green_string, new_green); strcpy(red_string, new_red); }
  • 34. Thinking Cap Implementation  Within the body of the function, the class’s member variables and other member functions may all be accessed. void thinking_cap::slots(char new_green[ ], char new_red[ ]) { assert(strlen(new_green) < 50); assert(strlen(new_red) < 50); strcpy(green_string, new_green); strcpy(red_string, new_red); } But, whose member variables are these? Are they student.green_string student.red_string fan.green_string fan.red_string ?
  • 35. Thinking Cap Implementation  Within the body of the function, the class’s member variables and other member functions may all be accessed. void thinking_cap::slots(char new_green[ ], char new_red[ ]) { assert(strlen(new_green) < 50); assert(strlen(new_red) < 50); strcpy(green_string, new_green); strcpy(red_string, new_red); } If we activate student.slots: student.green_string student.red_string
  • 36. Thinking Cap Implementation  Within the body of the function, the class’s member variables and other member functions may all be accessed. void thinking_cap::slots(char new_green[ ], char new_red[ ]) { assert(strlen(new_green) < 50); assert(strlen(new_red) < 50); strcpy(green_string, new_green); strcpy(red_string, new_red); } If we activate fan.slots: fan.green_string fan.red_string
  • 37. Thinking Cap Implementation void thinking_cap::push_green { cout << green_string << endl; } Here is the implementation of the push_green member function, which prints the green message:
  • 38. Thinking Cap Implementation void thinking_cap::push_green { cout << green_string << endl; } Here is the implementation of the push_green member function, which prints the green message: Notice how this member function implementation uses the green_string member variable of the object.
  • 39. A Common Pattern  Often, one or more member functions will place data in the member variables... class thinking_cap { public: void slots(char new_green[ ], char new_red[ ]); void push_green( ) const; void push_red( ) const; private: char green_string[50]; char red_string[50]; };  ...so that other member functions may use that data. slots push_green & push_red
  • 40.  Classes have member variables and member functions. An object is a variable where the data type is a class.  You should know how to declare a new class type, how to implement its member functions, how to use the class type.  Frequently, the member functions of an class type place information in the member variables, or use information that's already in the member variables.  In the future we will see more features of OOP. Summary
  • 41. THE END Presentation copyright 2010, Addison Wesley Longman For use with Data Structures and Other Objects Using C++ by Michael Main and Walter Savitch. Some artwork in the presentation is used with permission from Presentation Task Force (copyright New Vision Technologies Inc.) and Corel Gallery Clipart Catalog (copyright Corel Corporation, 3G Graphics Inc., Archive Arts, Cartesia Software, Image Club Graphics Inc., One Mile Up Inc., TechPool Studios, Totem Graphics Inc.). Students and instructors who use Data Structures and Other Objects Using C++ are welcome to use this presentation however they see fit, so long as this copyright notice remains intact.