Professional Documents
Culture Documents
Lesson On Variables and Datatypes
Lesson On Variables and Datatypes
Sciences
Introduction to C++
Programming
Object
•
Oriented programming
•
programming
•
language model organized
rather
data.
•
•
•
•
Riara University Lecture 2 2
Features of Object Oriented Programming (OOP)
now!
cout << “You entered: “ <<
number << endl;
return 0;
}
Riara University Lecture 2 27
Assignment
Assignment is an operation that assigns
the value of an expression to a variable
It operates/assigns from left to right
Ex.
Total = 2 + 3 + 5
First, the expresssion “2 + 3 + 5” is
evaluated
Then, this value is assigned to the
variable “Total”
Riara University Lecture 2 28
Assignment
When a variable is declared, space is
allocated in the computer’s memory for the
variable
Each data type requires a different number of
bytes in memory for storing a variable
int - 2
float - 4
double - 8
char, bool - 1
#include <iostream>
#include <cmath>
int main()
{
cout << "The reciprocal of 10 is " << 1.0/10.0 << endl;
cout << "The square root of 10 is " << sqrt(10.0) << endl;
cout << "e^(10.0) = " << exp(10.0) << endl;
cout << "The reciprocal of 15 is " << 1.0/15.0 << endl;
cout << "The square root of 15 is " << sqrt(15.0) << endl;
cout << "e^(15.0) = " << exp(15.0) << endl;
> mathExample2.exe
The reciprocal of 10 is 0.1
The square root of 10 is 3.16228
e^(10.0) = 22026.5
The reciprocal of 15 is 0.0666667
The square root of 15 is 3.87298
e^(15.0) = 3.26902e+06
>
#include <iostream>
#include <cmath>
int main()
{
double x;
x = 10.0;
cout << "The reciprocal of 10 is " << 1.0/x << endl;
cout << "The square root of 10 is " << sqrt(x) << endl;
cout << "e^(" << x << ") = " << exp(x) << endl;
x = 15.0;
cout << "The reciprocal of 15 is " << 1.0/x << endl;
cout << "The square root of 15 is " << sqrt(x) << endl;
cout << "e^(" << x << ") = " << exp(x) << endl;
int main()
{
int age;
float wage;
char initial;
double height;
a,b,c,..,z,A,B,C,…,Z or _.
Capitalized and lower case letters are different.
Examples:
int age; int age1;
int Age; int age2;
int myAge; int age3B;
int Jacks_age; int _age;
#include <iostream>
#include <cmath>
int main()
{
double x;
cout << "The reciprocal of " << x << " is " << 1.0/x << endl;
cout << "The square root of " << x << " is " << sqrt(x) << endl;
cout << "e^(" << x << ") = " << exp(x) << endl;
cout << "The reciprocal of " << x << " is " << 1.0/x << endl;
cout << "The square root of " << x << " is " << sqrt(x) << endl;
cout << "e^(" << x << ") = " << exp(x) << endl;
…
> mathExample4.exe
Enter x: 10.0
The reciprocal of 10 is 0.1
The square root of 10 is 3.16228
e^(10) = 22026.5
>
#include <iostream>
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
double x;
double y;
double z;
> xyz1.exe
Enter x, y and z: 1 2 3
x=1
y=2
z=3
>
> xyz1.exe
Enter x, y and z:
1
2
3
x=1
y=2
z=3
>
#include <iostream>
using namespace std;
int main()
{
double x, y, z; // multiple declarations
#include <iostream>
using namespace std;
int main()
{
double x, y, z;
> xyz1.exe
Enter x, y and z: 1 2 3
x=1
y=2
z=3
>
> xyz1.exe
Enter x, y and z:
1
2
3
x=1
y=2
z=3
>
int x, y, z;
#include <iostream>
using namespace std;
int main()
{
int x, y;
Try inputs:
cout << "Enter x and y: ";
cin >> x >> y; // Read in x and 17 3
then y
3 17
cout << "x = " << x << endl; 0 17
cout << "y = " << y << endl;
cout << "x+y = " << x+y << endl; 17 0
cout << "x/y = " << x/y << endl; 2000000000 2000000000
cout << "Done." << endl;
#include <iostream>
using namespace std;
int main()
{ Try inputs:
double x, y;
cout << "Enter x and y: "; 17 3
cin >> x >> y;
3 17
cout << "x = " << x << endl; 17 0
cout << "y = " << y << endl;
cout << "x+y = " << x+y << endl; 4000000000 4000000000
cout << “x*y = " << x*y << endl; 1e100 1e100
cout << “x/y = " << x/y << endl;
cout << "Done." << endl; 1e200 1e200
#include <iostream>
using namespace std;
int main()
{
char c1, c2, c3;
c3 = 'X';
> charExample1.exe
Enter first initial: R
Enter second initial: W
{What is the output?}
◦ "Hello"
◦ "Hello World!" (Note: Blank space is part of the
string.)
◦ "He who hesitates is lost.\nHaste makes waste.\
n"
◦ "" (The empty string.)
storage.
#include <iostream>
using namespace std;
int main()
{
cout << "5 % 3 = " << (5 % 3) << endl;
cout << "4 - 3 = " << (4 - 3) << endl;
cout << "5.0 / 2.0 = " << (5.0 / 2.0) << endl;
cout << "5 / 2 = " << (5 / 2) << endl;
return 0;
}
>arithmetic.exe
5%3=2
4-3=1
5.0 / 2.0 = 2.5
5/2=2
>