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 constructorDemo()
}
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;
int m,n;
public:
Integer()
{
m=10;
m=10;
n=20;
}
Integer(int a,int b)
Integer(int a,int b)
{
m=a;
m=a;
n=b;
}
void display()
void display()
{
cout<<m<<n;
cout<<m<<n;
}
void show()
void show()
{
cout<<m<<n;
cout<<m<<n;
}
}
}
void main()
{
Integer A();
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)
{
}
{
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();
}
----------------------------------------------------------------------
---->
#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()
}
------------------------------------------------------------------
0 Comments