1.C++ Getting Started
1.C++ Getting Started
Phenikaa University
▶ Lectures: 14 classes
▶ Assessment: consisting of multiple assignments in class during the course and two
exams (midterm and final exams)
▶ 10% CC, 30% midterm, and 60% final
Course Material
▶ Teach/learn:
▶ Fundamental programming concepts
▶ Key useful techniques
▶ Basic Standard C++ facilities
▶ Object-oriented programming
▶ After the course, you’ll be able to
▶ Write small C++ programs for scientific computations
▶ Learn the basics of many other languages by yourself
▶ Read much larger programs
▶ After the course, you will not (yet) be
▶ An expert programmer
▶ A C++ language expert
▶ An expert user of advanced libraries
Motivation
#include <iostream>
int main () // main () is where a C ++ program starts
{
std::cout << "Hello world !\n" ;
// output the 13 characters Hello world !
// followed by a new line
return 0;
// return a value indicating success
}
Compilation and Linking
compiler.png
▶ You write C++ source code. Source code is (in principle) human readable
▶ The compiler translates what you wrote into object code (sometimes called
machine code)
Object code is simple enough for a computer to “understand”
▶ The linker links your code to system code needed to execute
E.g., input/output libraries, operating system code, and windowing code
▶ The result is an executable program: E.g., a .exe file on windows or an a.out file
on Unix
Building a Program
Example: a simple program consisting of one single file, mycode.cpp, using the GNU
C++ compiler on a Unix or Linux system
./myprogram
A First Look at Input/Output
A program that uses the IO library
#include <iostream>
int main()
{
std::cout << "Enter two numbers:" << std::endl;
int v1 = 0, v2 = 0;
std::cin >> v1 >> v2;
std::cout << "The sum of " << v1 << " and " << v2
<< " is " << v1 + v2 << std::endl;
return 0;
}
Writing to a Stream
Reading from a Stream
Using Names from the Standard Library
▶ Careful readers will note that this program uses std::cout and std::endl rather than
just cout and endl.
▶ The prefix std:: indicates that the names cout and endl are defined inside the
namespace named std.
▶ Namespaces allow us to avoid inadvertent collisions between the names we define
and uses of those same names inside a library. All the names defined by the
standard library are in the std namespace.
▶ We can get rid of that by using
#include <iostream>
/*
* Simple main function:
* Read two numbers and write their sum
*/
int main()
{
// prompt user to enter two numbers
std::cout << "Enter two numbers:" << std::endl;
int v1 = 0, v2 = 0;
// variables to hold the input we read
std::cin >> v1 >> v2; // read input
std::cout << "The sum of " << v1 << " and " << v2
<< " is " << v1 + v2 << std::endl;
return 0;
}
Comments in C++
#include <iostream>
int main()
{
int number;
std::cout << "Enter an integer: ";
std::cin >> number;
// checks if the number is positive
if (number > 0)
std::cout << "You entered a positive integer: " << number <<
endl;
#include <iostream>
int main()
{
int sum = 0, val = 1;
while (val <= 10) {
sum += val; // assigns sum + val to sum
++val;
}
std::cout << "Sum of 1 to 10 inclusive is "
<< sum << std::endl;
return 0;
}
Flow of Control: for loop
the pattern—using a variable in a condition and incrementing that variable in the
body—happens so often that the language defines a second statement, the for
statement, that abbreviates code that follows this pattern. We can rewrite this
program using a for loop to sum the numbers from 1 through 10 as follows:
#include <iostream>
int main()
{
int sum = 0;
for (int val = 1; val <= 10; ++val)
sum += val; // equivalent to sum = sum + val
std::cout << "Sum of 1 to 10 inclusive is "
<< sum << std::endl;
return 0;
}
Example: Reading an Unknown Number of Inputs
#include <iostream>
int main()
{
int sum = 0, value = 0;
// read until end-of-file, calculating a running total of all
values read
while (std::cin >> value)
sum += value; // equivalent to sum = sum + value
std::cout << "Sum is: " << sum << std::endl;
return 0;
}
If the stream is valid—that is, if the stream hasn’t encountered an error—then the test
succeeds. An istream becomes invalid when we hit end-of-file or encounter an invalid
input, such as reading a value that is not an integer. An istream that is in an invalid
state will cause the condition to yield false.
Several cool things about C++ over C
▶ bool
▶ string
▶ vector
▶ struct/class member functions
In the next lecture
Will talk more about types, values, variables, declarations, input and output streams.