Professional Documents
Culture Documents
Unit 2: Beginning With C++
Unit 2: Beginning With C++
#include <iostream>
using namespace std;
int main()
{
cout << “Every age has a language of its own\n”;
return 0;
}
• The identifier cout (pronounced “C out”) is actually an object. It is
predefined in C++ to correspond to the standard output stream. A
stream is an abstraction that refers to a flow of data.
<< as the left-shift bit-wise operator and wonder how it can also be
used to direct output. In C++, operators can be overloaded. Performing
different activities, depending on the context.
#include <iostream.h> : Pre-processor directive, directs compiler to include
header file iostream.h
You must declare a variable before using it. However, you can place
variable declarations anywhere in a program.
It’s not necessary to declare variables before the first executable
statement.
Declarations and Definitions
• A declaration introduces a variable’s name (such as var1) into a program
and specifies its type (such as int). However, if a declaration also sets aside
memory for the variable, it is also called a definition. The statements
• int var1;
• int var2;
are definitions, as well as declarations, because they set aside memory for
var1 and var2.
Variables are also called Identifiers.
Can use upper and lower-case letters, underscore ( _ ) and digits.
#include <iostream>
using namespace std;
int main()
{
int ftemp; //for temperature in fahrenheit
cout << “Enter temperature in fahrenheit: “;
cin >> ftemp;
int ctemp = (ftemp-32) * 5 / 9;
cout << “Equivalent in Celsius is: “ << ctemp << ‘\n’;
return 0;
}
Output:
Enter temperature in fahrenheit: 212
Equivalent in Celsius is: 100
Arithmetic Operators
?
Prefix
Increment
operator
Postfix
Increment
operator
Library Functions
Operator Meaning
> Greater than (greater than)
< Less than
== Equal to
!= Not equal to
>= Greater than or equal to
<= Less than or equal to
Loops
#include <iostream>
using namespace std;
int main()
{
int j; //define a loop variable
for(j=0; j<15; j++)
cout << j * j << “ “;
cout << endl;
return 0;
}
Output
0 1 4 9 16 25 36 49 64 81 100 121 144 169 196
#include <iomanip>
using namespace std;
int main()
{
int numb; //define loop variable
for(numb=1; numb<=10; numb++) //loop from 1 to 10
{
cout << setw(4) << numb; //display 1st column
int cube = numb*numb*numb; //calculate cube
cout << setw(6) << cube << endl; //display 2nd column
}
return 0;
}
#include <iomanip>
using namespace std;
int main()
{
int numb; //define loop variable
for(numb=1; numb<=10; numb++) //loop from 1 to 10
{
cout << setw(4) << numb; //display 1st column
int cube = numb*numb*numb; //calculate cube
cout << setw(6) << cube << endl; //display 2nd column
}
return 0;
}
1 1
2 8
3 27
4 64
5 125
6 216
7 343
8 512
9 729
10 1000
Decisions
1. if...else statement, which chooses between two alternatives. This statement
can be used without the else, as a simple if statement.
2. switch, creates branches for multiple alternative sections of code, depending
on the value of a single variable.
3. Conditional operator
• The data items in a structure are called the members of the structure.
struct student
{
int rollno;
char name[20];
int phno;
};
student s1,s2;
s1.rollno=200101;
Structures Within Structures
struct Distance
{
int feet;
float inches;
};
struct Room
{
Distance length;
Distance width;
};
Room dining;
dining.length.feet=10;
dining.length.inches=2.5;
dining.width.feet=5;
dining.width.inches=1.5;
• Structured programming
• Reduce program size
• Function code is stored in only
one place in memory
Passing Arguments to Functions
• An argument is a piece of data (an int value, for example) passed from a program to the
function.
• Arguments allow a function to operate with different values, or even to do different
things, depending on the requirements of the program calling it.
• In the next program, TableArg, includes a function. In this program, arguments are
used to pass the character to be printed and the number of times to print it.
Reference Arguments
• A reference provides an alias—a different name—for a variable.
• When arguments are passed by value, the called function creates a new variable of the
same type as the argument and copies the argument’s value into it.
• The function cannot access the original variable in the calling program, only the copy it
created. Passing arguments by value is useful when the function does not need to
modify the original variable in the calling program.
• Passing arguments by reference uses a different mechanism. Instead of a value being
passed to the function, a reference to the original variable, in the calling program, is
passed.
• In this case, the memory address of the variable is passed.
//Program TableArg
// function definition
void repchar(char ch, int n) //function
#include <iostream> declarator
using namespace std; {
void repchar(char, int); //function declaration for(int j=0; j<n; j++) //function body
int main() cout << ch;
{ cout << endl;
repchar(‘-’, 43); //call to function }
cout << “Data type Range” << endl;
repchar(‘=’, 23); //call to function
order(n3, n4);
cout << “n1=” << n1 << endl; //print all nos
cout << “n2=” << n2 << endl;
cout << “n3=” << n3 << endl;
cout << “n4=” << n4 << endl;
return 0;
}
Pass by Reference
#include <iostream> //orders two numbers
order(n3, n4);
cout << “n1=” << n1 << endl; //print all nos Output: Output:
cout << “n2=” << n2 << endl; Before Call After Call
n1=99 n1=11
cout << “n3=” << n3 << endl; n2=11 n2=99
cout << “n4=” << n4 << endl; n3=22 n3=22
return 0; n4=88 n4=88
}
Defining Class
Private and Public
Data hiding
• Data is concealed
into a class,
• Doesn’t allow access
by outside functions
• Private Data and
functions are hidden
from other classes or
parts of the program
Using class
Creating object/ Instantiating object/ Instance variables
• If we do not specify any access modifiers for the members inside the class
then by default the access modifier for the members will be Private.
Access Specifiers or Modifiers : Public
Public:
• All the class members declared under public will be available to everyone.
• The data members and member functions declared public can be accessed by other classes
too.
• The public members of a class can be accessed from anywhere in the program using the
direct member access operator (.) with the object of that class.
};
#include <iostream>
int main()
using namespace std;
{
class Rectangle
Rectangle rt1, rt2;
{
rt1. Set_Data(7, 4);
private:
rt2. Set_Data(4, 5);
int length, breadth;
cout << "Area 1st rect:" << rt1.printArea() << endl;
public:
cout << "Area 2nd rect:" << rt2.printArea() << endl;
void Set_Data( int l, int b )
return 0;
{
}
length = l;
breadth = b;
}
int printArea()
{
return length * breadth;
}
};
#include <iostream>
int main()
using namespace std;
{
class Rectangle
Rectangle rt1, rt2;
{
rt1. Set_Data(7, 4);
private:
rt2. Set_Data(4, 5);
int length, breadth;
cout << "Area 1st rect:" << rt1.printArea() << endl;
public:
cout << "Area 2nd rect:" << rt2.printArea() << endl;
void Set_Data( int l, int b )
return 0;
{
}
length = l;
breadth = b;
}
int printArea() Output:
{ Area of 1st rect:28
return length * breadth; Area of 2nd rect:20
}
};
Array
1-D Array
2-D Array
Linear Search
• Suppose we have 50 students in a class and we have to
input the name and marks of all the 50 students.
The code of the function is same in both the cases, but the
function header is different
Inside Class Definition:
• When a member function is defined inside a class, we do not
require to place a membership label along with the function
name. We use only small functions inside the class definition
and such functions are known as inline functions.
To declare all member functions of class Class2 as friends of class Class1, place following
declaration in the definition of class ClassOne −