payoffer

PayOffers.in

Wednesday, March 26, 2014

C++ program for demonstrating use of virtual functions

Perform following using virtual functions :
i) Build a master table ii) List a table iii) Insert a new entry
iv) Delete old entry  v) Edit an entry vi) Search for a record

Description :
Virtual functions : Its concept is same as a function but it does not really exists but it appears in a program.The functionality of virtual function can be overridden in its derived class.
Syntax:  class  class_name {
public:
virtual void member_function_main(){        }
};
Code :
#include<iostream.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
class student
{
protected:
char name[15],sub[15];
int rollno;
};

class derived1 : public virtual student
{
protected:
int sub_code,asses_mark;
};
class derived2 : public virtual student
{
protected:
int uni_marks;
};
class derived3 : public derived1 , derived2
{
public:
derived3();
void accept();
void display();
void update();
int search(int key);
};
derived3 :: derived3()
{
strcpy(name,”aaaaa”);
rollno=-999;
strcpy(sub,”sssss”);
sub_code=-999;
asses_mark=-99;
uni_marks=-99;
}
void derived3 :: accept()
{
cout<<”\n\nEnter ROLL NO : “;
cin>>rollno;
cout<<”\n\nEnter NAME : “;
gets(name);
cout<<”\n\nEnter SUBJECT NAME : “;
gets(sub);
cout<<”\n\nEnter SUBJECT CODE :”;
cin>>sub_code;
cout<<”\n\nEnter INTERNAL ASSESSMENT MARKS :”;
cin>>asses_mark;
cout<<”\n\nEnter UNIVERSITY EXAM MARKS :”;
cin>>uni_marks;
}
void derived3 :: display()
{
cout<<”\n\n\tROLL NO:”<<”\t\t”<<rollno;
cout<<”\n\n\tNAME:”<<”\t\t\t”<<name;
cout<<”\n\n\tSUBJECT:”<<”\t\t”<<sub;
cout<<”\n\n\tSUBJECT CODE:”<<”\t\t”<<sub_code;
cout<<”\n\n\tINTERNAL ASSESS MARKS:”<<” “<<asses_mark;
cout<<”\n\n\tUNIVERSITY EXAM MARKS:”<<”\t”<<uni_marks;
}
void derived3 :: update()
{
int ch;
char c;
do
{
cout<<”\n\t1.ROLL NO”;
cout<<”\n\t2.NAME”;
cout<<”\n\t3.SUBJECT”;
cout<<”\n\t4.SUBJECT CODE”;
cout<<”\n\t5.INTERNAL ASSESSMENT MARKS”;
cout<<”\n\t6.UNIVERSITY EXAM MARKS”;
cout<<”\n\nEnter your choice for modifying the fields : “;
cin>>ch;
switch(ch)
{
case 1:
cout<<”\nEnter new ROLL NO:”;
cin>>rollno;
break;
case 2:
cout<<”\nEnter new NAME:”;
cin>>name;
break;
case 3:
cout<<”\nEnter new SUBJECT NAME:”;
cin>>sub;
break;
case 4:
cout<<”\nEnter new SUBJECT CODE:”;
cin>>sub_code;
break;
case 5:
cout<<”\nEnter new INTERNAL ASSESSMENT MARKS:”;
cin>>asses_mark;
break;
case 6:
cout<<”\nEnter UNIVERSITY EXAM MARKS:”;
cin>>uni_marks;
break;
}
cout<<”\n\nDo you want to modify more fields?: “;
cin>>c;
}while(c==’Y’ || c==’y');
}
int derived3 :: search(int key)
{
int i,flag=0;
if(key==rollno)
{
flag=1;
}
return flag;
}
void main()
{
int i,ch,rec_no,key,flag;
derived3 d[20];
clrscr();
do
{
cout<<”\n\n Menu: ”
<<”\n\n\t1.Insert a Record”<<”\n\t2.Display Record”
<<”\n\t3.Delete a Record”<<”\n\t4.Search for a Record”
<<”\n\t5.Update”<<”\n\t6.EXIT”
<<”\n\nEnter your choice: “;
cin>>ch;
switch(ch)
{
case 1:
cout<<”\n\nEnter Record Number:”;
cin>>rec_no;
d[rec_no-1].accept();
break;
case 2:
cout<<”\n\nEnter Record Number:”;
cin>>rec_no;
d[rec_no-1].display();
break;
case 3:
cout<<”\n\nEnter Record Number:”;
cin>>rec_no;
for(int i=rec_no-1;i<=20;i++)
{
d[i]=d[i+1];
}
break;
case 4:
cout<<”\n\nEnter ROLL NO:”;
cin>>key;
for(i=0;i<20;i++)
{
flag=d[i].search(key);
if(flag==1)
break;
}
if(flag==0)
cout<<”\n\nRecord is not present.”;
else
cout<<”\n\nRecord is present at “<<i+1<<” location.”;
break;
case 5:
cout<<”\n\nEnter Record Number: “;
cin>>rec_no;
d[rec_no-1].update();
break;
case 6: exit(0);
}
} while(ch!=6);
getch();
}

Program for demonstrating inheritance in C++


Write a menu driven program to carry out the following things in C++  :
i)Build a master table ii) Display iii) Insert a new entry
iv) Delete entry v) Edit  vi) Search for a record

Description:
Inheritance :The main advantage of inheritance is that ,we can use the predefined code.
All the predefined code resides in the form of class called as base or super class.And to use this code,we have to inherit or extend the class and these classes are called as derived classes or sub classes.
Types of Inheritance :
  • Single Inheritance – There is only one super class and one sub class having one-to-one communication only.
  • Multilevel Inheritance -In this the derived class can has be inherited by other class.
  • Multiple Inheritance – In this,the derived class inherits the features of two base classes.
  • Hierarchical Inheritance -In this,the base class has many subclasses.
  • Hybrid Inheritance -Mixture of any of  the  above  inheritances
Code :
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
class base1
{
char dob[10];
char bg[3];
public:
char name[30];
void get1();
void disp1();
};
class base2
{
float ht;
float wt;
public:
void get2();
void disp2();
};
class base3
{
char policy[10];
char address[50];
public:
void get3();
void disp3();
};
class derived : public base1, public base2 ,public base3
{
char telephone[10];
char liscence[10];
public:
int present;
derived();
void get();
void disp();
};
derived :: derived()
{
present=0;
}
void base1 :: get1()
{
cout<<”\nEnter Name: “;
cin>>name;
cout<<”\nEnter DOB: “;
cin>>dob;
cout<<”\nEnter Blood group: “;
cin>>bg;
}
void base1 :: disp1()
{
cout<<”\n”<<name<<”\t”<<dob<<”\t”<<bg;
}
void base2 :: get2()
{
cout<<”\nEnter height: “;
cin>>ht;
cout<<”\nEnter weight: “;
cin>>wt;
}
void base2 :: disp2()
{
cout<<”\t”<<ht<<”\t”<<wt;
}
void base3 :: get3()
{
cout<<”\nEnter policy number: “;
cin>>policy;
cout<<”\nEnter address: “;
cin>>address ;
}
void base3 :: disp3()
{
cout<<”\t”<<policy<<”\t”<<address;
}
void derived :: get()
{
base1 :: get1();
base2 :: get2();
base3 :: get3();
present=1;
cout<<”\nEnter telephone number: “;
cin>>telephone;
cout<<”\nEnter liscence number: “;
cin>>liscence;
}
void derived :: disp()
{
base1::disp1();
base2::disp2();
base3::disp3();
cout<<”\t”<<telephone<<”\t”<<liscence<<”\t”;
}
void main()
{
derived obj[50],temp;
int ch,i,j=-1,no,flag=0;
char key[10],ans;
clrscr();
do
{
cout<<”\n\tMenu”
<<”\n1.Build Master Table”
<<”\n2.Display”
<<”\n3.Insert a new entry”
<<”\n4.Delete entry”
<<”\n5.Edit”
<<”\n6.Search for record”
<<”\n7.Exit”
<<”\nEnter your choice: “;
cin>>ch;
switch(ch)
{
case 1:
do
{
j++;
obj[j].get();
cout<<”\nAdd another…..(y/n)?: “;
cin>>ans;
}while(ans==’Y’ || ans==’y');
break;
case 2: cout<<”\nName\tDOB\tB.G.\tHeight\tWeight\tPolicy\tAddres\tTeleNo\tLiscNo “;
cout<<”\n———————————————————————–”;
for(i=0;i<=j;i++)
obj[i].disp();
break;
case 3:
cout<<”\n Enter the index no.: “;
cin>>no;
if(no<=j)
{
for(i=no;i<=j;i++)
{
temp=obj[i];
obj[i+1]=temp;
}
obj[no].get();
j++;
cout<<”\nRecord Inserted Successfully…..”;
}
else
cout<<”\nERROR…..”;
break;
case 4:
if(j!=-1)
{
cout<<”\nEnter the record no. to delete: “;
cin>>no;
if(no<=j)
{
for(i=no;i<=j;i++)
{
temp=obj[i];
obj[i]=obj[i+1];
}
j–;
cout<<”\nRecord Deleted SuccessFully…..”;
}
else
cout<<”\nERROR…..”;
}
else
cout<<”\nNo Record to delete…..”;
break;
case 5:
if(j!=-1)
{
cout<<”\nEnter index no.”;
cin>>no;
cout<<”\nEnter modified data: “;
obj[no].get();
cout<<”\nRecord modified Successfully…..”;
}
else
cout<<”\nNo Record to modify…..”;
break;
case 6:
if(j!=-1)
{
cout<<”Enter the name to be searched:” ;
cin>>key;
for(i=0;i<=j;i++)
{
if(strcmp(obj[i].name,key)==0)
{
cout<<”\nRecord Found…..”;
obj[i].disp();
flag=1;
}
}
if(flag==0)
cout<<”\nRecord not found…..”;
}
else
cout<<”\nNo Record to search…..”;
break;
case 7:
exit(0);
default:
cout<<”\nInvalid Choice…..”;
}
}while(ch!=7);
getch();
}

Tuesday, March 25, 2014

Program to perform String operations using operator overloading in C++

C++ code to perform string operations  using  operator overloading 
i. = Equality
ii. == String Copy
iii. + Concatenation
iv. << To display a string

Description :Operator Overloading : It is a specific case of polymorphism where different operators have different implementations depending on their arguments.


Code :
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 50
class string
{
char str[MAX];
public:
friend void operator>>(istream &din,string &s1)
{
din>>s1.str;
}

friend void operator<<(ostream &dout,string &s1)
{
dout<<s1.str;
}
void operator+=(string s1);
void operator==(string s1);
void operator=(string s1);
void operator~();
void operator!();
void operator^(string s1);
string()
{
strcpy(str,” “);
}
string(char s[])
{
strcpy(str,s);
}
};
void string::operator+=(string s1)
{
if(strlen(str)+strlen(s1.str)<MAX)
{
strcat(str,s1.str);
cout<<”\n\nConcatinated string is: “<<str;
}
else
{
cout<<”\n\nSorry the array size is exceeded.”;
}
}
void string::operator==(string s1)
{
if(strcmp(str,s1.str)==0)
cout<<”\n\nStrings are equal.”;
else
cout<<”\n\nStrings are not equal.”;
}
void string::operator=(string s1)
{
strcpy(str,s1.str);
cout<<”\n\nCopy of string is: “<<str;
}
void string::operator~()
{
strrev(str);
cout<<”\n\nReverse of string is: “<<str;
}
void string::operator!()
{
string temp;
strcpy(temp.str,str);
strrev(str);
if(strcmp(temp.str,str))
cout<<”\n\nString is not palindrome.”;
else
cout<<”\n\nString is palindrome.”;
}
void string::operator^(string s1)
{
if(strstr(str,s1.str))
cout<<”\n\n”<<s1.str<<” is substring of “<<str<<”.”;
else
cout<<”\n\nSubstring is not present.”;
}
void main()
{
string s1,s2;
int choice;
char ans;
clrscr();
cout<<”\n\n **** STRING OPERATIONS ****”;
do
{
cout<<”\n\nÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ”;
cout<<”\n\n MENU:”;
cout<<”\n\n\t1.Concatination of string\n\n\t2.Equality\n\n\t3.Copy string\n\n\t4.Reverse of string\n\n\t5.Palindrome\n\n\t6.Occurance of substring”;
cout<<”\n\nÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ”;
cout<<”\n\nEnter your choice: “;
cin>>choice;
switch(choice)
{
case 1:
cout<<”\n\nEnter the first string: “;
cin>>s1;
cout<<”\n\nEnter the second string: “;
cin>>s2;
s1+=s2;
break;
case 2:
cout<<”\n\nEnter the first string: “;
cin>>s1;
cout<<”\n\nEnter the second string: “;
cin>>s2;
s1==s2;
break;
case 3:
cout<<”\n\nEnter the string: “;
cin>>s1;
s2=s1;
break;
case 4:
cout<<”\n\nEnter the string: “;
cin>>s1;
~s1;
break;
case 5:
cout<<”\n\nEnter the string: “;
cin>>s1;
!s1;
break;
case 6:
cout<<”\n\nEnter the string: “;
cin>>s1;
cout<<”\n\nEnter the string which you want to check for occurance: “;
cin>>s2;
s1^s2;
break;
default:
cout<<”\n\nPlease enter correct choice.”;
break;
}
cout<<”\n\n\nDo you want to continue?(y/n): “;
flushall();
cin>>ans;
}while(ans==’y’ || ans==’Y');
getch();
}

Program for performing arithmetic operations of two complex numbers using operator overloading in C++

Description :Operator Overloading : It is a specific case of polymorphism where different operators have different implementations depending on their arguments.

Code :
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class complex
{
float real,img;
public:
complex()
{
real=0;
img=0;
}
complex(float a,float b)
{
real=a;
img=b;
}

void get()
{
cout<<”\n\nEnter the real part of number: “;
cin>>real;
cout<<”\n\nEnter the imaginary part of number: “;
cin>>img;
}
void print()
{
cout<<”(“<<real<<”)”<<” + “<<”(“<<img<<”i”<<”)”;
}
complex operator+(complex c1)
{
complex temp;
temp.real=real+c1.real;
temp.img=img+c1.img;
return temp;
}
complex operator-(complex c1)
{
complex temp;
temp.real=real-c1.real;
temp.img=img-c1.img;
return temp;
}
complex operator*(complex c1)
{
complex temp;
temp.real=(real*c1.real)-(img*c1.img);
temp.img=(img*c1.real)+(real*c1.img);
return temp;
}
complex operator/(complex c1)
{
complex temp,c2;
c2.img=-c1.img;
float x;
temp.real=(real*c1.real)-(img*(c2.img));
temp.img=(real*c1.real)+(real*(c2.img));
x=(c1.real)*(c1.real)+(c1.img)*(c1.img);
temp.real=temp.real/x;
temp.img=temp.img/x;
return temp;
}
};
void main()
{
complex c1,c2,c3;
int choice;
char ans;
clrscr();
do
{
cout<<”\n\n MENU: “;
cout<<”\n\n\t1.Addition\n\n\t2.Subtraction\n\n\t3.Multiplication\n\n\t4.Division”;
cout<<”\n\nEnter your choice: “;
cin>>choice;
switch(choice)
{
case 1:
c1.get();
c2.get();
c3=c1+c2;
cout<<”\n\nAddition is: “;
c3.print();
break;
case 2:
c1.get();
c2.get();
c3=c1-c2;
cout<<”\n\nSubtraction is: “;
c3.print();
break;
case 3:
c1.get();
c2.get();
c3=c1*c2;
cout<<”\n\nMultiplication is: “;
c3.print();
break;
case 4:
c1.get();
c2.get();
c3=c1/c2;
cout<<”\n\nDivision is: “;
c3.print();
break;
}
cout<<”\n\nDo you want to continue?(y/n): “;
fflush(stdin);
cin>>ans;
}while(ans==’y’ || ans==’Y');
getch();
}

Program for database using Static member functions,friend class,this pointer,inline code and dynamic memory allocation in C++

Description :
1) Static member functions :These can be called without any instance(object) of the class .
2)Friend Class :Whenever we create a friend class,all the member function of the friend class also become the friend of other class.
3)This pointer :It is used for pointing to the object for which the meber function is called.
4)Inline function : ‘inline ‘keyword is used to define these functions.Whenever this function is called,the compiler will replace the function call with the actual code.
5)Dynamic Memory allocation :Whenever memory is allocated at runtime,it is called dynamic memory allocation.It is done using ‘new’ operator.

CODE :
#include<iostream.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
class data
{
char name[30],dob[15],bg[5],add[30],no[15];
int height,weight;
public:
data()
{
strcpy(name,”");
strcpy(dob,”00-00-0000″);
strcpy(bg,”");
strcpy(add,”");
strcpy(no,”");
height=0;
weight=0;
}
static int count;
friend class personal;
static void dispcount()
{
cout<<”\n\n\nTOTAL NUMBER OF RECORDS CREATED: “<<count;
}
};
class personal
{
char policy_no[10],license_no[10];
public:
personal()
{
strcpy(policy_no,”");
strcpy(license_no,”");
}
friend class data;
void getdata(data *b);
void putdata(data *b);
};

int data::count=0;
void main()
{
personal *a[10];
data *c[10];
int n=0,i,choice;
char ans;
clrscr();
do
{
cout<<”\n\nMENU: “;
cout<<”\n\n\t1.Create\n\n\t2.Display”;
cout<<”\n\nEnter your choice: “;
cin>>choice;
switch(choice)
{
case 1:
do
{
a[n]=new personal;
c[n]=new data;
a[n]->getdata(c[n]);
n++;
data::dispcount();
cout<<”\n\nDo you want to add more records?(y/n): “;
fflush(stdin);
cin>>ans;
}while(ans==’y’ || ans==’Y');
break;
case 2:
for(int i=0;i<n;i++)
a[i]->putdata(c[i]);
data::dispcount();
break;
default:
cout<<”\n\nPlease enter correct choice.”;
break;
}
cout<<”\n\nDo you want to continue?(y/n): “;
fflush(stdin);
cin>>ans;
}while(ans==’y’ || ans==’Y');
delete[]a;
delete[]c;
getch();
}
void personal :: getdata(data *b)
{
cout<<”\n\n\tENTER THE DETAILS”;
cout<<”\n ————————–”;
cout<<”\n\nNAME: “;
gets(b->name);
cout<<”\n\nDATE OF BIRTH(DD-MM-YYYY): “;
gets(b->dob);
cout<<”\n\nADDRESS: “;
gets(b->add);
cout<<”\n\nTELEPHONE NUMBER: “;
gets(b->no);
cout<<”\n\nBLOOD GROUP: “;
gets(b->bg);
cout<<”\n\nHEIGHT: “;
cin>>b->height;
cout<<”\n\nWEIGHT: “;
cin>>b->weight;
cout<<”\n\nDRIVING LICENSE NUMBER: “;
gets(this->license_no);
cout<<”\n\nPOLICY NUMBER: “;
gets(this->policy_no);
b->count++;
}
void personal :: putdata(data *b)
{
cout<<”\n\n\n——————————————–”;
cout<<”\n\t…. PERSONAL DATABASE ….”;
cout<<”\n——————————————–”;
cout<<”\n\nNAME : “<<b->name;
cout<<”\n\nDATE OF BIRTH : “<<b->dob;
cout<<”\n\nADDRESS : “<<b->add;
cout<<”\n\nTELEPHONE NUMBER : “<<b->no;
cout<<”\n\nBLOOD GROUP : “<<b->bg;
cout<<”\n\nHEIGHT : “<<b->height;
cout<<”\n\nWEIGHT : “<<b->weight;
cout<<”\n\nDRIVING LICENSE NO.: “<<this->license_no;
cout<<”\n\nPOLICY NUMBER : “<<this->policy_no;
}

Program for weather report using constructor and destructor in C++

Description :Whenever the object of the class is created,the constructor is called.Constructor is responsible for assigning initial values for all its data members.It has the same name as its class.Whenever an object is no longer needed ,it has to be deleted from the memory,then we write a destructor for it.It takes the same name of their class preceded by a tilde (~)

Code :
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
class weather
{
int day,month;
float ltemp,htemp;
float rain_amt,snow_amt;
public:
weather();
void getdetail(int);
void putdetail();
};

weather :: weather()
{
day = 99;
ltemp = -999;
htemp = 999;
rain_amt = 0;
snow_amt = 0;
}
void getdata(int *m1,int *d1)
{
cout<<”\n ENTER THE MONTH NUMBER: “;
cin>>*m1;
cout<<”\n ENTER THE DAY NUMBER: “;
cin>>*d1;
return ;
}
void weather :: getdetail(int d)
{
day=d;
cout<<”\n HIGH TEMPERATURE: “;
cin>>htemp;
cout<<”\n LOW TEMPERATURE: “;
cin>>ltemp;
cout<<”\n AMOUNT OF RAIN: “;
cin>>rain_amt;
cout<<”\n AMOUNT OF SNOW: “;
cin >> snow_amt;
return ;
}
void weather :: putdetail()
{
cout<<”\t”<<day;
cout<<”\t “<<htemp;
cout<<”\t\t”<<ltemp;
cout<<”\t\t “<<rain_amt;
cout<<”\t\t “<<snow_amt;
return ;
}
const m=12,d=31;
main()
{
weather w[m][d];
int ch,day,month;
clrscr();
cout<<”\n\n\t\t\t\t\t\t\t\t\t\t..*..*..*..* WEATHER REPORT *..*..*..*..”;
do
{
cout<<”\n\n MENU: “;
cout<<”\n\n\t 1. INSERT “;
cout<<”\n\n\t 2. DISPLAY “;
cout<<”\n\n\t 3. EXIT “;
cout<<”\n\n Enter your choice: “;
cin>>ch ;
switch(ch)
{
case 1 :
getdata(&month,&day);
w[month][day].getdetail(day);
break;
case 2:
cout<<”\n\nENTER THE MONTH: “;
cin>>month ;
cout<<”\n\n———————————————————————”;
cout<<”\n\tDAY \t\tTEMPERATURE\t\t RAIN \t\t SNOW”;
cout<<”\n\n———————————————————————”;
cout<<”\n\t\t HIGH \t LOW”;
cout<<”\n———————————————————————”;
for(int i=1 ; i<=31 ; i++)
{
cout<< “\n” <<i;
w[month][i].putdetail();
}
break;
}
}while(ch!=3);
return 0;
}