C++ IOE Computer Electrical OOP Note for final exam for 32 Mark conform
Inline function:
It is the function that is expanded inline when it is called with corresponding code. The syntax for making inline is inline return-type function-name(no arguments/arguments)
Note:- inline function should always have return type.
- It reduces the exception time of a program with function having fewer lines of codes.
- The execution time of inline function is more than normal function if the function has the larger number of codes or more lines of code.
- It does not work for the recursive course.
Default Arguments:
C++ allows programmers to call a function without specifying all its arguments. In such case function assigned a default value to the parameters which does not have matching arguments in function call. These value in function definition are called default arguments.
Note: while assigning a default value , we should place the default from right to left.
eg:-
- float simple (int P, int t, float r=0.15) (default argumernt or legal)
- float simple(int p, int t=2,float r) (illegal)
- float simple(int p, int t=2,float r=0.15) (legal)
Inline and default arguments:
function overloading:
function overloading means using same function name to
perform variety of task. But in function overloading we must consider two
things. They are:
- 1. Either the number of arguments in function should be different (the
number of input to function should vary).
- 2. if the number of arguments are same then the data type of the function arguments should be different number of parameter different.
Note: return
type has no role in function overloading
int sum(int a, int b)
{
return (a+b);
}
int sum(int a1, int b1, int c1)
{
return(a1+b1+c1);
}
int sum(int d)
{
return (d+5);
}
void main()
{
cout<<"The sum of one
variable"<<sum(10)<<endl;
cout<<"The sum of two variable"<<sum(10,20)<<endl;
cout<<"The sum of three
vriable"<<sum(10,20,30)<<endl;
getch();
}
Q. write a program to calculate the area of rectangle and triangle using concept of function overloading.
type II
#include<iostream.h>
#include<conio.h>
using namespace std;
int area(int l, int b)
{
return (l*b)
}
float area(int b, float h)
{
return (0.5*b*h);
}
void main()
{
int l1,b1;
float h;
cout<<"Enter the value of l1,b1 &
h1<<endl;
cin>>l1>>b1>>h1;
cout<<"The area of rectangle is
<<area(l1,b1)<<endl;
cout<<"The area of triangle
is<<area(l1,h1);
getch();
}
Reference variable:
Ans: Is an alternative name given to the original variable. any changes made to reference variable will change original variable and vice versa.
the syntax for making reference variable is:-
data-type & reference_variable = original variable
eg:- int &a(reference variable)=X(original
variable)
#include<iostream.h>
#include<conio.h>
using namespace std;
void main()
{
int x=5;
int &a=x; // making refrance variable
a=a+5;
cout<<"The value of x
is"<<x<<endl;
getch();
}
output =10
Q. is the concept of alternative variable used for pass by reference ? if yes show with an appropriate example.
Ans: pass by reference: provision of reference variable in
c++ allows us to pass parameter to the function by reference. when we pass
argument by reference the formal argument in the function call (the input
variable of function definition) becomes alternative variable ( or reference
variable) of the variable in function call.
#include<iostream.h>
#include<conio.h>
using namespace std;
void swap (int &a, &b)
{
int temp=a;
a=b;
b=temp;
}
void main()
{
int x1=5,y1=6;
cout<<"before swapping
x1="<<x1<<endl<<"y1="<<y1<<endl;
swap(x1,y1); //
function call
cout<<"After
swapping"<<"x1="<<x1<<endl<<"y1"<<y1<<endl;
getch();
}
Q. wap to calculate area and perimeter of rectangle in a function and display the result in main().
#include<iostream.h>
#include<conio.h>
using namespace std;
void rectangle(int l, int b, int &A1, int &P1)
{
A1=l*b;
P1=2*(l+b);
}
void main()
{
int l1,b1,A,P;
cout<<"Enter the value of l1 and b1"<<endl;
cin>>l1>>b1;
rectangle (l1,b1,A,P);
cout<<"Area of rectangle is
"<<A<<endl<<"Perimeter of rectangle
is"<<P<<endl;
getch();
}
Return by reference:
In return by reference in true
case c++ assign a reference value . in return by the function is called as:
syntax: function call = reference value;
eg:-
Max(a,b)=200; (reference value )
the syntax for function definition in return by
reference is
return-type & function_name(data_type1 &
variable1,data type2 and variable 2...... upto n)
example of return by reference:
#include<iostream.h>
#include<conio.h>
using namespace std;
int &Max(int &a, int &b)
{
if (a>b);
{
return (a);
}
else
{
return
(b);
}
}
void main()
{
int a=10, b=5;
Max(a,b)=200;
// function call
cout<<"a="<<a<<endl<<"b="<<b<<endl;
getch();
}
output: a=10, b=5
Q. How memory is allocated dynamically for normal variable and array?
Ans: Memory is allocated dynamically for normal
variable by using syntax:-
pointer-variable = new data type
eg:-
int *p;
p=new int;
alternatively we can combine to allocate dynamically
syntax: data-type *pointer variable = new data type
for array
syntax
data type *pointer = new data type (size)
eg:-
int *p = new int [20];
class and object:
class:
- 1. a class is a way to bind data and associated function together.
- 2. it allows data and function to hidden if necessary for external use.
- 3. defining a class means creating a user defined data type that behaves as
built in data.
- 4. once a class has been declared we can create any number of object
belonging to that class.
syntax:
class
class_name
{
private:
data
member;
member
function;
public:
data
member;
member
function;
}; //end of class
Generally
data are placed in private section and function are placed in public section.
- 5. class contains both data and function . Data are called data member and
function are called member function.
- 6. data and function are defined in private section are not accessible out
of the class.
- 7. data and function placed in public section are only accessible from
outside the class.
- 8. the word private is optional . the data in function of class by default
private.
object:
- ð object are the basic entities of oop. the object may be person place or anything's that have some physical attribute. Object are the variable of class in terms of programming.
syntax:
class_name
objects_name(variable name)
great
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteGreat information
ReplyDeleteWhen you no longer need the dynamically allocated memory, it is essential to free it using the delete operator. Cisco distributors in dubai
ReplyDelete