C++ || Function Overloading || Inline Function

C++







CONSTRUCTOR
----------------------

Constructor is a special member function whose class name and function  name same. Constructor does not have return type. It should be always before  public. it cannot call it is automatically invoke. It is use construct the values or initialize the value. More than one constructor in a class is called multiple constructor. More than one constructor in a class and parameter should be different is called constructor overloading.  


TYPES OF CONSTRUCTOR 
  • Default constructor
  • Prameterized  constructor
  • Copy constructor
  •  Dynamic constructor

 a copy constructor take a reference to a object as a same class as its self as an argument.

e.g -

class Demo
{
Demo()                                                                         
{      //Default constructor
}
Demo(int a,int b)
{     //parameterized constructor
}
void add(int a, int b)
{
}

};



Examples-

Q) Write a program in C++ to print any two number using default or parameterized constructor.

--- >


#include<stdio.h>
#include<conio.h>

class Integer
{
int m,n;

public:
Integer()
{
m=10;
n=20;
}
Integer(int a,int b)
{
m=a;
n=b;
}
void display()
{
cout<<m<<n;
}
void show()
{
cout<<m<<n;
}
}
void main()
{
Integer A();
Integer A1(20,30);
A.display();
A1.show();
getch();
}

-------------------------------------------------------------------------------------------------

FUNCTION  OVERLOADING

More than one function having same name with different parameter is called Function overloading.
Function overloading is also called compile time polymorphism. it is also called as early binding or static binding.

e.g-


void add(loang a,long b)
{
}
void add(int a,int b)
{
}
void add(float a,float b)
{
}
void main()
{
add(31,41);

add(5,6);
add(5.4,6.1);
}

Example -


Q)write a program in C++ to calculate area of rectangle, triangle, circle use function overloading.

--->

#include<iostream.h>
#include<conio.h>
void area(int l,int w)
{
int A;

A=l*w;
cout<<"area of rectangle is"<<A;
}
void area(float b,float h)
{
int A;

A=1/2*b*h;
cout<<"area of triangle is"<<A;

void area(int r)
{
int A;

A=3.14*r*r;
cout<<"area of circle"<<A;
}
void main()
{
area(2,4);

area(2.1f;2,4f);
area(4);
getch();
}

---------------------------------------------------------------------------

Q)Write a program to calculate area of cone,sphere, circle by using function overloading.

--->

#include<iostream.h>
#include<conio.h>
#include<match.h>

void area(float r,float h)
{
float a;
a=3.14*r*(r+sqrt(h*h+r+r));
cout<<"area is"<<a;
}
void area(float r)
{
float a;
a=4*3.14*r*r;
cout<<"area is"<<a;
}
void area(double r)
{
float a;
a=3.14*r*r;
cout<<"area is"<<a;
}
void main()
{
area(1.5f,2.f);
area(1.5f);
area(1.5);
getch();
}
----------------------------------------------------------------------


Q) Design a class book with the data member to hold title. number of author. ISBN number,price and number of copies, the title and ISBN number are pointer to character Define parameterized and default constructor(number of author and number of cpies equal to 1). write getter and setters for each of the member function . write the copy constructor. write the copy constructor .write member function to check the validity of the ISBN number: it is a unique number assigned to a book which can be a 10 digit or 13 digit number. for 10 digit ISBN number. the sum of all the 10 digits. multiplied by its integer weight, descending from 10 to 1, or asending from 1 to 10, is a muiltiple of 11. 10*1+9*2+8*3+7*4+6*5+5*6+4*7+3*8+2*10+11*3+12*13=0 mod 11 for digit ISBN number. the last degits is a check digit which must range from 0 to 9 and sum of all the thirteen digits multiplied by weights alternating between 1 and 3 is a multiple of   10*1+9*2+8*3+7*4+6*5+5*6+4*7+3*8+2*10+11*3+12*13=0 mod 10 call the function in the constructor before initializing the ISBN number.

---->

#include<iostream.h>
#include<string.h>
using namespace std;

class Book
{
private:
int authors.copies:
char *title.*ISBN;
float price;
public:
Book();
Book(const Book &obj);
Book(int number_of_author,int number_of_copies);
void getdata();
void putdata();
void validity(char*);
};

Book::Book()
{
authors=1;
copies=1;
}

void Book::getData()
{
char temptitle[100],tempISBN[15];
cout<<"Enter the details of the book:\n";
cout<<"Enter Title of the book:";
cin>>tempTitle;
title=new char[strlrn(tempTitle)+1];
cout<<"Number of authors:";
cin>>authors;
cout<<"Number of copies:";
cin>>copies;
cout<<"prices";
cin>>prices;
cout<<"ISBN code:";
cin>>tempISBN;
ISBN=new char[strlen(tempISBN)+1];
strcpy(ISBN,tempISBN);
validity(ISBN);
}

void Book::putData()
{
cout<<"Title:"<<title<<end1;
cout<<"Number of Authors";
cout<<authors<<end1;
cout<<"Number of copies:";
cout<<copies<<end1;
cout<<"price:";
cout<<price<,end1;
cout<<"ISBN codes:";
cout<<"ISBN"<<end1;
}

Book::Book(const book &obj)
{
author=obj.author;
copies=obj.copies;
title=obj.ISBN;
price=obj.price;
}

Book::Book(int number_of_author,int number_of_copies)
{
authors=number_of _author;
copies=number_of_copies;
}

void Book::validity(char *ISBN)
{
int i=0,length;
long int sum=0;
length=strlen(ISBN);
if(length==10)
{
for(i=0;i<length;i++)
sum=sum+((i+1)*(ISBN[i]%48));
if(sum%11==0)
cout<<"ISBN code is valid\n";
else
cout<<"ISBN code is INVALID\n";
}
else if(length==13)
{
for(i=0;i<length;i++)
{
if(i%2==0)
sum=sum+(1*(ISBN[i]%48));
else
sum=sum+(3*(ISBN[i]%48));
}
if(sum%10==0)
{
cout<<"ISBN code is VALID\n";
else
cout<<"ISBN code is INVALID\n";
}
}

int main()
{
Book Book1,Book2(1,500),Book3(Book1);
Book1.getData();
Book2.getData();
Book3.getData();
cout<<'--------------------------------Book  details-------------------\n\n";
Book1.putData();
Book2.putData();
Book3.putData();
return (0);
}

output -
Enter the details of the Book:
Enter Title of the book: Physical_Chemistry
Number of authors:3
Number of Copies:1000
Price:665
ISBN code:9789382956013
ISBN code is VALID

Enter the details of the Book:
Enter Title of the book:the_last_c
Number of authors:3
Number of Copies:500
Price:249
ISBN code:0632054530
ISBN code is VALID

Enter the details of the Book:
Enter Title of the book:Wise&OtherWise
Number of authors:1
Number of Copies:750
Price:150
ISBN code:3459876134
ISBN code is INVALID

----------------------------------- Book  Details----------------------------------------

Title: physical_chemistry
Number of Author:3
Number of copies:1000
price:665
ISBN code:9789382956013
Title:the_last_c
Number of Author:2
Number of copies:500
price:249
ISBN code:0632054530
Title:Wise&OtherWise
Number of Author:1
Number of copies:150
price:150
ISBN code:3459876134

------------------------------------------------------------------------------------------------------

INLINE FUNCTION
--------------------------

Inline function is a function which expanded in a line its take more memory as compare to micros.
the advantage of inline function is increase the proccess exicution and time save. but in some cases inline function cannot be work like function return values, if a loop a switch or go to statement and if function contains static variable and if  function are recursive.
e.g -

#include<iostream.h>
#include<conio.h>
online void cube(int x)
{
cout<<x*x*x;
}
void main()

{
int a;

clrscr();
cout<<"enter a";
cin>>a;
cube(a);
getch();
}

Example -

Q)write a program in C++ to find a simple interest using inline Function.

--->

#include<iostream.h>
#include<conio.h>
inline void simple (int p,float r,int t)
{
int s;

s=(p*r*t)/100;
cout<<"simple interest is"<<s;
}
void main()
{
int x;
float y;
int z;
clrscr();
cout<<"enter x,y,z";
cin>>x>>y>>z;
simple(x,y,z);
getch();
}
-----------------------------------------------------------------

Q) Write a program in C++ to swapping of two number two integer or two float values using inline function as well as function overloading.

--->


#include<iostream.h>
#include<conio.h>
online void swap(int a,int b)

{
int r;
r=a;
a=b;
b=r;
cout<<"swapping is "<<a<<b;
}
online void swap(float a,float b)

{
int c;
c=a;
a=b;
b=c;
cout<<"swapping is "<<a<<b;

}
void main()
{
clrscr();

swap(2,4);
swap(1.6,2.1);
getch()
}

------------------------------------------------------------------

Post a Comment

0 Comments