140 likes | 221 Views
Subject: Programming in C ++. Chapter 1 Introduction. LEARNING OUTCOME. At the end of this slide, student able to: Know the brief history of C++. Explore Microsoft Visual C++ Create applications using Variables, Data Types and Arithmetic expressions. History & fundemental of C++.
E N D
Subject: Programming in C++ Chapter 1 Introduction
LEARNING OUTCOME • At the end of this slide, student able to: • Know the brief history of C++. • Explore Microsoft Visual C++ • Create applications using Variables, Data Types and Arithmetic expressions.
History & fundemental of C++ • C, C++, Java, and C# are very similar. C++ evolved from C. Java was modeled after C++. C# is a subset of C++ with some features similar to Java. If you know one of these languages, it is easy to learn the others. • C evolved from the B language and the B language evolved from the BCPL language. BCPL was developed by Martin Richards in the mid-1960s for writing operating systems and compilers. • C++ is an extension of C, developed by BjarneStroustrup at Bell Labs during 1983-1985. C++ added a number of features that improved the C language. • An international standard for C++ was created by American National Standards Institute (ANSI) in 1998.
Example 1 Let us begin with a simple C++ program that displays the message “Welcome to C++!” on the console. START #include <iostream> using namespace std; int main() { // Display Welcome to C++ to the console cout << "Welcome to C++!" << endl; System(“pause”); // this is needed within visual studio return 0; } Step1: Display show: “Welcome to C++ FINISH
Example 2 #include <iostream> using namespace std; int main() { cout << "Programming is fun!" << endl; cout << "Fundamentals First"; // what happens when no “endl”? cout << "Problem Driven" << endl; System(“pause”); return 0; } START Step1: Display show: “Programming is fun!” Step 2: Display show: “Fundamental First” Step 4: Display show: “Problem Driven” FINISH
Example 3 #include <iostream> using namespace std; int main() { string name, age; cout<< “Type your name" << endl; cin>> name>>; cout<< “Type your age” << endl; cin>>age>>; cout<< “My name is”<<name<<endl; cout << “My age is”<<age<<endl; System(“pause”); return 0; } START Step1: Display show: “Type your name” Step 2: Computer ask to key in your name Step 3: Display show: “My name is” <yourname> Display show: “My age is” <yourage> FINISH
Example 4 #include <iostream> using namespace std; int main() { int first, second; cout<< “Welcome to math " << endl; cout<< “Type for first number” << endl; cin>>first>>; cout<< “Type for second number”<<second<<endl; cin>> second>>; cout << “The total is”<<first + second <<endl; System(“pause”); return 0;} START Step1: Display show: “Welcome to math” & “Type for first number” Step 2: Computer ask to key in first number Step 3: Display show: “Type for second number” Step 4: Display show: “The total is” <total> FINISH
animation Trace a Program Execution allocate memory for radius #include <iostream> using namespace std; int main() { double radius; double area; // Step 1: Read in radius radius = 20; // Step 2: Compute area area = radius * radius * 3.14159; // Step 3: Display the area cout << "The area is "; cout << area << endl; } radius no value
animation Trace a Program Execution memory #include <iostream> using namespace std; int main() { double radius; double area; // Step 1: Read in radius radius = 20; // Step 2: Compute area area = radius * radius * 3.14159; // Step 3: Display the area cout << "The area is "; cout << area << std::endl; } radius no value area no value allocate memory for area
animation Trace a Program Execution assign 20 to radius #include <iostream> using namespace std; int main() { double radius; double area; // Step 1: Read in radius radius = 20; // Step 2: Compute area area = radius * radius * 3.14159; // Step 3: Display the area cout << "The area is "; cout << area << std::endl; } radius 20 area no value Note: “=“ is not “equals” but an “assignment” i.e., “take the value of the expression on the right and stick it in the memory location on the left.” Think <=
animation Trace a Program Execution #include <iostream> using namespace std; int main() { double radius; double area; // Step 1: Read in radius radius = 20; // Step 2: Compute area area = radius * radius * 3.14159; // Step 3: Display the area cout << "The area is "; cout << area << std::endl; } memory radius 20 area 1256.636 compute area and assign it to variable area
animation Trace a Program Execution #include <iostream> using namespace std; int main() { double radius; double area; // Step 1: Read in radius radius = 20; // Step 2: Compute area area = radius * radius * 3.14159; // Step 3: Display the area cout << "The area is "; cout << area << std::endl; } memory radius 20 area 1256.636 print a message to the console
START EXERCISE IN CLASS Step1: Display show: “What is your name?” By given flowchat, create a C++ programming that can calculate BMI (Body Mass Index), (BMI=weight/height) Step 2: Computer ask to key in your name Step 3: Display show: “what is your birth year?” Step 4: Computer ask to key in your birth year Step 5: Display show: “what is your weight?” Step 6: Computer ask to key in your weight Step 7: Display show: “what is your height?” Step 8: Computer ask to key in your height Step 9: Display show: “the BMI of<your name> is <your BMI> and age is <your age> FINISH