0% found this document useful (0 votes)
6 views46 pages

Ders 6

The document discusses the concepts of Object-Oriented Programming (OOP) using the GradeBook class as an example. It covers class definitions, member functions, data members, access specifiers, and how to create and manipulate objects in C++. Additionally, it introduces UML class diagrams and emphasizes the importance of encapsulation and data hiding in OOP.

Uploaded by

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

Ders 6

The document discusses the concepts of Object-Oriented Programming (OOP) using the GradeBook class as an example. It covers class definitions, member functions, data members, access specifiers, and how to create and manipulate objects in C++. Additionally, it introduces UML class diagrams and emphasizes the importance of encapsulation and data hiding in OOP.

Uploaded by

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

Yrd.Doç.Dr.

Zeynep ORMAN
[email protected]
} Bu dersten itibaren daha önce öğrendiğimiz
«Nesneye Yönelik Programlama»
kavramlarını kullanarak programlar yazacağız.
} Yazacağımız programlar temel olarak main
fonksiyonu ve her biri veri üyeleri (data
members) ve üye fonskiyonlar (member
functions) içeren bir ya da daha fazla sınıfı
içerecektir.
} We begin with an example that consists of class
GradeBook (lines 8–16), represents a grade book that
an instructor can use to maintain student test scores,
and a main function (lines 19–23) that creates a
GradeBook object.
} Function main uses this object and its
displayMessage member function to display a
message on the screen welcoming the instructor to the
grade-book program.
} The GradeBook class definition (lines 8–16) begins with
keyword class and contains a member function called
displayMessage (lines 12–15) that displays a message
on the screen (line 14).
} Need to make an object of class GradeBook (line 21) and
call its displayMessage member function (line 22) to
get line 14 to execute and display the welcome message.
} The class definition begins with the keyword class
followed by the class name GradeBook.
◦ By convention, the name of a user-defined class begins with a
capital letter, and for readability, each subsequent word in the
class name begins with a capital letter.
◦ Often referred to as Pascal case.
◦ The occasional uppercase letters resemble a camel’s humps.
More generally, camel case capitalization style allows the first
letter to be either lowercase or uppercase
} Every class’s body is enclosed in a pair of left and right
braces ({ and }), as in lines 9 and 16.
} The class definition terminates with a semicolon (line
16).
} Function main is always called automatically when
you execute a program.
} Most functions do not get called automatically.
} You must call member function displayMessage
explicitly to tell it to perform its task.
} The access-specifier label public: contains the keyword
public is an access specifier.
◦ Indicates that the function is “available to the public”—that is,
it can be called by other functions in the program (such as
main), and by member functions of other classes (if there are
any).
◦ Access specifiers are always followed by a colon (:).
} Each function in a program performs a task and may return a
value when it completes its task.
} When you define a function, you must specify a return type to
indicate the type of the value returned by the function when it
completes its task.
} Keyword void to the left of the function name
displayMessage is the function’s return type.
◦ Indicates that displayMessage will not return any data to its calling
function when it completes its task.
} The name of the member function, displayMessage, follows
the return type.
} By convention, our function names use the camel case style with
a lowercase first letter.
} The parentheses after the member function name indicate that it
is a function.
} Empty parentheses indicate that a member function
does not require additional data to perform its task.
} The first line of a function definition is commonly
called the function header.
} Every function’s body is delimited by left and right
braces ({ and }).
} The function body contains statements that perform the
function’s task.
Testing Class GradeBook
}Typically, you cannot call a member function of a class until
you create an object of that class.
}First, create an object of class GradeBook called
myGradeBook.
◦ The variable’s type is GradeBook.
◦ The compiler does not automatically know what type
GradeBook is—it’s a user-defined type.
◦ Tell the compiler what GradeBook is by including the
class definition.
◦ Each class you create becomes a new type that can be used
to create objects.
} Call the member function displayMessage- by using
variable myGradeBook followed by the dot operator
(.), the function name displayMessage and an empty
set of parentheses.
} Causes the displayMessage function to perform its
task.
UML Class Diagram for Class GradeBook
}In the UML, each class is modeled in a UML class diagram as a
rectangle with three compartments.
}Figure 3.2 presents a class diagram for class GradeBook (Fig. 3.1).
}The top compartment contains the class’s name centered horizontally
and in boldface type.
}The middle compartment contains the class’s attributes, which
correspond to data members in C++.
◦ Currently empty, because class GradeBook does not yet have any attributes.
}The bottom compartment contains the class’s operations, which
correspond to member functions in C++.
}The UML models operations by listing the operation name followed by a
set of parentheses.
}The plus sign (+) in front of the operation name indicates that
display-Message is a public operation in the UML.
} Car analogy
◦ Pressing a car’s gas pedal sends a message to the car to
perform a task—make the car go faster.
◦ But how fast should the car accelerate? As you know, the
farther down you press the pedal, the faster the car accelerates.
◦ The message to the car includes both the task to perform and
additional information that helps the car perform the task.
} Additional information that a function needs to perform
its task is known as a parameter.
} A function call supplies values—called arguments—for
each of the function’s parameters.
} Fig. 3.3 redefines class GradeBook (lines 9–18) with a
displayMessage member function (lines 13–17) that
displays the course name as part of the welcome message.
◦ The new version of displayMessage requires a parameter
(courseName in line 13) that represents the course name to output.
} A variable of type string represents a string of characters.
} A string is actually an object of the C++ Standard Library
class string.
◦ Defined in header file <string> and part of namespace std.
◦ For now, you can think of string variables like variables of other
types such as int.
} Library function getline reads a line of text into a string.
} The function call getline( cin, nameOfCourse )
reads characters (including the space characters that
separate the words in the input) from the standard input
stream object cin (i.e., the keyboard) until the newline
character is encountered, places the characters in the
string variable nameOfCourse and discards the
newline character.
} When you press Enter while entering data, a newline is
inserted in the input stream.
} The <string> header file must be included in the
program to use function getline.
} Line 33 calls myGradeBook’s displayMessage
member function.
◦ The nameOfCourse variable in parentheses is the argument
that is passed to member function displayMessage so that
it can perform its task.
◦ The value of variable nameOfCourse in main becomes the
value of member function displayMessage’s parameter
courseName in line 13.
} To specify that a function requires data to perform its task, you
place additional information in the function’s parameter list,
which is located in the parentheses following the function name.
} The parameter list may contain any number of parameters,
including none at all to indicate that a function does not require
any parameters.
} Each parameter must specify a type and an identifier.
} A function can specify multiple parameters by separating each
parameter from the next with a comma.
} The number and order of arguments in a function call must match
the number and order of parameters in the parameter list of the
called member function’s header.
} The argument types in the function call must be consistent with
the types of the corresponding parameters in the function header.
} The UML class diagram of Fig. 3.4 models class
GradeBook of Fig. 3.3.
} The UML models a parameter by listing the parameter
name, followed by a colon and the parameter type in
the parentheses following the operation name.
} The UML has its own data types similar to those of C+
+.
} The UML is language independent—it’s used with
many different programming languages—so its
terminology does not exactly match that of C++.
} Variables declared in a function definition’s body are
known as local variables and can be used only from the
line of their declaration in the function to the closing
right brace (}) of the block in which they’re declared.
◦ A local variable must be declared before it can be used in a
function.
◦ A local variable cannot be accessed outside the function in
which it’s declared.
◦ When a function terminates, the values of its local variables
are lost.
} An object has attributes that are carried with it as it’s used
in a program.
◦ Such attributes exist throughout the life of the object.
◦ A class normally consists of one or more member functions that
manipulate the attributes that belong to a particular object of the
class.
} Attributes are represented as variables in a class definition.
◦ Such variables are called data members and are declared inside a
class definition but outside the bodies of the class’s member-function
definitions.
} Each object of a class maintains its own attributes in
memory.
} A typical instructor teaches several courses, each with
its own course name.
} A variable that is declared in the class definition but
outside the bodies of the class’s member-function
definitions is a data member.
} Every instance (i.e., object) of a class contains each of
the class’s data members.
} A benefit of making a variable a data member is that all
the member functions of the class can manipulate any
data members that appear in the class definition.
#include <iostream>
#include <string>
using namespace std;

//Sınıf Tanımlıyoruz
class GradeBook
{
private://Veri üyesi tanımladık
string courseName;
//Üye fonksiyonları tanımlıyoruz
public:
void setCourseName(string name)
{
courseName = name;
}

©1992-2014 by Pearson Education, Inc.


All Rights Reserved.
void displayMessage() const
{
cout<<"GradeBook Uygulamasina
Hosgeldiniz
\n"<<getCourseName()<<endl;
}

};//Sınıf tanımı sonu

int main()
{
GradeBook myObject;
string DersAdi;

cout<<"Ders ismi: "<<myObject.getCourseName()<<endl;

cout<<"\nDers ismi giriniz: "<<endl;


getline(cin,DersAdi);

©1992-2014 by Pearson Education, Inc.


All Rights Reserved.
myObject.setCourseName(DersAdi);

cout<<endl;

myObject.displayMessage();

return 0;
}

©1992-2014 by Pearson Education, Inc.


All Rights Reserved.
} Most data-member declarations appear after the access-
specifier label private:
} Like public, keyword private is an access specifier.
} Variables or functions declared after access specifier
private (and before the next access specifier) are
accessible only to member functions of the class for which
they’re declared.
} The default access for class members is private so all
members after the class header and before the first access
specifier are private.
} The access specifiers public and private may be
repeated, but this is unnecessary and can be confusing.
} Declaring data members with access specifier
private is known as data hiding.
} When a program creates (instantiates) an object, its data
members are encapsulated (hidden) in the object and
can be accessed only by member functions of the
object’s class.
} In this example, setCourseName does not attempt to
validate the course name—i.e., the function does not check
that the course name adheres to any particular format or
follows any other rules regarding what a “valid” course
name looks like.
◦ Suppose, for instance, that a university can print student transcripts
containing course names of only 25 characters or fewer.
◦ In this case, we might want class GradeBook to ensure that its data
member courseName never contains more than 25 characters.
} When a function that specifies a return type other than
void is called and completes its task, the function uses a
return statement to return a result to its calling function.
} Member function displayMessage (lines 26–32) does not
return any data when it completes its task, so its return type is
void.
} The function does not receive parameters, so its parameter list is
empty.
} Line 30 calls member function getCourseName to obtain the
value of courseName.
◦ Member function displayMessage could also access data member
courseName directly, just as member functions setCourseName
and getCourseName do.
} By default, the initial value of a string is the so-called empty
string, i.e., a string that does not contain any characters.
} Nothing appears on the screen when an empty string is displayed.
} A client of an object—that is, any class or function that calls the
object’s member functions from outside the object—calls the
class’s public member functions to request the class’s services
for particular objects of the class.
◦ This is why the statements in main call member functions
setCourseName, getCourseName and displayMessage on a
GradeBook object.
} Classes often provide public member functions to allow
clients of the class to set (i.e., assign values to) or get (i.e., obtain
the values of) private data members.
◦ These member function names need not begin with set or get, but this
naming convention is common.
} Set functions are also sometimes called mutators (because they
mutate, or change, values), and get functions are also sometimes
called accessors (because they access values).
} Figure 3.6 contains an updated UML class diagram for
the version of class GradeBook in Fig. 3.5.
} The UML represents data members as attributes by
listing the attribute name, followed by a colon and the
attribute type.

You might also like