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
• 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 //Englis
{
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 -------------------------------------------
Data type Range
cout << “char -128 to 127” << endl =======================
<< “short -32,768 to 32,767” << endl char -128 to 127
<< “int System dependent” << endl short -32,768 to 32,767
<< “long -2,147,483,648 to int System dependent
2,147,483,647” << endl; long -2,147,483,648 to 2,147,483,647
-------------------------------------------
repchar(‘-’, 43); //call to function
return 0;
}
// demonstrates variable arguments :VarArg.cpp
#include <iostream> Enter a character: +
using namespace std; Enter number of times to repeat it: 20
void repchar(char, int); //function declaration ++++++++++++++++++++
int main()
{ The data types of variables used as
char chin; arguments must match those specified in
int nin; the function declaration and definition.
cout << “Enter a character: “;
cin >> chin; In VarArg.cpp the particular values
cout << “Enter number of times to repeat
possessed by chin and nin when the
it: “;
function call is executed will be passed to
cin >> nin;
the function. The function gives these new
repchar(chin, nin);
return 0; variables the names and data types of the
} parameters specified in the declarator:
// function definition ch of type char and n of type int.
void repchar(char ch, int n) //function declarator
{ It initializes these parameters to the values
for(int j=0; j<n; j++) //function body passed.
cout << ch; They are then accessed like other variables
cout << endl; by statements in the function body.
}
#include <iostream> // display structure of type Distance in feet & inches
using namespace std; void disp( Distance dd ) //parameter of type Distance
struct Distance {
{ cout << dd.feet << “\’-” << dd.inches << “\””;
int feet; }
float inches;
};
void disp( Distance ); //declaration
int main()
{ Output:
Distance d1, d2; //define two lengths
//get length d1 from user Enter feet: 6
cout << “Enter feet: “; cin >> d1.feet; Enter inches: 4
cout << “Enter inches: “; cin >> d1.inches; Enter feet: 5
//get length d2 from user Enter inches: 4.25
cout << “\nEnter feet: “; cin >> d2.feet; d1 = 6’-4”
cout << “Enter inches: “; cin >> d2.inches; d2 = 5’-4.25”
cout << “\nd1 = “;
disp(d1); //display length 1
cout << “\nd2 = “;
disp(d2); //display length 2
cout << endl;
return 0;
}
#include <iostream> // display structure of type Distance in feet & inches
using namespace std; void disp( Distance dd ) //parameter of type Distance
struct Distance {
{ dd. Feet=2;
int feet; dd.inches=3.25;
float inches; cout<<"\n in function:";
}; cout << dd.feet << “\’-” << dd.inches << “\””;
void disp( Distance ); //declaration }
int main()
{ Output:
Distance d1, d2; //define two lengths
//get length d1 from user Enter feet: 6
cout << “Enter feet: “; cin >> d1.feet; Enter inches: 4
cout << “Enter inches: “; cin >> d1.inches; Enter feet: 5
//get length d2 from user Enter inches: 4.25
cout << “\nEnter feet: “; cin >> d2.feet; d1= 6’-4”
cout << “Enter inches: “; cin >> d2.inches; In function: 2’-3.25”
cout << “\nd1 = “;
disp(d1); //display length 1 d2 =
cout << “\nd2 = “; In function: 2’-3.25”
disp(d2); //display length 2
cout << endl;
return 0;
}
Pass by Reference
#include <iostream> //orders two numbers
order(n3, n4);
cout << “n1=” << n1 << endl; //print all nos Output:
cout << “n2=” << n2 << endl;
n1=11
cout << “n3=” << n3 << endl; n2=99
cout << “n4=” << n4 << endl; n3=22
return 0; 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.
};
Array
1-D Array
2-D Array
Linear Search
#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
}
};
• Suppose we have 50 students in a class and we have to
input the name and marks of all the 50 students.