Visual C++ Programming – Penn Wu, PhD 222 Lecture #8 Object-Oriented Programming What is a class and what is an object? Assuming that the instructor found a new breed of “bear” and decided to name it “Kuma”. In an email, the instructor described what “Kuma” is by saying “Kuma is a new breed of bear. Kuma has two hands and two legs. Kuma eats, cries, and runs.” The following is the sample content of the email which is a pseudocode and is not written in any programming language. class Kuma { String specifies = "new breed of bear"; int numberOfHands = 2; int numberOfLegs = 2; void eat() { } void cry() { } void run() { } } In terms of object-oriented methodology, the instructor had declared a new class named “Kuma” with two kinds of members: appearance and behaviors. Number of “hands” and “legs” describe the appearance of “Kuma” while “eats”, “cries”, and “runs” describe the behaviors of “Kuma”. Now that “Kuma” has been defined, the instructors can simply say “Teddy” is a “Kuma”, “Cindy” is a “Kuma”, “Bunny” is a “Kuma”, “Loony” is a “Kuma”, and so on, without the need to define and explain the details of the “Kuma” over and over again. The following is the pseudocode that demonstrates how to create “instances” of “Kuma”. Kuma Teddy = new Kuma; Kuma Cindy = new Kuma; Kuma Bunny = new Kuma; Kuma Loony = new Kuma; “Teddy”, “Cindy”, “Bunny”, and “Loony” are instances of the “Kuma” class. They are said to be “objects” of the “Kuma” class. A “class”, in terms of object-oriented paradigm, is a structured way that defines what an “object” is. The process of declaration, creation, and definition of a class and its members is known as “abstraction”. After the “abstraction”, programmers can create an object as an instance of the class by using proper C++ code to say, “the object is an instance of the class”. This process is known as “instantiation”. The following table explains what “abstraction” and “instantiation” are. Term Object-Oriented Paradigm Real-life example Abstraction Class declaration, creation, and definition “Kuma” is a new breed of “bear”. Kuma has two hands and two legs. Kuma eats, cries, and runs. Instantiation Create an object of the class “Teddy” is a “Kuma”. “Cindy” is a “Kuma”.
34
Embed
Term Object-Oriented Paradigm Real-life examplestudents.cypresscollege.edu/cis223/lc08.pdf · Term Object-Oriented Paradigm Real-life example Abstraction Class declaration, creation,
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Visual C++ Programming – Penn Wu, PhD 222
Lecture #8 Object-Oriented Programming
What is a class
and what is an
object?
Assuming that the instructor found a new breed of “bear” and decided to name it “Kuma”. In
an email, the instructor described what “Kuma” is by saying “Kuma is a new breed of bear.
Kuma has two hands and two legs. Kuma eats, cries, and runs.” The following is the sample
content of the email which is a pseudocode and is not written in any programming language.
class Kuma
{
String specifies = "new breed of bear";
int numberOfHands = 2;
int numberOfLegs = 2;
void eat() { }
void cry() { }
void run() { }
}
In terms of object-oriented methodology, the instructor had declared a new class named
“Kuma” with two kinds of members: appearance and behaviors. Number of “hands” and
“legs” describe the appearance of “Kuma” while “eats”, “cries”, and “runs” describe the
behaviors of “Kuma”.
Now that “Kuma” has been defined, the instructors can simply say “Teddy” is a “Kuma”,
“Cindy” is a “Kuma”, “Bunny” is a “Kuma”, “Loony” is a “Kuma”, and so on, without the
need to define and explain the details of the “Kuma” over and over again. The following is the
pseudocode that demonstrates how to create “instances” of “Kuma”.
Kuma Teddy = new Kuma;
Kuma Cindy = new Kuma;
Kuma Bunny = new Kuma;
Kuma Loony = new Kuma;
“Teddy”, “Cindy”, “Bunny”, and “Loony” are instances of the “Kuma” class. They are said to
be “objects” of the “Kuma” class.
A “class”, in terms of object-oriented paradigm, is a structured way that defines what an
“object” is. The process of declaration, creation, and definition of a class and its members is
known as “abstraction”.
After the “abstraction”, programmers can create an object as an instance of the class by using
proper C++ code to say, “the object is an instance of the class”. This process is known as
“instantiation”. The following table explains what “abstraction” and “instantiation” are.
Term Object-Oriented Paradigm Real-life example
Abstraction Class declaration, creation,
and definition
“Kuma” is a new breed of “bear”. Kuma
has two hands and two legs. Kuma eats,
cries, and runs.
Instantiation Create an object of the class “Teddy” is a “Kuma”. “Cindy” is a
“Kuma”.
Visual C++ Programming – Penn Wu, PhD 223
Object-oriented programming (OOP) is a conceptual programming model (known as
“paradigm”) that organizes the definition of program objects (such as data and methods) to
enhance the modularity and reusability of coding. One simply analogy is to consider object-
oriented programming as a reference model that provides feasible framework to write
“modularized” codes that can be “plugged-in” to other programs to support features. A
wireless “mouse”, for example, is a “modularized” device that can be connected to any laptop
to provide the “pointing device” feature. “Can work with any laptop” is the “reusability” of the
wireless mouse as a “module”.
The rest of lecture will focus on the discussion of object-oriented programming written in
managed codes.
Abstraction and
instantiation in
C++
A “class” is a data structure that specifies both code and data of an “object”. Programmers
create a Visual C++ class and define “members” of the class for the sake of representing
essential features of a data. Once the class is created, they are used in a program as references
to describe what these “features” are. “Abstraction” is the act of representing an object, while
“instantiation” is the act of creating an object.
In Visual C++, the class keyword declares a class. The following is a generic form of class
declaration in Visual C++, where accressModifiers are options like “public”, “private”, and
“protected”; and type could be “ref” for managed reference type while identifier is a unique
name given to the class. The class body must be enclosed by a pair of curly braces and must be
ended with a semicolon (;).
accessModifier type class identifier
{
private: data and/or methods
public: data and/or methods
};
Classes in managed code must be declared as “reference” type. A “reference” type contains a
pointer to another memory location that holds the data. In other words, a “reference” type of
class needs a “pointer” to indicate the memory address that stores the code of a managed class
to the system. A reference class type must be prefaced with the “ref” keyword. By the way,
the default access modifier of a “reference” class is “public”.
The following is an example of “abstraction” written in managed code that define what a
“Car” is and provides two instance variables “doors” and “mpg”.
ref class Car
{
public:
int doors;
int mpg; // miles per gallon
};
The following is sample class of the “ref” type whose identifier is “Discount”. It has five
members: one variable named “dt” with “private” access modifier, two variables of float type
named “rate” and “price” with “public” modifier, and a method of float type named
“getDiscount()”. The “getDiscount()” method will perform an arithmetic calculation and return
the rounded result to its calling party. There is a special type of class method, known as
“constructor”. A later section will discuss about what “class constructors” are. In the following
example, the constructor is “Discount()” which has the same identifier as the “Discount” class.
By the way, “dt” is declared as DateTime type.
public ref class Discount
Visual C++ Programming – Penn Wu, PhD 224
{
private:
DateTime dt = DateTime::Now;
public:
float rate;
float price;
public:
Discoun() { rate=0.0; } //default constructor
float getDiscount()
{
return Math::Round(price * (1 - rate/100), 2);
}
};
Variables of the “Discount” class are known as “instance variable”, meaning that they will be
implemented by the “instance” of the class. Variables of a class are typically used to describe a
“state”, “property”, or “appearance” of an object. When defining a class, programmers declare
the data the class will contain and the code that operates on the data. Data is contained in
instance variables defined by the class, and code is contained in functions. The code and data
that constitute a class are called members of the class. The following is the syntax for
declaring instance variables, access-specifiers are options like “private”, “public”, and
“protected”.
Access-specifiers: DataType variableName;
The following is an example.
public: float rate;
While variables in a class are known as “instance variables”, and methods in a class can be
called “instance methods”. Methods of a class are used to define “behavior” of an object. The
following is the syntax for declaring an instance method.
Access-specifiers DataType methodName() { }
The following is an example.
public: float getDiscount() { }
Programmers use “access modifiers”, such as “private” and “public”, to specify the access
control of a class member; therefore “access modifiers” are also known as “access specifiers”.
The discussion of this lecture will focus on “public” and “private” access modifiers. A later
lecture will discuss the “protected” modifier in detail. The following table illustrates the
usages.
Modifier Scope Ownership public “public” class members can be access by any program
objects regardless the objects are inside or outside the
class.
Not specified
private “private” class members can only be accessed by members
of the same class.
The class
Visual C++ Programming – Penn Wu, PhD 225
The public modifier simply indicates that the entity can be accessed by code in any place of
the same program. If an entity is declared with a private modifier, it will be accessible only to
the code that is contained within the class that defines the entity.
In the following example, the “Student” class contains two group of members: private and
public. The private group consists of a double type of variable named “gpa”. The public group
is made of one String variables named “firstName”. Inside the “main()” function, the codes
attempt to access these two “instance variables”, “firstName” and “gpa”. The access to “gpa”
is denied because it is a “private” member of the “Student” class, while the access to
“firstName” is granted because its modifier is “public”.
#using <System.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Windows::Forms;
ref class Student
{
private:
double gpa;
public:
String^ firstName;
Student() { } //default constructor
};
int main()
{
Student^ s1;
s1 = gcnew Student();
s1->firstName = "Jennifer"; // allowed
s1->gpa = 3.5; // denied
}
The following figure illustrates how the “public” modifier keeps external code items from
accessing “private members”.
In Visual C++, declaring a “ref” class without specifying the “access modifier” will be
assumed to be “publicly declared”; therefore, the “public” modifier of the “Student” class can
be omitted.
With “public” modifier Without “public” modifier
public ref class Student { }
ref class Student { }
The next section will discuss the concept of “instantiation” in detail.
private: double gpa
Public: string^ fristName
int main() {
.........
s1->firstName = "Jennifer";
s1->gpa = 3.5;
}
NO
Visual C++ Programming – Penn Wu, PhD 226
What is an
object?
The term “object” is used in object-oriented programming to refer to an instance of a class. A
class in Visual C++ can be considered a new data type created by programmers. The definition
of the class describes the new data type, so an “object” can be treated as a variable of the new
data type. The following is the syntax to declare an instance of a class.
className objectID;
The following illustrates how to create an instance of the “Car” class.
ref class Car
{
public:
int doors;
int mpg; // miles per gallon
};
int main()
{
Car^ c1 = gcnew Car(); // instantiation
}
In reality, the instantiation takes two steps: declaration and creation. In order to create an
instance of a “reference” class, the class name must be followed by a caret sign (^) as a
“handle”. In Visual C++, a handle is a pointer to an object located on the Garbage Collection
heap. The following declares an instance c1 of the Car class, use:
Car^ c1;
The following statement demonstrates how to declare an instance of the “Student” class.
ref class Student { }
.......
Student^ s1; // declaration
Similar to the following statement that declares a variable named “x” of int type, the above
statement declares a variable named “s1” of the “Student” type.
int x;
A class declaration is only a type description; it does not actually create an object. Similar to
the following statement which instructs the computer to allocate a memory space to store the
value 15, programmers must issue a statement to instruct the computer to allocate memory
space for the “instance” of a “class” to use.
x = 15;
A “class” is said to be “abstract” because a class does not have any physical entity till an
object is declared and instantiated as its type. An “object” is said to be a physical
representation of the class that exists in memory. One analogy is to compare a “class” to a
blueprint of a building. A blueprint only illustrates the design plan with technical drawing, yet
it does not build anything. An “object” is one of the house that was built according to the
blueprint.
In Visual C++, memory for a managed type (reference or value type) is allocated by the
“gcnew” operator and deallocated by using garbage collection. The following bold-faced line
demonstrates how to “create” an “instance” after it has been declared as an “object” (named
“s1”) of the “Student” class. In other words, “s1” is “instantiated” as an object of the “Student”
class.
Visual C++ Programming – Penn Wu, PhD 227
ref class Student
{
..........
public:
Student() { }
.........
}
.......
Student^ s1; // declaration
s1 = gcnew Student(); // instantiation
As shown above, the “gcnew” operator calls the “default constructor” of the “Student” class.
In terms of object-oriented programming, a “constructor” is a “class method” of a special form
that is used by the compiler to create an “instance” of the class during the runtime. One
analogy to understand the role played by a “constructor” is to compare it to the construction
workers of a house-building project. While the architect draws the blueprint, the builder must
send construction workers to physically build the house.
Action Sample code House-building project
class definition ref class Student { }
draw the blueprint
instance
declaration
Student^ s1; approve to build the “s1”
unit of house
object creation s1 = gcnew Student(); launch the construction
The following demonstrates how to use the gcnew keyword to create an instance of the “Car”
class. Interestingly, the “gcnew” keyword can call either the “Car” class or its constructor to
create an instance of a managed type on the garbage collected heap. The follows call the
default constructor.
c1 = gcnew Car();
or, simply call the “Car” class and let the “Car” class calls its default constructor.
c1 = gcnew Car;
Often, programmers will declare and instantiate an instance in one single lien of statement, as
shown below.
Student^ s1 = gcnew Student();
Another example,
Car^ c1 = gcnew Car();
or,
Car^ c1 = gcnew Car;
A class is a data type; therefore, programmers can create an array of instance of a given class.
In other words, a C++ class can be instantiated as an array with every element of the array
using the class as data type. The following illustrates how to create an array of the “Car” class.
int main()
{
Visual C++ Programming – Penn Wu, PhD 228
array<Car^>^ c = gcnew array<Car^> (3); // an array of Car
c[0] = gcnew Car; // first object
c[1] = gcnew Car; // second object
c[2] = gcnew Car; // third object
}
The following is a sample class named “Rectangle”.
#using <System.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Windows::Forms;
ref class Rectangle
{
public:
int w, h;
Rectangle(int _w, int _h)
{
w = _w; h = _h;
}
};
The following a sample “main()” function that declare “rt” as an array of the “Rectangle”
class. In other words, “rt” uses “Rectangle” as data type. Each element of “rt” is an instance of
the “Rectangle” class with “rt[0]” being the first instance, “rt[1]” being the second instance,