0% found this document useful (0 votes)
12 views38 pages

Welcome

Uploaded by

Musa E. Ndlela
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views38 pages

Welcome

Uploaded by

Musa E. Ndlela
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 38

Welcome

 CSCI 3233.01
Object-Oriented Design and Programming
• Mon. and Wed., 6:00–8:29 p.m.
 Instructor: Charles Moen
• Email – crmoen@juno.com
• Web page – http://sce.cl.uh.edu/moenc/
• Office – Delta Building 232
• Office hours – Mon. and Wed. 5:30–6 and 8:30–9 p.m.
– Phone me at (281) 283-3848 so that I can open the hall door.
• Home – (713) 880-2924
1
CSCI 3233

 Understand the fundamentals of C++


 Understand the elements of object-oriented design
and programming in the context of C++
• Classes and objects
• Encapsulation, inheritance
• Using UML
 Know advanced C++ techniques
• Polymorphism
• Exception handling
• Templates
 Develop programs using C++
2
31-May-2006

Today’s Objectives
 Class roster
 Course introduction
• Required text, web site, syllabus, schedule
• How to succeed
 Brief facts about C++ and object-oriented programming
 Using C++ Compilers at UHCL – Microsoft Visual Studio
 Introduction to C++ programming (Ch. 1 and Ch. 2)
• A simple example
• Arithmetic operators, equality operators, relational operators
 Introduction to classes and objects (Ch. 3)
• Data members and member functions
• Example: the GradeBook class
 Next class – Intro to UML (Ch. 1) and Control Structures

3
Course Introduction

4
Course Introduction

Required Textbook

H. M. Deitel and P. J. Deitel, C++ How to


Program, Fifth Edition. Upper Saddle River, NJ:
Prentice Hall, 2005. ISBN 0-13-185757-6
Textbook web page:
http://www.deitel.com/books/cppHTP5/index.html

A recommended book (not required):


Ann R. Ford and Toby J. Teorey, Practical
Debugging in C++. Upper Saddle River, NJ:
Prentice Hall, 2002. ISBN 0-13-065394-2
5
Course Introduction

Course Web Pages

 Course page
http://sce.cl.uh.edu/moenc/csci3233summer06/index.html

 Schedule
http://sce.cl.uh.edu/moenc/csci3233summer06/schedule.html

 Syllabus
http://sce.cl.uh.edu/moenc/csci3233summer06/syllabus.html

 Resources
http://sce.cl.uh.edu/moenc/csci3233summer06/resources.html

6
Course Introduction

How to Succeed

 Read the chapter before class


 Participate
• In class and on the discussion board
 Learn object-oriented programming by
writing a lot of small programs
• To learn C++ programming skills
• To apply those skills in problem solving
 Start the assignments early
 Invest time and work hard!
7
Course Introduction

Bonus Labs
 Optional guided lab assignment
• At least 5 scheduled during the semester
• During regular class time, but at the end of the period
• Simple assignments that will give you some hands-on C++ experience
 Procedure
• First, a demo by the instructor
• Then you will complete the assignment on your own
• Print it and hand it in before the end of class
 Rules
• Do your own work, but you may help each other
• You may ask the instructor for help
• You may leave if you finish early or if you do not wish to do this
assignment
 1 bonus point added to a quiz grade for each lab correctly
completed and handed in before the end of class
8
C++ and
Object-Oriented Programming

Some brief facts

9
C++ and Object-Oriented Programming (Deitel; Randell; Wirth)

Historical Perspective

 1968 – The “software crisis”


• NATO Software Engineering Conference in
Garmisch, Germany
• Late projects, high costs, code was hard to
maintain
• Ad hoc programming
 Structured programming
• Adapted in the late 1960s to produce programs
that were easy to understand, develop, test,
and modify 10
C++ and Object-Oriented Programming (Deitel; Wirth)

Structured Programming

 Programs should be composed of pieces


that have only one starting point and one
terminating point
 Use only three kinds of “pieces”
1. Sequence
2. Selection control structures
– e.g., if, if/else, switch
3. Repetition control structures
– e.g., while, do/while, for
11
C++ and Object-Oriented Programming (Deitel; Eckel)

Object-Oriented Programming

 Became widely used in the 1990s


• Provided the foundation for real progress in creating software
that is easy to understand, develop, test, and modify
 A way of solving a problem by writing a program that
models the elements in the problem space as objects
• For example, a program used to support a video rental
business would have objects such as a store, videos, and
customers
• Programs are collections of objects
• Each object can provide a set of services
• Objects interact by sending messages telling each other what
to do
12
C++ and Object-Oriented Programming (Deitel)

Object-Oriented Programming

 Some advantages
• Easy to understand and develop because the objects directly
model real-world elements
• Faster development
• Easier to maintain
• Object-oriented software is easier to reuse
 Object-based programming
• Class – program element that contains both data and the
functions that manipulate it
• Object –instance of a class (like a “variable”)
 Object-oriented programming
• Inheritance
• Polymorphism
13
C++ and Object-Oriented Programming (Deitel; Josuttis)

Brief Facts About C++

 Evolved from C
 Designed and implemented by Bjarne Stroustrup
at the Bell Labs in the early 1980s
 “C with classes”
 Standardized by ISO in 1997
• Includes the C++ standard library
• Standard Template Library (STL)
– Part of the C++ standard library
– Readymade classes for data structures and algorithms

14
Using C++ Compilers at UHCL

15
Using C++ Compilers at UHCL

Compilers

 Microsoft Visual Studio


• IDE (Integrated Development Environment)
• PC Lab (Delta Bldg., second floor)
– Available in MS Visual Studio .NET (MSVS)
• Home use
– MSVS can be purchased from David Webb
– Delta D120 to purchase it
 GNU g++ compiler
• Sun Lab (Delta Bldg., first floor)
• Home use
– Log in using Telnet
– diamond.rocks.cl.uh.edu 16
Using C++ Compilers at UHCL

Using Microsoft Visual Studio

http://sce.cl.uh.edu/moenc/usingMSVS.html

17
Introduction to
C++ Programming

C++ Features in Chapter 1

18
Introduction to C++ Programming (Deitel)

A Simple Example

 Page 42, Fig. 2.4


 C++ features
• Comments
• Include directive
• main()
• Statements end with semicolons ;
• cout and << for output
• std namespace
• Escape characters, e.g. \n
19
20
1 // Fig. 2.4: fig02_04.cpp
2 // A first program in C++.
Single-line comments.
3 Function main
#include <iostream> returns an
4 integer { begins Preprocessor
value.
Left brace function directive to
5 // function main body. program
begins Function include input/output Statements
main appears
execution stream end with a
6 int main() header file <iostream>.
exactly once in every C++ semicolon ;.
7 { program..
8 std::cout << "Welcome to C++!\n";
9 Corresponding right brace }
10 return 0; //ends function
indicate body.
that program ended successfully
11 Name coutStream insertion
belongs to operator.
12 } // end function main namespace std.

Keyword return is one of


Welcome to C++!
several means to exit
function; value 0 indicates
program terminated
successfully.
Introduction to C++ Programming (Deitel)

Another Example

 Page 43, Fig. 2.5


 C++ features
• Declaring variables
• Input using cin and >>
• C++ supports many arithmetic operators
+, -, *, /, %
Precedence, p. 33: parentheses first; then *,/, and %;
and last are + and –. All from left to right.
• endl to output a newline and flush output
buffer
• << can be concatenated 21
22
1 // Fig. 2.5: fig02_05.cpp
2 // Addition program.
3 #include <iostream>
4
5 // function main begins program execution
6 int main()
7 { Declare integer variables.
8 int integer1; // first number to be input by user
9 int integer2; // second number to be input by user
10 int sum;
Use stream extraction
// variable in which sum will be stored
11 operator with standard input
12 std::cout << "Enter first stream to obtain
integer\n"; // user input.
prompt
13 std::cin >> integer1; // read an integer
14
15 std::cout << "Enter second integer\n"; // prompt
16 std::cin >> integer2; // read an integer
Calculations can be performed inStream
output manipulator
statements: alternative for
17
lines 18 and 20: std::endl outputs a
18 sum = integer1 + integer2; // assign result to sum
19
newline, then “flushes output
std::cout << "Sum is " << integer1 + integer2 << std::endl;
20 std::cout << "Sum is " << sum << std::endl; // print sumbuffer.”
21
22 return 0; // indicate that program ended successfully
23
24 } // end function main Concatenating, chaining or
cascading stream insertion
operations.
Introduction to C++ Programming (Deitel)

Equality Operators and


Relational Operators
 Page 53, Fig. 2.13
 C++ features
• if
• equality operator == (don’t confuse with = )
• operator !=
• operator <
• operator >
• operator <=
• operator >=
23
Introduction to
Classes and Objects

Chapter 3

24
Introduction to Classes and Objects (Deitel, 76)

Class

 A programming construct that contains


• “Data elements” that are related – they may have
different data types
• “Member functions” – all the operations that can
be performed with the data
 Definition
• Class = a collection of related elements with
different data types and the operations that can be
performed on them

25
Introduction to Classes and Objects (Deitel, 77–99)

The GradeBook Class

 Example of a class in Ch. 3


 Problem: We want to write a program that a teacher
can use to keep track of students’ test scores
 Solution: Create a GradeBook class that contains the
data and the functions for keeping track of the scores
• In this first example, the GradeBook class will:
– Store the name of the course in memory
– It also will have member functions to initialize the course name,
set the name, and print a message containing the name
• Later we’ll add the following features:
– Stores the students’ test scores in memory
– Sets the scores, displays the scores, calculates the class average
26
Introduction to Classes and Objects (Deitel, 96)

Declaring a Class
Name
class GradeBook{
public:
GradeBook(string name="CSCI 3233");
void setCourseName(string name); Member functions
string getCourseName(); (also called
void displayMessage(); operations)
private:
string courseName; Data Member
};
Remember the ‘;’ !

27
Introduction to Classes and Objects (Deitel, 96)

Access Specifiers
Access specifier All members after
class GradeBook{ “public:” are accessible
public: by any user in your
GradeBook(string name="CSCI 3233"); program wherever there
void setCourseName(string name); is an object of this class.
string getCourseName();
void displayMessage();
All members after
private: Access specifier “private:” are only
string courseName;
accessible to the member
};
functions of this class.
Class members are private
by default.

28
Introduction to Classes and Objects (Deitel, 76)

Information Hiding

 Data members are always made private


• The user of an object in your program cannot change the data by
accessing it directly
• So that we can control how the data can be changed
– Make sure that the new data is okay
– Verify that a requested change is allowed
– Example: An “courseID” member has to be an non-negative integer,
and it should not be a number that is already in use
• Also, so we can control how we store the data inside the class
– Example: We may decide to store the ID as a string instead of an int
 The user of an object can still get the data and set it, but
only by using a member function
• Example:
void setCourseID( int id );
29
Introduction to Classes and Objects (Deitel, 76)

Get and Set Member Functions

 Public member functions are provided to give


users access to the private data
 Member functions that return the data values
• Called “accessors”
• Usually named with the prefix “get”
string getCourseName();
 Member functions that change the data values
• Usually named with the prefix “set”
void setCourseName();
• Must ensure that the new value is appropriate
• May need to convert the argument to a different type
30
Introduction to Classes and Objects (Deitel, 99)

The Public Interface

 All of the public member functions are collectively called


the “public interface” of the class
 Includes all the operations that a user can do with the
class – sometimes called the “services”
 As long as the interface to your class never changes,
you can change the implementation inside your class
and your program will not break – easy to improve your
program
• You can change the way the data is stored, e.g. from an
unsorted array to a sorted array
• You can change the way the operation is performed, e.g. finding
a target with a binary search instead of a linear search
31
Introduction to Classes and Objects (Deitel, 90)

Encapsulation

 One of the principle advantages of object-oriented


programming
 Means that we keep both the data and the operations
that can be done with that data inside a single entity, the
class
 In addition, the user of the class has an outside view,
and has access only to the operations.
• The user knows what the class can do because the names of
the operations are public.
• But the user does not know how it is done because the
implementation of the class is not accessible.
• The programmer has the freedom to implement the operations
and change them in any way, as long as the public interface
does not change. 32
Introduction to Classes and Objects (Deitel, 96)

Defining the Member Functions

class GradeBook{
public:
GradeBook(string name="CSCI 3233"){
setCourseName(name);
}
void setCourseName(string name){
courseName = name;
}
string getCourseName(){
return courseName;
}
void displayMessage(){
cout << "GradeBook for " << getCourseName() << endl;
}
private:
string courseName;
33
};
Introduction to Classes and Objects (Deitel, 95)

Separating
the Class from main()
 In C++, this is the normal approach
• The class declaration and definition goes in a header
file (GradeBook.h)
• A preprocessor directive includes it in the cpp file
#include "GradeBook.h"
 Advantage
• Reusability – if the GradeBook class is in its own file,
it can be used in any program where we need it, just
by “including it”
 Not required – it is legal to put the class in the
same file as main()
34
Introduction to Classes and Objects (Deitel, 80)

Using a Class to Create an Object

 After a class is declared, it defines a new data


type
 We can use it to “declare a variable,” but we use
a different terminology. We say that a class is
used to create an “object” that is an instance of
the class.
 Instantiating an object
int main(){
GradeBook myGradeBook;
} Object
35
Introduction to Classes and Objects (Deitel, 93)

Constructor

 Public member function with the same name as the class


and with no return value
 Called automatically whenever an object is instantiated
 Initializes the data members
 Often, there are several overloaded constructors
GradeBook(){
setCourseName("CSCI 3233");
}

GradeBook(string name){
setCourseName(name);
}
36
Introduction to Classes and Objects (Deitel, 103)

The Main Function

#include <iostream>
#include "GradeBook.h"
using namespace std;

int main(){

GradeBook myFirstGradeBook;
cout << "GradeBook created for "
<< myFirstGradeBook.getCourseName() << endl;

GradeBook mySecondGradeBook("CSCI 3333");


cout << "GradeBook created for "
<< mySecondGradeBook.getCourseName() << endl;
}
37
References

Deitel, H. M., and P. J. Deitel, C++ How to Program, Fifth Edition.


Upper Saddle River, NJ: Prentice Hall, 2005.
Eckel, B., Thinking in C++, Second Edition. Upper Saddle River,
NJ: Prentice Hall, 2002.
Goodrich, M. T., R. Tamassia, and D. Mount, Data Structures and
Algorithms in C++. Hoboken, NJ: John Wiley & Sons, Inc., 2004.
Josuttis, Nicolai M., The C++ Standard Library, A Tutorial and
Reference. Boston: Addison-Wesley, 1999.
Randell, B., “Software Engineering in 1968,” Proceedings of the 4th
International Conference on Software Engineering. Piscataway,
NJ: IEEE Press, 1979.
Wirth, N., “On the Composition of Well-Structured Programs,”
Computing Surveys. Vol. 6, No. 4, December 1974.
38

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy