Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C++

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

The General Form of a C++ Program

C++ programs will have this general form:


#includes
base-class declarations
derived class declarations
nonmember function prototypes
int main( )
{
...
}
nonmember function definitions
Ex:
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World";
return 0;
}

<iostream> : input output stream


Need for execution of cout statement

Namespace
Scope in C++ can be named. Scope in C++ can contain
declaration statements or definition statements.

Main necessity of namespaces is to avoid name clashes between


identifiers. Two different identifiers in two different
namespaces can have the same name.

“std” is one such built-in namespace. User-defined namespaces


can also be created in C++.

cout
cout is an object/variable of type ostream class in C++.
Main purpose of cout is to print the contents onto the standard
output device.

<< is termed as insertion operator


1
Program to accept and print an integer value.
#include <iostream>
using namespace std;
int main()
{
cout<<"Enter an integer value\n";
int a;
cin>>a;
cout<<"Value of a is "<<a;
return 0;
}

cin is an object of type istream class in C++ .


>> is an extraction operator to read values from standard input
devices.
Variable definition can be done in any part of the program in
C++.

Namespace
#include <iostream>
using namespace std;
namespace nm1
{
int a;
}
namespace nm2
{
int b;
}

int main() {
nm1::a = 20; nm2::b = 30;
cout<<nm1::a<<endl<<nm2::b<<endl;

using namespace nm1;


cout<<a;
cout<<b; //CTE
return 0;
}

2
Reference variables
In C++ an alias for an existing variable can be created, termed
as reference variables.
Ex: int a = 10;
int &r = a;
cout<<r<<a; output: 10 10

Reference is another name for an existing variable, both names


share the same memory location.
Ex: cout<<&a<<endl<<&r; will give same address as output

If the value of the location is modified by one identifier, the


changes are reflected in another too.

Multiple references for the same location can be created.


int a=10; int &r = a; int &z = r;
a, z and r are the different names of the same location.

Reference variables must be initialized at the point of


definition, if not compile-time error will be encountered.
Ex: int &r; // CTE

Reference can be created for a single location and not for


multiple locations.

Parameter passing techniques in C++


1) Call by value
2) Call by address
3) Call by reference

Call by reference
#include <iostream>
using namespace std;
3
void accept(int &, int &);
void display(int, int );
void swap(int &, int &);
int main() {
int i,j;
accept(i,j);
cout<<"Before swapping ";
display(i,j);
swap(i,j);
cout<<"After swapping ";
display(i,j);
return 0; }

void accept(int &p, int &q)


// FP can be "call by pointer", but not "call by value"
{ cin>>p>>q; }

void display(int p, int q)


// FP can be "call by pointer" or "call by reference"
{ cout<<p<<" "<<q<<endl; }

void swap(int &p, int &q)


{ int t; t = p; p = q; q= t; }
Usages of reference variables
Stand alone reference
Ex: int a=10; int &r = a;
Formal parameter as reference
Above example
Return type as reference ⇒ LEARNING OBJECTIVES

Dynamic memory allocation


Memory for variables can be acquired in two ways
1) static/automatic memory allocation
2) Dynamic memory allocation

Static/automatic memory will be acquired and released


automatically.

Dynamic memory allocation can be done when the program is being


executed.

4
Whenever dynamic memory is acquired, using a new operator
address of the memory will be returned. In order to store the
address a pointer of the specific type will be necessary.

In C++ operator ‘new’ is used for acquiring dynamic memory and


operator ‘delete’ is used to release the dynamically allocated
memory.
Ex: int *p= NULL;
p = new int(10); // initializing dynamically acquired
memory
//p=new int; without initializing dynamically acquired
memory
cout<<*p;
delete p; // will release the dynamically acquired memory

DMA for array


#include <iostream>
using namespace std;
int main( ) {
int *p= NULL;
int n,i;
cout<<"enter value for n\n";
cin>>n;
p = new int[n];
cout<<"Enter "<<n<<" values\n";
for(i=0;i<n;i++)
cin>>p[i];

cout<<"Array values are\n";


for(i=0;i<n;i++)
5
cout<<p[i]<<endl;
delete []p; }

You might also like