322 Practical
322 Practical
Ans.
#include<iostream>
#include<cstdlib>
int main()
{
int i, n;
float arr[100];
return 0;
}
Viva Questions:
Ques.What are the differences between references and pointers??
Ans.
On the surface, both references and pointers are very similar, both are used to
have one variable provide access to another. With both providing lots of the same
capabilities.
References : A reference variable is an alias, that is, another name for an already
existing variable. A reference, like a pointer, is also implemented by storing the
address of an object.
A reference can be thought of as a constant pointer (not to be confused with a
pointer to a constant value!) with automatic indirection, i.e the compiler will
apply the * operator for you.
int i = 3;
Differences :
While in references,
int a=10;
int &p=a; //it is correct
but
int &p;
p=a; // it is incorrect as we should declare and initialize references at
single step.
int a = 5;
int b = 6;
int &p = a;
int &p = b; //At this line it will show error as "multiple declaration is not
allowed".
Memory Address: A pointer has its own memory address and size on the stack whereas
a reference shares the same memory address (with the original variable) but also
takes up some space on the stack.
NOTE However if we want the true address of reference, then it can be found out in
turbo IDE by writing the following statement,
int &p = a;
cout << &(&p);
NULL value: Pointer can be assigned NULL directly, whereas reference cannot. The
constraints associated with references (no NULL, no reassignment) ensure that the
underlying operations do not run into exception situation.
Indirection: You can have pointers to pointers offering extra levels of
indirection. Whereas references only offer one level of indirection.I.e,
In Pointers,
int a = 10;
int *p;
int **q; //it is valid.
p = &a;
q = &p;
Whereas in references,
int &p = a;
int &&q = p; //it is reference to reference, so it is an error.
Virtual functions ensure that the correct function is called for an object,
regardless of the type of reference (or pointer) used for function call.
They are mainly used to achieve Runtime polymorphism
Functions are declared with a virtual keyword in base class.
The resolving of function call is done at Run-time.
Example -
#include <iostream>
using namespace std;
class base {
public:
virtual void print()
{
cout << "print base class" << endl;
}
void show()
{
cout << "show base class" << endl;
}
};
void show()
{
cout << "show derived class" << endl;
}
};
int main()
{
base* bptr;
derived d;
bptr = &d;
A Class is a user defined data-type which has data members and member functions.
Data members are the data variables and member functions are the functions used to
manipulate these variables and together these data members and member functions
defines the properties and behavior of the objects in a Class.
In the above example of class Car, the data member will be speed limit, mileage etc
and member functions can be apply brakes, increase speed etc.
A class is defined in C++ using keyword class followed by the name of class. The
body of class is defined inside the curly brackets and terminated by a semicolon at
the end.
Declaring Objects: When a class is defined, only the specification for the object
is defined; no memory or storage is allocated. To use the data and access functions
defined in the class, you need to create objects.
Syntax:
ClassName ObjectName;