This presentation is a part of the COP2272C college level course taught at the Florida Polytechnic University located in Lakeland Florida. The purpose of this course is to introduce students to the C++ language and the fundamentals of object orientated programming..
The course is one semester in length and meets for 2 hours twice a week. The Instructor is Dr. Jim Anderson.
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
Intro To C++ - Class 2 - An Introduction To C++
1. An Introduction To Software
Development Using C++
Class #2:
An Introduction To
C++
2. What’s In A Name?
• The “C” programming language was developed by Dennis Ritchie at Bell
Laboratories 1969-1973. He wanted to port the Unix operating system from a
PDP-7 to a PDP-11 & he didn’t want to do it in assembly language.
• Yes, the “B” language already existed. However, it sucked so he created “C”
• C is available for most computers and is hardware independent. With careful
design, it’s possible to write C programs that are portable to most computers.
• The widespread use of C with various kinds of computers (sometimes called
hardware platforms) unfortunately led to many variations. A standard version of C
was needed.
• The American National Standards Institute (ANSI) cooperated with the
International Organization for Standardization (ISO) to standardize C worldwide;
the joint standard document was published in 1990 and is referred to as ANSI/ISO
9899: 1990.
3. What Is C++?
• C++, an extension of C, was developed by
Bjarne Stroustrup in the early 1980s at Bell
Laboratories.
• C++ provides a number of features that
“spruce up” the C language, but more
importantly, it provides capabilities for
object-oriented programming.
4. What Do You Have To Do To
“Learn” C++?
Learn the C++
Language
Learn how to use the
classes and functions
in the
C++ Standard Library
#1 #2
5. A Typical C++ Program
Development Environment
Step #1: Write the program!
// Text-printing program.
#include <iostream> // allows program to output data to the screen
// function main begins program execution
int main()
{
std::cout << "Welcome to C++!n"; // display message
return 0; // indicate that program ended successfully
} // end function main
6. A Typical C++ Program
Development Environment
Step #2: Preprocess the program
// Text-printing program.
#include <iostream> // allows program to output data to the screen
// function main begins program execution
int main()
{
std::cout << "Welcome to C++!n"; // display message
return 0; // indicate that program ended successfully
} // end function main
iostream
7. A Typical C++ Program
Development Environment
Step #3: Compile the C++ program
00000100 00010010 00010001 00100100
00000000 11111001 00000111 01010101
8. A Typical C++ Program
Development Environment
Step #4: Link the C++ program
00000100 00010010 00010001 00100100
00000000 11111001 00000111 01010101
00000100 00010010 00010001 00100100
00000000 11111001 00000111 01010101
00000100 00010010 00010001 00100100
00000000 11111001 00000111 01010101
00000100 00010010 00010001 00100100
00000000 11111001 00000111 01010101
9. A Typical C++ Program
Development Environment
Step #5: Load the C++ program
00000100 00010010 00010001 00100100
00000000 11111001 00000111 01010101
00000100 00010010 00010001 00100100
00000000 11111001 00000111 01010101
00000100 00010010 00010001 00100100
00000000 11111001 00000111 01010101
00000100 00010010 00010001 00100100
00000000 11111001 00000111 01010101
10. What’s Up With This Object
Orientated Stuff?
Procedural Languages
One long piece of code
where data and logic
are all mixed in
together.
1 #
2 # Sample program that demonstrates the print function.
3 #
4
5 # Prints 7
6 print(3 + 4)
7
8 # Prints “Hello World!” in two lines.
9 print("Hello")
10 print("World!")
11
12 # Prints multiple values with a single print function call.
13 print("My favorite numbers are", 3 + 4, "and", 3 + 10)
14
15 # Prints three lines of text with a blank line.
16 print("Goodbye")
17 print()
18 print("Hope to see you again")
11. What’s Up With This Object
Orientated Stuff?
• Each object represents a different part
of the application.
• Each object contains it’s own data & it’s
own logic.
• The objects communicate between
themselves.
• Objects are designed to represent how
you talk and think about the actual
problem.
• Objects are meant to make thinking
about your program easier.
• Object orientation is just an idea that is
supported by C++
12. I Know How To Program, Why
Bother With This C++ Stuff?
• Why is so much attention today focused on object-oriented programming
in general and C++ in particular?
ANSWER: Object-oriented programming enables the programmer to build
reusable software components that model items in the real world.
Building software quickly, correctly, and economically has been an elusive
goal in the software industry.
The modular, object-oriented design and implementation approach has
been found to increase productivity 10 to 100 times over conventional
programming languages while reducing development time, errors, and
cost. C++ is extremely popular because it is a superset of the widely used C
programming language. Programmers already familiar with C have an
easier time learning C++.
Image Credit: www.dreamstime.com
13. What are “Objects”?
Objects (both software and real)
have two ways that they can
be represented:
A. Attributes
B. Behaviors
14. What are “Objects”?
The gas pedal hides from the driver the complex
mechanisms that actually make the car go faster,
just as the brake pedal hides the mechanisms that
slow the car, and the steering wheel “hides” the
mechanisms that turn the car.
This enables people with little or no knowledge of
how engines, braking and steering mechanisms
work to drive a car easily.
Objects work the same way – they hide your code
from other developers so that they don’t have to
know HOW you did something, just what your
code DOES.
15. Member Functions and Classes
Performing a task in a program requires a member function, which houses
the program statements that actually perform its task. The member function
hides these statements from its user, just as the accelerator pedal of a car
hides from the driver the mechanisms of making the car go faster.
In C++, we create a program unit called a class to house the set of
member functions that perform the class’s tasks.
For example, a class that represents a bank account might contain one
member function to deposit money to an account, another to withdraw
money from an account and a third to inquire what the account’s current
balance is.
A class is similar in concept to a car’s engineering drawings, which house
the design of an accelerator pedal, steering wheel, and so on.
16. What Is A Class?
• It’s a blueprint, the definition, the description.
• It is not the thing itself!
Image Credit: www.chickslovethecar.com
18. Instantiation
You must build an object of a class before a program can perform the tasks
that the class’s member functions define.
The process of doing this is called instantiation. An object is then referred to
as an instance of its class.
19. Object
• The object is created from the class
• One class can create multiple objects
Image Credit: http://8z4.net/images/blueprint-/5.html
20. Reuse
You can reuse a class many times to build many objects.
Reuse of existing classes when building new classes and programs saves
time and effort.
Reuse also helps you build more reliable and effective systems, because
existing classes and components often have gone through extensive testing,
debugging and performance tuning.
21. Messages and
Member Function Calls
When you drive a car, pressing its gas pedal sends a message to the car to
perform a task—that is, to go faster.
Similarly, you send messages to an object.
Each message is implemented as a member function call that tells a member
function of the object to perform its task.
For example, a program might call a particular bank account object’s deposit
member function to increase the account’s balance.
22. Attributes and Data Members
A car, besides having capabilities to accomplish tasks, also has attributes,
such as its color, its number of doors, the amount of gas in its tank, its current
speed and its record of total miles driven (i.e., its odometer reading).
As you drive an actual car, these attributes are carried along with the car.
Every car maintains its own attributes. For example, each car knows how
much gas is in its own gas tank, but not how much is in the tanks of other
cars.
An object, similarly, has attributes that it carries along as it’s used in a
program. These attributes are specified as part of the object’s class. For
example, a bank account object has a balance attribute that represents the
amount of money in the account. Each bank account object knows the
balance in the account it represents, but not the balances of the other
accounts in the bank. Attributes are specified by the class’s data members.
23. What Does A Class Define?
Attributes
Name
Height
Weight
Gender
Age
Behavior
Walk
Run
Jump
Speak
Sleep
Let’s say that our
class is designed
to describe a
person…
24. Encapsulation
Classes encapsulate (i.e., wrap) attributes and member functions into
objects—an object’s attributes and member functions are intimately related.
Objects may communicate with one another, but they’re normally not allowed
to know how other objects are implemented—implementation details are
hidden within the objects themselves.
This information hiding, as we’ll see, is crucial to good software engineering.
26. Inheritance
A new class of objects can be created quickly and conveniently by
inheritance—the new class absorbs the characteristics of an existing class,
possibly customizing them and adding unique characteristics of its own.
In our car analogy, an object of class “convertible” certainly is an object of the
more general class “automobile,” but more specifically, the roof can be raised
or lowered.
27. Object-Oriented Analysis
and Design (OOAD)
To create the best solutions, you should follow a detailed analysis process for
determining your project’s requirements (i.e., defining what the system is supposed
to do) and developing a design that satisfies them (i.e., deciding how the system
should do it).
Ideally, you’d go through this process and carefully review the design (and have your
design reviewed by other software professionals) before writing any code.
If this process involves analyzing and designing your system from an object-oriented
point of view, it’s called an object-oriented analysis and design (OOAD) process.
Languages like C++ are object oriented. Programming in such a language, called
object-oriented programming (OOP), allows you to implement an object-oriented
design as a working system.
28. In-Class Fun:
Running A C++ Program
What you are going to be doing:
1. Locate and download the working C++ program that I’ve given you
2. Start up the C++ compiler
3. Compile the code
4. Run the code
5. Quit the compiler
29. What We Covered Today
1. Where did C / C++ come
from?
2. What happens when you
compile a C++ program?
3. What are “objects”?
4. Compiling and running
your very 1st C++
program.
Image Credit: http://www.tswdj.com/blog/2011/05/17/the-grooms-checklist/
30. What We’ll Be Covering Next Time
1. Taking apart our first
C++ program
2. Displaying data on the
screen
3. Output streams
Image Credit: http://merchantblog.thefind.com/2011/01/merchant-newsletter/resolve-to-take-advantage-of-these-5-e-commerce-trends/attachment/crystal-ball-fullsize/
31. An Introduction To Software
Development Using C++
Class #2:
Introduction To The
In-Class Programming
Assignment
32. Today’s In-Class C++
Programming Assignment
• Write a C++ program that will ask you to enter
your age. It will then print out the following
statement:
Hello, you have been alive for <x> days.
Assume every year has 365 days.
33. Answer To Today’s Challenge
// In-Class Exercise #1- Days Alive
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
int main()
{
int age;
cout << "Please Enter Your Age ";
cin >> age;
cout << "Hello, you have been alive for " << age * 365 << " days.";
return(0);
}
Image Credit: www.clipartpanda.com
35. What We Covered Today
1. Got our first in-class
programming
assignment.
2. Found out how many
days we’ve been alive.
3. Found out how many
days we’ll probably live
Image Credit: http://www.tswdj.com/blog/2011/05/17/the-grooms-checklist/
36. What We’ll Be Covering Next Time
1. How to find out which
number is larger?
Image Credit: http://merchantblog.thefind.com/2011/01/merchant-newsletter/resolve-to-take-advantage-of-these-5-e-commerce-trends/attachment/crystal-ball-fullsize/
Editor's Notes
New name for the class
I know what this means
Technical professionals are who get hired
This means much more than just having a narrow vertical knowledge of some subject area.
It means that you know how to produce an outcome that I value.
I’m willing to pay you to do that.
New name for the class
I know what this means
Technical professionals are who get hired
This means much more than just having a narrow vertical knowledge of some subject area.
It means that you know how to produce an outcome that I value.
I’m willing to pay you to do that.