(1) Write a program which illustrates the use of scope resolution operator..
(2) Define a class to represent a bank account. Include the following members :
Shared By:Mr.Adarsh Patel (Lecturer - AIIS - Anand) Show/Hide Program
/*
Written by :Adarsh Patel-This is Sample Program..
*/
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
class A
{
private:
int no1,no2;
public:
void get();
void display();
};
void A :: get()
{
cout << "Enter Value of No 1 : ";
cin >> no1;
cout << "Enter Value of No 2 : ";
cin >> no2;
}
void A :: display()
{
cout << endl << " ---Value--- " << endl;
cout << "No 1 : " << no1 << endl;
cout << "No 2 : " << no2 << endl;
}
void main()
{
clrscr();
A obj1;
obj1.get();
obj1.display();
getch();
}
Shared By:Mohsin Baloch (SVICS - KADI) Show/Hide Program
//WRITE A PROGRAM WHICH ILLUSTRATES THE USE OF SCOPE RESOLUTION OPERATOR
#include<iostream.h>
#include<conio.h>
int a=10;
int main()
{
int a;
clrscr();
a=5;
cout << "\nOuter(5)=" << a;
{
int a=3;
cout << "\nInner(3)=" << a;
}
cout << "\nglobal(10)=" << ::a;
getch();
return 0;
}
//To show the use of Scope Resolution Operator
#include<iostream.h>
#include<conio.h>
class scope_reso
{
public:
void sum(int);
};
void scope_reso :: sum(int no)
{
int tot=0;
for(int i=1;i<=no;i++)
tot+=i;
cout<<endl<<"Sum of first "<<no<<" integer no. is "<<tot;
}
void main()
{
scope_reso obj;
int n;
clrscr();
do
{
cout<<"Enter No.: ";
cin>>n;
}while(n<=0);
obj.sum(n);
getch();
}
/*1. Write a program which illustrates
the use of scope resolution operator. */
#include<conio.h>
#include<iostream.h>
int variableA=10;//globel
int main()
{
int variableB=20;
{
int variableA=100;
{
cout<<"\nValue of Variable A="<<variableA;
cout<<"\nValue of Variable B="<<variableB;
cout<<"\nValue of Variable A With help of scope operator="<<::variableA;
}
}
return 0;
}
(2) Define a class to represent a bank account. Include the following members :
Shared By:Mr.Adarsh Patel (Lecturer - AIIS - Anand) Show/Hide Program
/*
Written by :Adarsh Patel-This is Sample Program..
*/
/*
Define a class to represent a bank account.
Include the following members :
DATA MEMBERS MEMBER FUNCTIONS
------------------------ ------------------------------
Name of depositor (1) To assign initial values
Account Number (2) To Deposit the amount
Type of Account (3) To withdraw an amount after checking the
Balance amount in account
(4) To display name and balance
Write C++ program to handle 10 customers. Situation when withdrawal is not
possible must be handled through exceptions handling feature of C++.
*/
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
class Bank
{
private:
int accno[10],acctype[10],accbal[10];
char name[10][20];
public:
static int count;
Bank()
{
for(int i=0;i<10;i++)
{
accno[i]=0;
acctype[i]=-1;
accbal[i]=0;
strcpy(name[i],'\0');
}
}
void CreateAccount(int a,char* n,int at)
{
if (count<9)
{
count++;
accno[count] = a;
strcpy(name[count],n);
acctype[count] = at;
accbal[count]=0;
}
}
void Deposite(int an, int amt)
{
for(int i=0;i<=count;i++)
{
if( accno[i]==an)
{
accbal[i]=accbal[i]+amt;
}
}
}
void Withdraw(int an,int amt)
{
for(int i=0;i<=count;i++)
{
if( accno[i]==an)
{
accbal[i]=accbal[i]-amt;
}
}
}
void display()
{
cout << endl << "*********************************";
cout << endl << " Bank Statement ";
cout << endl << "*********************************";
cout << endl << " Acc No Name Balance";
cout << endl << "*********************************";
for(int i=0;i<=count;i++)
{
cout << endl << " " << accno[i] << " " << name[i] << " " << accbal[i];
}
cout << endl << "*********************************";
}
};
int Bank :: count = -1;
void main()
{
clrscr();
Bank b;
b.CreateAccount(101,"Adarsh",1);
b.CreateAccount(102,"Sandeep",1);
b.Deposite(101,5000);
b.Deposite(102,6000);
b.Withdraw(101,100);
b.Withdraw(102,200);
b.display();
getch();
}
Shared By::Mohsin Baloch (SVICS - KADI) Show/Hide Program
//DEFINE A CLASS TO REPRESENT A BANK ACCOUNT.
#include<iostream.h>
#include<conio.h>
#include<string.h>
int N;
static int obj;
class bank
{
char dname[20];
int accno;
char type;
float balance;
static int flag;
static int flag1;
public:
void display()
{
cout<<accno<<"\t"<<dname<<"\t"<<type<<"\t"<<balance<<endl;
}
void create()
{
cout << "Enter Account number=";
int no1;
cin >> no1;
cout << "Enter the name of customer=";
char *name1;
cin >> name1;
cout << "Enter the account type 'C' or 'F'=";
char type1;
cin >> type1;
cout << "Enter amount=";
int amt1;
cin >> amt1;
if(amt1 >= 5000)
{
accno=no1;
strcpy(dname,name1);
type=type1;
balance=amt1;
obj++;
cout <<"\n\nAccount is created.";
}
else
cout << "\nU can't pay less than 5000 to create account.";
}
int deposit(int);
int withdraw(int);
};
int bank :: flag;
int bank :: flag1;
int bank :: withdraw(int ac)
{
float amount;
if(accno==ac)
{
cout<<"\nBalance is="<<balance;
cout<<"\nEnter amount=";
cin >> amount;
if((balance-amount) >= 5000)
{
balance = balance - amount;
cout << "\nWithdrawal amount is= " << amount;
cout << "\n\nSo New Balance is=" << balance;
flag1++;
return flag1;
}
else
{
cout << "U have not sufficient balance.";
flag1++;
return flag1;
}
}
return flag1;
}
int bank :: deposit(int ac)
{
float amount;
if(accno==ac)
{
cout << "\nHowmany amount u want to deposite=";
cin >> amount;
balance=balance + amount;
cout << endl << amount << " amount is deposited.";
cout << "\n\nSo New Balance is=" << balance;
flag++;
return flag;
}
else
{
return flag;
}
}
void main()
{
int n;
clrscr();
cout << "\nEnter howmany account u entered=";
cin >> n;
bank *b=new bank[n];
int ch;
N=n;
getch();
clrscr();
do
{
clrscr();
cout << "\nUr limit 2 create account is=" << n;
cout << "\n1..Create Account.";
cout << "\n2..Deposit.";
cout << "\n3..Withdraw.";
cout << "\n4..Display.";
cout << "\n5..Exit.";
cout << "\nEnter choice=";
cin >> ch;
switch(ch)
{
case 1: if(obj<N)
{
cout << "\nEnter Account detail.\n";
b[obj].create();
}
else
{
cout << "\n\nUr limit of create acc is exeed so can't create account.";
}
getch();
break;
case 4: if(obj>0)
{
cout << "\n\naccno\t"<<"name\t"<<"type\t"<<"balance\n";
for(int i=0;i<obj;i++)
{
b[i].display();
}
}
if(obj==0)
cout << "\n\nThere are no account created.";
getch();
break;
case 2: if(obj>0)
{
int ac;
int temp;
cout << "\nFor in which account u want to deposit=";
cin >> ac;
for(int i=0;i<obj;i++)
{
temp=b[i].deposit(ac);
}
if(temp==0)
{
cout << "\nAccount is not found";
}
}
if(obj==0)
cout << "\n\nThere are no account created.";
getch();
break;
case 3: if(obj>0)
{
int temp1;
int ac;
cout << "\nFor in which account u want to withdraw=";
cin >> ac;
for(int i=0;i<obj;i++)
{
temp1=b[i].withdraw(ac);
}
if(temp1==0)
{
cout << "\nAccount is not found.";
}
}
if(obj==0)
cout << "\n\nThere are no account created.";
getch();
break;
case 5: return;
default: cout << "\nEnter right option.";getch();
}
}while(ch);
getch();
}
#include<iostream.h>
#include<conio.h>
class bank_acct
{
char name[25];
int acct_no,amount;
public:
void insert(bank_acct * ob);
void deposit(bank_acct * ob);
void withdraw(bank_acct * ob);
void disp(bank_acct *ob)
{
cout<<endl<<"Name: ";
cout<<ob->name;
cout<<endl<<"Acct No: ";
cout<<ob->acct_no;
cout<<endl<<"Amount: ";
cout<<ob->amount;
}
};
void bank_acct :: insert(bank_acct * ob)
{
cout<<"Enter Name: ";
cin>>ob->name;
cout<<"Enter Account No.: ";
cin>>ob->acct_no;
cout<<"Enter Amount: ";
do
{
cin>>ob->amount;
cout<<endl<<"Record Inserted";
}while(ob->amount>0);
}
void bank_acct :: deposit(bank_acct * ob)
{
int amt;
do
{
cout<<"Enter amount: ";
cin>>amt;
}while(amt<=0);
ob->amount = ob->amount + amt;
cout<<endl<<"Amount Deposited";
}
void bank_acct :: withdraw(bank_acct * ob)
{
int amt;
do
{
cout<<"Enter amount: ";
cin>>amt;
}while(amt<=0);
if(ob->amount>=amt)
{
ob->amount = ob->amount - amt;
cout<<endl<<"Withdrawal Successful";
}
else
cout<<endl<<"No Sufficient Amount";
}
void main()
{
bank_acct b[10];
int idx=0,ch,i;
clrscr();
do
{
do
{
clrscr();
cout<<"1. Insert"<<endl<<"2. Deposit"<<endl;
cout<<"3. Withdraw"<<endl<<"4. Display"<<endl;
cout<<"5. Exit";
cin>>ch;
}while(ch<1 || ch>5);
switch(ch)
{
case 1:
if(idx<10)
{
b[idx].insert(&b[idx]);
idx++;
}
else
cout<<"Insert not Possible";
getch();
break;
case 2:
do
{
cout<<"Enter Record ID: ";
cin>>i;
}while(i<0 || i>9);
b[i].deposit(&b[i]);
getch();
break;
case 3:
do
{
cout<<"Enter Record ID: ";
cin>>i;
}while(i<0 || i>9);
b[i].withdraw(&b[i]);
getch();
break;
case 4:
do
{
cout<<"Enter Record ID: ";
cin>>i;
}while(i<0 || i>9);
b[i].disp(&b[i]);
getch();
}
}while(ch!=5);
}
/* (2) Define a class to represent a bank account. Include the following members :
DATA MEMBERS MEMBER FUNCTIONS
------------------------ ------------------------------
Name of depositor (1) To assign initial values
Account Number (2) To Deposit the amount
Type of Account (3) To withdraw an amount after checking the
Balance amount in account(4) To display name and balance */
#include <iostream.h>
#include <conio.h>
#include <string.h>
class account
{
char accno[15],accname[20];
float bal;
public:
account( char a[],char b[],float c)
{
strcpy(accno,a);
strcpy(accname,b);
bal=c;
}
account()
{
}
void get()
{
cout << "\n Enter Account no,Name of Holder and Opening balance:";
cin >> accno >> accname >> bal;
}
void deposit(float b)
{
bal+=b;
}
void withdraw(float b)
{
float t=bal-b;
try
{
if(t <= 500)
throw(t);
else
{
bal -= b;
cout << "\n =====>Amount withdrawn";
}
}
catch(int f)
{
cout << "\n =====>>Not enough balance";
}
if(t <= 500)
cout << "\n =====>>Not enough balance to withdraw";
else
{
bal-=b;
cout << "\n=====>Amount withdrawn";
}
}
void put()
{
cout << endl <<"Accno ="<< accno <<"\tAccname="<<accname<<"\tBalnce= "<<bal;
}
char * getname()
{
return accname;
}
};
int main()
{
account ac[10],t;
int count=0,ch,i;
float amt;
char nm[20];
while(1)
{
cout <<" \n=======MENU========";
cout << "\n 1=Create New account";
cout << "\n 2=Deposit amount";
cout << "\n 3=Withdraw amount";
cout << "\n 4=Display account";
cout << "\n 5=Exit";
cout << "\n =====>>>Enter choice: ";
cin >> ch;
switch(ch)
{
case 1: if(count > 9)
cout << "\n No more accounts please";
else
{ t.get();
ac[count++]=t;
}
break;
case 2: cout << "\n Enter name and deposit balance: ";
cin >> nm >> amt;
for(i=0; i < count; i++)
{
if(strcmp(nm,ac[i].getname())==0)
{
ac[i].deposit(amt);
break;
}
}
break;
case 3: cout << "\n Enter name and withdraw balance: ";
cin >> nm >> amt;
for(i=0; i < count; i++)
{
if(strcmp(nm,ac[i].getname())==0)
{
ac[i].withdraw(amt);
break;
}
}
break;
case 4:
cout << "\n Enter name: ";
cin >> nm;
for(i=0; i < count; i++)
{
if(strcmp(nm,ac[i].getname())==0)
{
ac[i].put();
break;
}
}
break;
case 5: return 0;
break;
default: cout << "\n Invalid choice";
}
}
}
(3) Demonstrate the use of static variables in a class by using it to count the number of objects created in the program
Shared By:Mr.Adarsh Patel (Lecturer - AIIS - Anand) Show/Hide Program
/*
Written by :Adarsh Patel-This is Sample Program..
Demonstrate the use of static variables in a class by using it to count the number of
objects created in the program
*/
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
class Student
{
public:
static int count;
Student()
{
count = count + 1;
cout << endl << "...Constructor Called....";
cout << endl << " Object Count : " << count;
}
~Student()
{
count = count - 1;
cout << endl << "...Destructor Called....";
cout << endl << " Object Count : " << count;
}
};
int Student :: count = 0;
void main()
{
clrscr();
Student S1,S2,S3;
getch();
}
Shared By:Mohsin Baloch (SVICS - KADI) Show/Hide Program
/* DEMONSTRATES THE USE OF STATIC VARIABLES IN A CLASS BY
USING IT TO COUNT THE NUMBER OF OBJECTS CREATED IN THE PROGRAM */
#include <iostream.h>
#include <conio.h>
class test
{
int b;
static int a;
public:
test()
{
cout << "\nEnter value=";
cin >> b;
a++;
}
void display()
{
cout << "\nValue of b is=" << b;
cout << "\nNo of object is=" << a;
}
};
int test :: a;
void main()
{
clrscr();
test t1;
t1.display();
test t2;
test t3;
t1.display();
t2.display();
t3.display();
getch();
}
//To use Static variables in a class to count the number of objects
//created in the program
#include<iostream.h>
#include<conio.h>
class count
{
static int cnt;
public:
count()
{
cnt++;
}
~count()
{
cout<<endl<<"Total no. of Objects: "<<cnt;
}
};
int count :: cnt;
void main()
{
count c1,c2,c3;
clrscr();
c1.disp();
getch();
}
(4) Write a program that calculates the value of m raised to power n for both int and double data types. (Use the concept of function overloading)
Shared By:Mr.Adarsh Patel (Lecturer - AIIS - Anand) Show/Hide Program
/*
Written by :Adarsh Patel-This is Sample Program..
Write a program that calculates the value of m raised
to power n for both int and double
data types. (Use the concept of function overloading)
*/
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <math.h>
class Power
{
public:
void Calculate(int a, int n)
{
int ans=1;
for(int i=1;i<=n;i++)
{
ans=ans*a;
}
cout << endl << "******************************************";
cout << endl << " Calculating Power";
cout << endl << "******************************************";
cout << endl << " Power " << a << " Raised to " << n << " is : " << ans ;
cout << endl << "******************************************";
}
void Calculate(double a, double n)
{
cout << endl << exp(n*log(a)) << endl;
/*
double ans=1.0;
for(int i=1;i<=n;i++)
{
ans=ans*double(a);
}
cout << endl << "******************************************";
cout << endl << " Calculating Power";
cout << endl << "******************************************";
cout << endl << " Power " << a << " Raised to " << n << " is : " << ans ;
cout << endl << "******************************************";
*/
}
};
void main()
{
clrscr();
Power p1,p2;
// p1.Calculate(5,2);
p1.Calculate(5.0,2.5);
getch();
}
Shared By :Mohsin Baloch (SVICS - KADI) Show/Hide Program
/* WRITE A PROGRAM THAT CALCULATES THE VALUE OF M
RAISED TO POWER N FOR BOTH INT AND DOUBLE
DATA TYPES(USING FUNCTION OVERLOADING). /*
#include <iostream.h>
#include <conio.h>
#include <math.h>
void power (double,int);
void power (int,int);
void main()
{
int m1=5;
double m2=3.5;
int n=3;
clrscr();
cout << "\nValue of m1 is=" << m1 <<endl;
cout << "power is=" << n;
power(m1,n);
cout << "\nValue of m2 is=" << m2 <<endl;
cout << "power is=" << n <<endl;
power(m2,n);
getch();
}
void power(double m,int n)
{
cout << "M raise to n power is(double)=" << pow(m,n) << endl;
}
void power(int m,int n)
{
cout << "\nM raise to n power is(int)=" << pow(m,n) << endl;
}
Shared By :Sonam Patel(SRIMCA college-Tarsadi) Show/Hide Program
//Calculates the value of m raised to power n for int and double data types.
//Use the concept of function overloading
#include<iostream.h>
#include<conio.h>
long int mpowern(int a,int b)
{
long int ans=1;
for(int i=1;i<b;i++)
ans = ans * a;
return ans;
}
float mpowern(float a,float b)
{
float ans=1;
for(int i=1;i<b;i++)
ans = ans * a;
return ans;
}
void main()
{
int a,b,c;
float x,y,z;
clrscr();
cout<<"Enter Integer M and N: ";
cin>>a>>b;
cout<<"Enter Float M and N: ";
cin>>x>>y;
c=mpowern(a,b);
z=mpowern(x,y);
cout<<endl<<a<<" Power "<<b<<" : "<<c;
cout<<endl<<x<<" Power "<<y<<" : "<<z;
getch();
}
(5) Write a class to represent a vector. Include member functions to perform the following
Shared By:Mr.Adarsh Patel (Lecturer - AIIS - Anand) Show/Hide Program
/*
Written by :Adarsh Patel-This is Sample Program..
Write a class to represent a vector.
Include member functions to perform the following
1. To create the vector
2. To modify the value of a given element
3. To multiply by a scalar value
4. To display the vector in the form(10,20,30...)
*/
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
class Vector
{
private:
int v[100],n;
public:
void Create()
{
cout << "Enter N for Vector : ";
cin >> n;
for(int i=0;i<n;i++)
{
cout << "Enter V["<< i << "] :";
cin >> v[i];
}
}
void Modify()
{
int t;
cout << "Enter Index to Modify :";
cin >> t;
cout << "Enter New Value for v[" << t-1 <<"] :";
cin >> v[t-1];
}
void Multiply()
{
int t,t2;
cout << "Enter Value for Multiplication v[" << t-1 <<"] :";
cin >> t2;
for(int i=0;i<n;i++)
{
v[i-1] = v[i-1] * t2;
}
}
void Display()
{
cout << endl << " ********************************************";
cout << endl << " : Values of Vector is : ";
cout << endl << " ********************************************";
for(int i=0;i<n;i++)
{
cout << endl << "V["<< i << "] :" << v[i];
}
}
};
void main()
{
clrscr();
Vector v1;
v1.Create();
v1.Modify();
v1.Multiply();
v1.Display();
getch();
}
Shared By:Mohsin Baloch (SVICS - KADI) Show/Hide Program
/*WRITE A CLASS TO REPRESENT A VECTOR.
INCLUDING FUNCTION....
CREATE.
MODIFY ELEMENT.
MULTIPLY BY SCALER VALUE
DISPLAY LIKE (10,20....). */
#include <iostream.h>
#include <conio.h>
#define N 5
class vector
{
int a[N];
public:
vector();
void display();
void scaler(int);
void modify(int);
};
vector :: vector()
{
cout << "\nEnter elements of vector.\n\n";
for(int i=0;i<N;i++)
{
cout << "Enter " << i+1 << " Element=> ";
cin >> a[i];
}
}
void vector :: display()
{
cout << "\nElements are under below.\n";
cout << "(";
int i=0;
cout << a[i];
for(i=1;i<N;i++)
{
cout << "," << a[i];
}
cout << ")" << endl;
}
void vector :: scaler(int x)
{
for(int i=0;i<N;i++)
{
a[i]=a[i]*x;
}
cout << "\nAfter Multipling.";
display();
}
void vector :: modify(int x)
{
int flag=0;
for(int i=0;i<N;i++)
{
if(a[i]==x)
{
int value;
cout << "Enter new value=";
cin >> value;
a[i]=value;
flag=1;
}
}
if(flag==0)
{
cout << "\nValue not found.";
}
else
{
cout << "\nAfter changing.";
display();
}
}
void main()
{
clrscr();
int ch;
do
{
clrscr();
cout << "\n1..Creating vector.";
cout << "\n2..Modify vector.";
cout << "\n3..Multiply by a scaler value.";
cout << "\n4..Display the vector.";
cout << "\n5..Exit.";
cout << "\nEnter choice what u want=";
cin >> ch;
switch(ch)
{
case 1: vector v; //creating vector
getch();
break;
case 2: int s,m;
cout << "\nEnter value which u want to modify=";
cin >> m;
if(m>=0 && m<=N)
{
v.modify(m); //modify value
}
getch();
break;
case 3: cout << "\nMultiply vector with scalar value.";
cout << "\nSo Enter scaler value=";
cin >> s;
v.scaler(s); // scaler multiplication
getch();
break;
case 4: v.display();
getch();
break;
case 5: return;
default: cout << "\nInvalid choice.";
getch();
}
}while(ch);
getch();
}
//Create a class to represent a vector and Perform some Operations on vector.
#include<iostream.h>
#include<conio.h>
class vector
{
int a[10];
public:
vector();
void create(int n);
void modify(int i,int v);
void multiply(int s,int n);
void show(int n);
};
vector :: vector()
{
for(int i=0;i<10;i++)
a[i]=0;
}
void vector :: create(int n)
{
for(int i=0;i<n;i++)
cin>>a[i];
}
void vector :: modify(int i,int val)
{
a[i]=val;
}
void vector :: multiply(int s,int n)
{
for(int i=0;i<n;i++)
a[i] *= s;
}
void vector :: show(int n)
{
cout<<endl;
for(int i=0;i<n-1;i++)
cout<<a[i]<<",";
if(i==n-1)
cout<<a[n-1]<<endl;
}
void main()
{
vector v;
int n,i,val,s,ch;
clrscr();
do
{
cout<<"How many elelments you want to insert? ";
cin>>n;
}while(n<=0 || n>10);
v.create(n);
do
{
do
{
clrscr();
v.show(n);
cout<<endl<<"Do You want to Modify the Vector?";
cout<<endl<<"1. YES"<<endl<<"2. NO"<<endl<<"Enter Your Choice: ";
cin>>ch;
}while(ch<1 || ch>2);
if(ch==1)
{
do
{
cout<<endl<<"Enter Index No.: ";
cin>>i;
}while(i<0 || i>=n);
cout<<endl<<"Enter New Value: ";
cin>>val;
v.modify(i,val);
}
}while(ch!=2);
cout<<endl<<"Enter Scalar Value to Multiply Vector: ";
cin>>s;
v.multiply(s,n);
v.show(n);
getch();
}
(6) Define a class Car. Add data members as Make, Color, Size, and Cost....
Shared By:Mr.Adarsh Patel (Lecturer - AIIS - Anand) Show/Hide Program
/*
Written by :Adarsh Patel-This is Sample Program..
Define a class Car.
Add data members as
Make, Color, Size, and Cost.
Write member functions for
reading values
printing values of car.
Define one more class as CarCollection.
CarCollection contains array of cars.
CarCollection class should contain
member functions as
Add,
delete,
modify and
replace.
Collection is to be defined as friend of Car class.
Use Exception Handling techniques to handle errors.
*/
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <process.h>
class Car
{
public:
int make,color,size,cost;
public:
void reading()
{
cout << endl << "*****************************";
cout << endl << " Enter Informations of Car";
cout << endl << "*****************************";
cout << endl << "Maker : ";
cin >> make;
cout << "Color : ";
cin >> color;
cout << "Size : ";
cin >> size;
cout << "Cost : ";
cin >> cost;
}
/*
void modify(int a,int b,int c,int d)
{
make=a;
color=b;
size=c;
cost=d;
} */
void printing()
{
cout << endl << "*****************************";
cout << endl << "Car Information";
cout << endl << "*****************************";
cout << endl << "Maker : " << make;
cout << endl << "Color : " << color;
cout << endl << "Size : " << size;
cout << endl << "Cost : " << cost;
cout << endl << "*****************************";
}
};
class Carcollection
{
private:
int count;
Car obj[10];
public:
Carcollection()
{
count=0;
}
void cadd()
{
cout << endl << " Object : " << count+1 << " Is Create Please enter values " << endl;
obj[count].reading();
count++;
//obj[count++].printing();
}
void cdelete()
{
int n;
cout << " Enter Index of Car to delete" << endl;
cin>>n;
//obj[--count].printing();
for(int i=0;i<count;i++)
{
if(i>=n)
obj[i]=obj[i+1];
if(i==count-1)
{
obj[count-1].make=0;
obj[count-1].color=0;
obj[count-1].size=0;
obj[count-1].cost=0;
}
}
}
void cmodify()
{
int t;
cout << "Which object do you want to Modify : ";
cin >>t;
obj[t-1].reading();
}
void creplace() {
int s,d;
cout << endl << "Replace Object";
cout << endl << "Source Object : ";
cin >> s;
cout << endl << "Destination Object : ";
cin >> d;
obj[s-1] = obj[d-1];
}
void cdisplay()
{
int o;
cout << endl << " Total Object Count : " << count;
cout << endl << "Which Object You want to See ";
cin >> o;
obj[o-1].printing();
}
};
void main()
{
clrscr();
Carcollection c;
int choice;
do
{
cout << endl << "1 : Create Object";
cout << endl << "2 : Modify Object";
cout << endl << "3 : Delete Object";
cout << endl << "4 : Replace Object";
cout << endl << "5 : Display";
cout << endl << "6 : Exit";
cout << endl << endl << "Enter Your Choice : ";
cin >> choice;
switch(choice)
{
case 1:
c.cadd();
break;
case 2:
c.cmodify();
break;
case 3:
c.cdelete();
break;
case 4:
c.creplace();
break;
case 5:
c.cdisplay();
break;
}
}while(choice!=6);
getch();
}
Shared By :Mohsin Baloch (SVICS - KADI) Show/Hide Program
/*DEFINE A CLASS CAR. ADD DATA MEMBERS AS MAKE,COLOR,SIZE AND COST. WRITE
MEMBER FUNTIONS FOR READING VALUES AND PRINTING VALUES OF CAR. DEFINE
ONE MORE CLASS AS COLLETION. COLLECTION CONTAINS ARRAY OF CARS. COLLECTION
CLASS SHOULD CONTAIN MEMBER FUNCTION AS ADD,DELETE,MODIFY,REPLACE.
COLLECTION IS TO BE DEFINED AS FRIEND OF CAR CLASS. USE EXCEPTION
HANDLING TECHNIQUES TO HANDLE ERRORS.*/
#include <iostream.h>
#include <conio.h>
#include <string.h>
static int count;
class car
{
int id;
int make;
char color[10];
int size;
double cost;
public:
void getdata()
{
id=count + 1;
cout << "Enter production year=";
cin >> make;
cout << "Enter color of car=";
cin >> color;
cout << "Enter sheeting capasity of car=";
cin >> size;
cout << "Enter cost of car=";
cin >> cost;
}
void display();
friend collection;
};
void car :: display()
{
cout <<"\n\n\nInformation of car=";
cout << "\nCar id is= " << id;
cout << "\nCar production year= " << make;
cout << "\nCar color=" << color;
cout << "\nCar sheeting capasity= " << size;
cout << "\nCar cost=" << cost;
}
class collection
{
car *c;
int ch;
public:
collection()
{
cout << "Enter how many car u want to add=";
cin >> ch;
c=new car[ch];
}
void add()
{
if(count==ch)
cout << "\n\nU cant enter more car.";
else
{
c[count].getdata();
count++;
}
}
void delete1();
void modify();
void replace();
void show();
};
void collection :: delete1()
{
int rep;
int flag=0;
cout << "\nEnter which car u want to delete=";
cin >> rep;
int i;
for(i=0;i<count;i++)
{
if(rep==c[i].id)
{
c[i].id=0;
c[i].make=0;
strcpy(c[i].color,NULL);
c[i].size=0;
c[i].cost=0;
flag=1;
cout << "\nCar successful deleted.";
}
}
if(flag==0)
cout << "\n No car found of this ID.";
}
void collection :: modify()
{
int id;
int flag=0;
cout << "Enter id which u want to modify= ";
cin >> id;
for(int i=0;i<count;i++)
{
if(c[i].id==id)
{
int ch;
cout << "\n1..production year.";
cout << "\n2..color of car.";
cout << "\n3..capasity.";
cout << "\n4..cost.";
cout << "\nWhat u want to modify= ";
cin >> ch;
flag=1;
switch(ch)
{
case 1: cout << "Enter production year=";
cin >> c[i].make;
break;
case 2: cout << "Enter color of car=";
cin >> c[i].color;
break;
case 3: cout << "Enter sheeting capasity of car=";
cin >> c[i].size;
break;
case 4: cout << "Enter cost of car=";
cin >> c[i].cost;
break;
default: cout << "\nInvalid option.";
}
cout << "\nSuccessful modified";
}
}
if(flag==0)
cout << "\nNo such car of this ID.";
}
void collection :: replace()
{
int rep;
int flag=0;
cout << "\nEnter which car u want to replace=";
cin >> rep;
int i;
for(i=0;i<count;i++)
{
if(rep==c[i].id)
{
cout << "Enter production year=";
cin >> c[i].make;
cout << "Enter color of car=";
cin >> c[i].color;
cout << "Enter sheeting capasity of car=";
cin >> c[i].size;
cout << "Enter cost of car=";
cin >> c[i].cost;
flag=1;
cout << "\nCar successful replaced.";
}
}
if(flag==0)
cout << "\nNo such car of thid ID.";
}
void collection :: show()
{
int flag=0;
for(int i=0;i<count;i++)
{
if(c[i].id!=0 && c[i].color!=NULL)
{
c[i].display();
flag=1;
}
}
if(flag==0)
{
cout << "\nThere are no such car.";
}
}
void main()
{
int ch;
clrscr();
collection cc;
do
{
clrscr();
cout << "\n1...Add car.";
cout << "\n2...Delete car.";
cout << "\n3...Modify car.";
cout << "\n4...Replace car.";
cout << "\n5...Display.";
cout << "\n6...Exit.";
cout << "\nEnter choice what u want to do= ";
cin >> ch;
switch(ch)
{
case 1: cc.add();
getch();
break;
case 2: cc.delete1();
getch();
break;
case 3: cc.modify();
getch();
break;
case 4: cc.replace();
getch();
break;
case 5: cc.show();
getch();
break;
case 6: return;
default: cout << "\nInvalid option.";
getch();
}
}while(ch);
}
(7) Overload all the four arithmetic operators to operate on a vector class and...
Shared By:Mr.Adarsh Patel (Lecturer - AIIS - Anand) Show/Hide Program
/*
Written by :Adarsh Patel-This is Sample Program..
Overload all the four arithmetic operators to operate
on a vector class and also the overload the * operator to
multiply scalar values to the vector class. Overload the >>
operator to input a vector and the
<< operator to display the vector in the form (10,20,.....).
Also overload the [] operator to access the indivisual member of the vector.
Use Dynamic memory allocation to achieve the solution.
Write appropriate constructor and destructure for the class.
*/
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
class Vector
{
private:
int no[100],n;
public:
Vector(int j)
{
for(int i=0;i<25;i++)
{
no[i]=0;
}
}
Vector()
{
cout << endl << " Enter Number of Terms : ";
cin >> n;
for(int i=0;i<n;i++)
{
cout << "Enter Value of V[" << i <<"]: " ;
cin >> no[i];
}
}
~Vector()
{
// cout << "... Destructor called... ";
}
Vector operator +(Vector v)
{
Vector t(0);
for(int i=0;i<n;i++)
{
t.no[i] = no[i] + v.no[i];
}
t.n = v.n;
return t;
}
Vector operator -(Vector v)
{
Vector t(0);
for(int i=0;i<n;i++)
{
t.no[i] = no[i] - v.no[i];
}
t.n = v.n;
return t;
}
Vector operator *(Vector v)
{
Vector t(0);
for(int i=0;i<n;i++)
{
t.no[i] = no[i] * v.no[i];
}
t.n = v.n;
return t;
}
Vector operator /(Vector v)
{
Vector t(0);
for(int i=0;i<n;i++)
{
t.no[i] = no[i] / v.no[i];
}
t.n = v.n;
return t;
}
void operator << (Vector)
{
//Display
for(int i=0;i<n;i++)
{
cout << " " << no[i] << " ";
}
}
Vector operator >>(Vector v)
{
// cout << endl << " Enter Number of Terms : ";
// cin >> n;
for(int i=0;i<n;i++)
{
cout << "Enter Value of V[" << i <<"]: " ;
cin >> no[i];
}
}
};
void main()
{
clrscr();
Vector v1,v2;
// >> Operator Overloading
v1 >> v2;
v2 >> v1;
//Display Values
v1 << v2;
v2 << v1;
//+ Operator overloading
v1 = v1 + v2;
//- Operator overloading
v2 = v1 - v2;
//* Operator overloading
v1 = v1 * v2;
// / Operator overloading
v1 = v1 / v2;
//Display Values
v1<< v2;
v2<< v1;
getch();
}
Shared By :Mohsin Baloch (SVICS - KADI) Show/Hide Program
/* OVERLOAD ALL THE 4 ARITHMATIC OPERATORS TO OPERATE ON A VECTOR CLASS AND
THE OVERLOAD THE * OPERATOR TO MULTIPLY SCALER VALUES TO THE VECTOR CLASS.
OVERLOAD THE >> OPERATOR TO INPUT A VECTOR.
OVERLOAD THE << OPERATOR TO DIAPLAY THE VECTOR.
OVERLOAD THE [] OPERATOR TO ACCESS INDIVISUAL MEMBER OF VECTOR.
USE DYNAMIC MEMORY ALLOCATION TO ACHIVE THE SOLUTION.
WRITE APPROPRIATE CONSTRUCTOR AND DESTRUCTOR FOR THE CLASS.
#include <iostream.h>
#include <conio.h>
static int N;
class vector
{
int *a;
public:
vector() {}
~vector() {}
void display();
vector operator *(int);
int operator [](int);
friend istream& operator >>(istream &in,vector &v);
friend ostream& operator <<(ostream &out,vector &v);
};
istream & operator >>(istream &in,vector &x)
{
cout << "\nEnter howmany elements=";
in >> N;
x.a=new int[N];
cout << "\nEnter elements of vector.\n\n";
for(int i=0;i<N;i++)
{
cout << "Enter " << i+1 << " Element=> ";
in >> x.a[i];
}
return in;
}
int vector :: operator [](int index)
{
return a[index-1];
}
ostream & operator <<(ostream &out,vector &x)
{
out << "\nElements are under below.\n";
out << "(";
int i=0;
out << x.a[i];
for(i=1;i<N;i++)
{
out << "," << x.a[i];
}
out << ")" << endl;
return out;
}
vector vector :: operator *(int x)
{
vector v;
for(int i=0;i<N;i++)
{
v.a[i]=a[i]*x;
}
return v;
}
void main()
{
clrscr();
vector v;
cin >> v;
int ch;
do
{
clrscr();
cout << "\n1..Scaler multiplication.";
cout << "\n2..Display the vector.";
cout << "\n3..Access a purticular member.";
cout << "\n4..Exit.";
cout << "\nEnter choice what u want=";
cin >> ch;
int s;
switch(ch)
{
case 1: cout << "\nMultiply vector with scalar value.";
cout << "\nSo Enter scaler value=";
cin >> s;
v = v * s; // scaler multiplication
getch();
break;
case 2: cout << v;
getch();
break;
case 3: int x;
cout << "Enter position of element in vector= ";
cin >> x;
int temp;
temp= v[x];
cout << endl << x <<" position value is= " << temp;
getch();
break;
case 4: return;
default: cout << "\nInvalid choice.";
getch();
}
}while(ch);
getch();
}
(8) Write a menu driven program that can perform the following functions...
Shared By:Neelam Patel (SRIMCA - Tarsadi) Show/Hide Program
/* Write a menu driven program that can perform the following functions on strings.
(Use overloaded operators where possible)(Do not use predefined string class )
1. Compare two strings for equality (== operator)
2. Check whether first string is smaller than the second (<= operator)
3. Copy the string to another
4. Extract a character from the string (Overload [])
5. Reverse the string
6. Concatenate two strings (+ operator)
solution : */
#include<iostream.h>
#include<conio.h>
#define MAX 100
class str_opr
{
char str[MAX];
public :
str_opr () { }
void getdata();
void display();
str_opr strrev();
str_opr operator +(str_opr s2);
int operator ==(str_opr s2);
void operator =(str_opr s1);
char operator [](int pos);
int operator <=(str_opr s2);
~str_opr (){}
};
int strlength(char str[])
{
int i=0;
while(str[i]!='\0')
{
i++;
}
return i;
}
int str_opr :: operator <= (str_opr s2)
{
int f=0;
if(strlength(str) < strlength(s2.str))
f=1 ;
else if(strlength(str) == strlength(s2.str))
{
int i=0;
while(str[i]!='\0')
{
if(str[i] == s2.str[i] )
f=2;
else if(str[i] < s2.str[i])
f=1;
else
f=0;
i++;
}
}
else
f=0;
return f;
}
str_opr str_opr :: operator +(str_opr s2)
{
str_opr ans;
int i=0,j=0;
while(str[i] != '\0')
{
ans.str[j++]=str[i];
i++;
}
i=0;
while(s2.str[i] != '\0')
{
ans.str[j++]=s2.str[i];
i++;
}
ans.str[j]='\0';
return ans;
}
char str_opr :: operator [](int pos)
{
int i=0;char ch;
while(str[i] != '\0')
{
if( i==pos )
{
return str[i];
}
else
i++;
}
if(str[i] == '\0')
{
cout<<"position is invalid !!!";
return 0;
}
}
int str_opr :: operator ==(str_opr s2)
{
int i=0,f=0;
while(str[i]!='\0' || s2.str[i] != '\0')
{
if(str[i] == s2.str[i] )
f=1;
else
f=0;
i++;
}
return f;
}
void str_opr :: operator =(str_opr s1)
{
int i=0,j=0;
while(s1.str[i] != '\0')
{
str[j++]=s1.str[i];
i++;
}
str[j]='\0';
}
void str_opr :: getdata()
{
cout<<"Enter string : ";
cin >> str;
}
void str_opr :: display()
{
cout<< str;
}
str_opr str_opr :: strrev()
{
int i;
str_opr s;
for(i=0;i<strlength(str);i++)
s.str[strlength(str)-1-i]=str[i];
s.str[i]='\0';
return s;
}
void main()
{
str_opr s1,s2,s3;
int ch,pos;
do
{
clrscr();
cout<<"String operations using operator overloading :------- \n";
cout<<"1.Concatation \n";
cout<<"2.Copy one string\n";
cout<<"3.Assignment \n";
cout<<"4.Search character at position \n";
cout<<"5.find smaller string \n";
cout<<"6.Reverse String \n";
cout<<"7.Exit......\n";
cout<<"Enter your choice ....... :? ";
cin>>ch;
switch(ch)
{
case 1 : s1.getdata();
s2.getdata();
s3=s1+s2;
cout <<"After concatation string is : \n";
s3.display();
break ;
case 2 : s1.getdata();
s2.getdata();
if(s1==s2)
cout << "Strings are equal !! ";
else
cout << "Strings are not equal !! ";
break ;
case 3 : s1.getdata();
s2=s1;
cout <<"After Assignment to s2: \n";
s2.display();
cout<<endl;
s2.display();
break ;
case 4 : s1.getdata();
cout<<"Enter position : ";
cin>>pos;
cout<<s1[pos];
break;
case 5: s1.getdata();
s2.getdata();
if(s1==s2)
cout << "Strings are equal !! ";
else if(s1<=s2)
{
s1.display();
cout<<" less then ";
s2.display();
}
else
{
s1.display();
cout<<" greater then ";
s2.display();
}
break;
case 6 : s1.getdata();
s2=s1.strrev();
s2.display();
break;
case 7: cout<<"Exiting..........";
break;
default : cout <<"invalid choice !!! ";
}
getch();
}while(ch !=7);
}
Shared By:Mr.Adarsh Patel (Lecturer - AIIS - Anand) Show/Hide Program
/*
Written by :Adarsh Patel-This is Sample Program..
Write a menu driven program that can perform
the following functions on strings. (Use overloaded operators where possible).
********** (Do not use predefined string class ) ********
1. Compare two strings for equality (== operator)
2. Check whether first string is smaller than the second (<= operator)
3. Copy the string to another
4. Extract a character from the string (Overload [])
5. Reverse the string
6. Concatenate two strings (+ operator)
*/
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
class Strings
{
private:
char str[100];
public:
Strings()
{
cout << "Enter String : ";
cin >> str;
}
int operator ==(Strings s)
{
int i=0,flag=1;
while(str[i]!=NULL || s.str[i]!=NULL)
{
if(str[i]!=s.str[i])
{
flag=0;
break;
}
i++;
}
return flag;
}
int operator <=(Strings s)
{
int i=0,flag=1;
while(str[i]!=NULL || s.str[i]!=NULL)
{
if(str[i]>s.str[i])
{
flag=0;
break;
}
i++;
}
return flag;
}
void scopy(Strings s1, Strings s2)
{
int i=0;
while(s2[i]!=NULL)
{
s1[i]=s2[i];
i++;
}
return flag;
}
char operator [](int index)
{
}
Strings sreverse(Strings s1)
{
}
Strings operator +(Strings s)
{
}
};
void main()
{
clrscr();
Strings s1,s2;
if(s1==s2)
cout << "Both String are same";
else
cout << "String are not same";
if(s1<=s2)
cout << "s1 is less than s2";
else
cout << "s2 is less than s1";
getch();
}
Shared By :Mohsin Baloch (SVICS - KADI) Show/Hide Program
/* WRITE A MENU DRIVEN PROGRAM THAT CAN PERFORM THE
FOLLOWING FUNCTION ON STRING.
{USING OPERATOR OVERLOADING}
1...COMPARE(==).
2...CHECK SMALLEST STRING(<=).
3...COPY STRING.
4...EXTRACT CHARACTER FROM STRING([]).
5...REVERSE THE STRING.
6...CONCATE STRING(+).*/
#include <iostream.h>
#include <conio.h>
#include <string.h>
class string
{
char s[20];
public:
string()
{
cin >> s;
}
int operator ==(string);
void operator +(string);
void operator |(string);
int operator <=(string);
char operator [](int);
void reverse();
friend void display(string,string);
};
char string :: operator [](int index)
{
char temp;
cout << "\nString is=" << s << endl;
temp=s[index-1];
return temp;
}
void string :: reverse()
{
int len=strlen(s);
char *temp=new char[len];
int i,j;
for(j=0,i=len-1;i>=0;i--,j++)
temp[j]=s[i];
temp[j]='\0';
strcpy(s,temp);
}
int string :: operator <=(string s1)
{
int l1=strlen(s);
int l2=strlen(s1.s);
if(l1<=l2)
return 1;
else
return 0;
}
void string :: operator +(string s1)
{
strcat(s,s1.s);
}
void string :: operator |(string s1)
{
strcpy(s,s1.s);
}
int string::operator ==(string s1)
{
int temp;
display(s1,*this);
temp=strcmp(s,s1.s);
return temp;
}
void display(string s1,string s2)
{
cout << "\nString S1 is= " <<s1.s;
cout << "\nString S2 is= " <<s2.s<<endl;
}
void main()
{
int ch;
clrscr();
cout <<"\nEnter 1st string=";
string s1;
cout <<"\nEnter 2nd string=";
string s2;
getch();
do
{
clrscr();
cout << "\n1..Compare 2 string(==).";
cout << "\n2..Check the string which is lower(<=).";
cout << "\n3..Copy 2 string(|).";
cout << "\n4..Extract character from string.";
cout << "\n5..Reverse a string.";
cout << "\n6..Concate 2 string(+).";
cout << "\n7..Display string.";
cout << "\n8..Exit.";
cout << "\nEnter choice=";
cin >> ch;
switch(ch)
{
case 1: if(s1==s2)
{
cout << "\nBoth string are not equal.";
}
else
{
cout << "\nBoth string are equal.";
}
getch();
break;
case 2: display(s1,s2);
if(s1<=s2)
{
cout << "\nString s1 is smaller than s2.";
}
else
{
cout << "\nString s2 is smaller than s1.";
}
getch();
break;
case 3: s1|s2;
cout << "\nAfter copy 1 string to another.";
display(s1,s2);
getch();
break;
case 4: cout << "Enter position of character u want to extract in S1=";
int temp;
cin >> temp;
char extract;
extract=s1[temp];
cout << temp <<" number charecter is=" << extract;
getch();
break;
case 5: s1.reverse();
s2.reverse();
cout << "After reverse the string are under.";
display(s1,s2);
getch();
break;
case 6: s1+s2;
cout << "\nAfter concate 2 string.";
display(s1,s2);
getch();
break;
case 7: display(s1,s2);
getch();
break;
case 8: return;
default: cout << "Ur choice is invalid.";
getch();
}
}while(ch);
getch();
}
Shared By :Sonam Patel(SRIMCA college-Tarsadi) Show/Hide Program
//Menu-Driven Progam for String.
#include<iostream.h>
#include<conio.h>
class String
{
char a[50];
public:
String()
{
a[0]='\0';
}
void get()
{
cout<<"Enter the String: ";
cin>>a;
}
void operator ==(String s2);
void operator <=(String s2);
void copy(String s2);
char operator [](int i)
{
return a[i];
}
void reverse();
void operator +(String s2);
void show()
{
cout<<endl<<a;
}
};
void String :: operator ==(String s2)
{
for(int l1=0;a[l1]!='\0';l1++);
for(int l2=0;s2.a[l2]!='\0';l2++);
if(!(l1 != l2))
{
for(int i=0;i<l1;i++)
{
if(a[i]!=s2.a[i])
{
cout<<"Both Strings are Different";
break;
}
}
if(i==l1)
cout<<"Both Strings are Equal";
}
else
cout<<"Both Strings are Different";
}
void String :: operator <=(String s2)
{
for(int l1=0;a[l1]!='\0';l1++);
for(int l2=0;s2.a[l2]!='\0';l2++);
if(l1<=l2)
cout<<endl<<"First String is Smaller than Second String";
else
cout<<endl<<"First String is Larger than Second String";
}
void String :: copy(String s2)
{
for(int i=0;s2.a[i]!='\0';i++)
a[i]=s2.a[i];
cout<<endl<<"Second String is copied into First String";
}
void String :: reverse()
{
char s[50];
for(int i=0;a[i]!='\0';i++);
i--;
for(int j=0;i>=0;i--,j++)
{
s[j]=a[i];
}
s[j]='\0';
for(i=0;i<=j;i++)
a[i]=s[i];
}
void String :: operator +(String s2)
{
for(int i=0;a[i]!='\0';i++);
int j=0;
do
{
a[i]=s2.a[j];
j++;
i++;
}while(s2.a[j]!='\0');
a[i]='\0';
cout<<endl<<"First String is concated with Second String";
}
void main()
{
String s1,s2;
int p,ch;
char c;
clrscr();
s1.get();
s2.get();
do
{
do
{
clrscr();
cout<<endl<<"1. Compare two strings for equality ==";
cout<<endl<<"2. Check whether first string is smaller than the second <=";
cout<<endl<<"3. Copy the string to another";
cout<<endl<<"4. Extract a character from the string []";
cout<<endl<<"5. Reverse the string";
cout<<endl<<"6. Concatenate two strings +"<<endl;
cout<<"7. Show Both Strings"<<endl<<"8. Exit";
cout<<endl<<"Enter Your Choice: ";
cin>>ch;
}while(ch<1 || ch>8);
switch(ch)
{
case 1:
s1==s2;
getch();
break;
case 2:
s1<=s2;
getch();
break;
case 3:
s1.copy(s2);
getch();
break;
case 4:
cout<<"Enter Position: ";
cin>>p;
c=s1[p];
cout<<endl<<"Extracted character is "<<c;
c=s2[p];
cout<<endl<<"Extracted character is "<<c;
getch();
break;
case 5:
s1.reverse();
s2.reverse();
cout<<endl<<"Both the Strings are Reversed";
getch();
break;
case 6:
s1+s2;
getch();
break;
case 7:
s1.show();
s2.show();
getch();
}
}while(ch!=8);
}
(9) Overload subscript operator [] for a array class.
Shared By:Mr.Adarsh Patel (Lecturer - AIIS - Anand) Show/Hide Program
/*
Written by :Adarsh Patel-This is Sample Program..
Overload subscript operator [] for a array class.......
*/
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
class Array
{
private:
char str[100];
public:
Array()
{
cout << "Enter String : ";
cin >> str;
}
char operator [](int i)
{
return str[i];
}
};
void main()
{
clrscr();
int ind;
Array a1;
cout << "Enter Index : ";
cin >> ind;
cout << "Character at : " << ind << " Is : " << a1[ind];
getch();
}
Shared By:Mohsin Baloch (SVICS - KADI) Show/Hide Program
//OVERLOAD SUBSTRING OPERATOR[] FOR A ARRAY CLASS.
#include <iostream.h>
#include <conio.h>
class subscript
{
int a[5];
public:
subscript()
{
a[0]=1;
a[1]=2;
a[2]=3;
a[3]=4;
a[4]=5;
}
int operator [](int index)
{
int temp;
temp=a[index];
return temp;
}
};
void main()
{
subscript s;
clrscr();
int temp[5];
cout << "Array are under below.";
for(int i=0;i<5;i++)
{
temp[i]=s[i];
cout << endl << temp[i];
}
getch();
}
Shared By:Sonam Patel(SRIMCA college-Tarsadi) Show/Hide Program
//Overload subscript operator [] for an Array class.
#include<iostream.h>
#include<conio.h>
class Array
{
int a[10];
public:
Array();
int operator [](int i)
{
return a[i];
}
void get();
void show();
};
Array :: Array()
{
for(int i=0;i<10;i++)
a[i]=i+1;
}
void Array :: get()
{
for(int i=0;i<10;i++)
{
cout<<"Enter Value: ";
cin>>a[i];
}
}
void Array :: show()
{ cout<<"Array: ";
for(int i=0;i<10;i++)
cout<<" "<<a[i];
}
void main()
{
Array arr;
int n;
clrscr();
arr.get();
arr.show();
cout<<endl<<"Enter Index value: ";
cin>>n;
cout<<endl<<"The Value on Index "<<n<<" is ";
n=arr[n];
cout<<n;
getch();
}
(10) Overload function call operator () to allow the more common...
Shared By:Mr.Adarsh Patel (Lecturer - AIIS - Anand) Show/Hide Program
/*
Written by :Adarsh Patel-This is Sample Program..
Overload function call operator () to allow the more common form of
double array subscripting. Instead of saying x[row][column] for
an array of objects, overload the function call operator to allow
the alternate form x(row, column)
*/
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
class Array
{
private:
int no[10][10],n;
public:
Array()
{
int i;
for(i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout << "No[" << i <<"][" << j << "] :";
cin >> no[i][j];
}
}
}
int operator ()(int r,int c) \
{
return no[r][c];
}
};
void main()
{
clrscr();
int indx,indy;
Array a1;
cout << "Enter Index (x and y): ";
cin >> indx >> indy;
cout << "No[" << indx << "][" << indy << "]:" << a1(indx,indy);
getch();
}
Shared By:Mohsin Baloch (SVICS - KADI) Show/Hide Program
/*OVERLOAD FUNCTION CALL OPERATOR() TO ALLOW
THE MORE COMMON FORM OF DOUBLE ARRAY.
INSTEAD OF SAYING A[a][b], USE A(a,b).
#include <iostream.h>
#include <conio.h>
class test
{
int a[2][2];
public:
test();
int operator ()(int r,int c)
{
return a[r][c];
}
};
test :: test()
{
cout << "\n\nEnter the value of 2D array.\n\n";
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
cout << "Enter" << i+1 << " row and " << j+1 <<" Column=";
cin >> a[i][j];
}
}
}
void main()
{
clrscr();
test t;
int x[2][2];
for(int row=0;row<2;row++)
{
for(int column=0;column<2;column++)
{
x[row][column]=t(row,column);
}
}
cout << "\nDisplay 2D Array.\n\n";
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
cout << x[i][j] << " ";
}
cout << endl << endl;
}
getch();
}
Shared By:Sonam Patel(SRIMCA college-Tarsadi) Show/Hide Program
//Overload function call operator () to allow the more common form of double array subscripting.
//Instead of saying x[row][column] for an array of objects, allow the alternate form x(row, column)
#include<iostream.h>
#include<conio.h>
class Array
{
int a[5][5];
public:
Array();
int operator ()(int i,int j)
{
return a[i][j];
}
void get();
void show();
};
Array :: Array()
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
a[i][j]=i+j+1;
}
}
void Array :: get()
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
cout<<"Enter Value: ";
cin>>a[i][j];
}
}
}
void Array :: show()
{ cout<<endl;
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
cout<<" "<<a[i][j];
cout<<endl;
}
}
void main()
{
Array arr;
int r,c,n;
clrscr();
arr.get();
clrscr();
arr.show();
cout<<endl<<"Enter RowIndex: ";
cin>>r;
cout<<"Enter ColumnIndex: ";
cin>>c;
n=arr(r,c);
cout<<"The Element on Index A("<<r<<","<<c<<") is "<<n;
getch();
}
(11) Define a singly linked list class, which is a made up objects...
Shared By:Mr.Adarsh Patel (Lecturer - AIIS - Anand) Show/Hide Program
/*
Written by :Adarsh Patel-This is Sample Program..
Define a singly linked list class, which is a made up
objects of node class. Provide addition, deletion of nodes,
with operator overloading.
*/
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
class Node
{
private:
int no;
Node *next;
public:
Node()
{
cout << "Constructor called..." << endl;
cout << "Enter value of N : ";
cin >> no;
next=NULL;
}
void operator ++()
{
Node *p;
p=this;
cout << ".....operator ++ Called..." << endl;
while(p->next!=NULL)
p=p->next;
p->next = new Node();
}
void operator --()
{
// cout << "..... display ....." << endl;
Node *t;
while( (t->next)->next!=NULL)
{
t = t->next;
}
t->next=NULL;
}
void display(Node *t)
{
cout << "..... display ....." << endl;
while(t->next!=NULL)
{
cout << t->no << " ";
t = t->next;
}
cout << t->no << " ";
}
};
void main()
{
clrscr();
Node *n,*node;
int choice;
n = new Node();
// *node = *n;
do
{
cout << endl << "1 : Create";
cout << endl << "2 : Display";
cout << endl << "3 : Delete";
cout << endl << "0 : Exit";
cout << endl << endl << "Enter Your Choice : ";
cin >> choice;
switch(choice)
{
case 1:
(*n)++;
break;
case 2:
n->display(n);
break;
case 3:
(*n)--;
break;
}
}while(choice!=0);
}
Shared By :Your Name Show/Hide Program
Share This Program..
send it gtumca@gmail.com
with your name - college name..
and share what you want ... at same mail id...
Thanx in advance...
Be connected...
D_i_Z
(12) Define a matrix class, which allows addition, subtraction,...
Shared By:Mr.Adarsh Patel (Lecturer - AIIS - Anand) Show/Hide Program
/*
Written by :Adarsh Patel-This is Sample Program..
Define a matrix class,
which allows
addition,
subtraction,
multiplication with another matrix,
multiplication with a scalar value, and
inverse of a matrix( Use operator overloading).
Use dynamic constructors and destructors for allocation and
deallocation of memory.
*/
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
class Matrix
{
private:
float *m[10]; int r,c;
public:
Matrix()
{
cout << "Constructor called..." << endl;
cout << "Enter value of Rows and Columns : ";
cin >> r >> c;
m[0] = new float[c];
m[1] = new float[c];
m[2] = new float[c];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout << " M [" << i << "][" << j << "] : ";
cin >> m[i][j];
}
}
}
Matrix(int r1,int c1)
{
// cout << "Constructor called..." << endl;
// cout << "Enter value of Rows and Columns : ";
// cin >> r >> c;
r = r1;
c = c1;
m[0] = new float[c];
m[1] = new float[c];
m[2] = new float[c];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
m[i][j]=0;
}
}
}
Matrix operator +(Matrix b)
{
Matrix t(r,c);
if( r != b.r || c != b.c )
{
cout << " Row and column should be same" << endl;
return t;
}
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
t.m[i][j] = m[i][j] + b.m[i][j];
}
}
return t;
}
Matrix operator -(Matrix b)
{
Matrix t(r,c);
if( r != b.r || c != b.c )
{
cout << " Row and column should be same" << endl;
return t;
}
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
t.m[i][j] = m[i][j] - b.m[i][j];
}
}
return t;
}
Matrix operator *(Matrix b)
{
Matrix t(r,c);
if( r != b.r || c != b.c )
{
cout << " Row and column should be same" << endl;
return t;
}
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
t.m[i][j]=0;
for(int k=0;k<r;k++)
t.m[i][j] = t.m[i][j] + ( m[i][k] * b.m[k][j] );
}
}
return t;
}
Matrix operator / (Matrix b)
{
Matrix t(r,c);
if( r != b.r || c != b.c )
{
cout << " Row and column should be same" << endl;
return t;
}
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
t.m[i][j] = m[i][j] / b.m[i][j];
}
}
return t;
}
void display()
{
cout << endl << " ... Matrix ..." << endl;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout << m[i][j] << " ";
}
cout << endl;
}
}
};
void main()
{
clrscr();
Matrix m1,m2,m3;
m1.display();
m2.display();
m3.display();
m3 = m2 + m1;
cout << endl << "... After Adition ..." << endl;
m3.display();
m3 = m1 - m2;
cout << endl << "... After Subtraction ..." << endl;
m3.display();
m3 = m1 * m2;
cout << endl << "... After Multiplication ..." << endl;
m3.display();
m3 = m1 / m2;
cout << endl << "... After Division ..." << endl;
m3.display();
getch();
}
Shared By:Mohsin Baloch (SVICS - KADI) Show/Hide Program
/*DEFINE MATRIX CLASS ALLOW ADDITION, SUBTRACTION,
MULTIPLICATION, MULTIPLICATION WITH SCALER
VALUE AND INVERSE THE MATRIX.(USING OPERATOR OVERLOADING)
USE DYNAMIC CONSTRUCTOR AND DESTRUCTOR FOR ALLOCATE OR DEALLOCATE MEMORY.*/
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <stdio.h>
class matrix
{
float a[3][3];
public:
matrix()
{
a[0][0]=0;
a[0][1]=0;
a[0][2]=0;
a[1][0]=0;
a[1][1]=0;
a[1][2]=0;
a[2][0]=0;
a[2][1]=0;
a[2][2]=0;
}
void getdata();
matrix operator +(matrix);
matrix operator -(matrix);
matrix operator *(int);
matrix operator *(matrix);
matrix inverse();
void display();
};
matrix matrix :: inverse()
{
matrix B;//the transpose of a matrix A
matrix C;//the adjunct matrix of transpose of a matrix A not adjunct of A
matrix X;//the inverse
int i,j;
float x,n=0;//n is the determinant of A
for(i=0,j=0;j<3;j++)
{
if(j==2)
n+=a[i][j]*a[i+1][0]*a[i+2][1];
else if(j==1)
n+=a[i][j]*a[i+1][j+1]*a[i+2][0];
else
n+=a[i][j]*a[i+1][j+1]*a[i+2][j+2];
}
for(i=2,j=0;j<3;j++)
{
if(j==2)
n-=a[i][j]*a[i-1][0]*a[i-2][1];
else if(j==1)
n-=a[i][j]*a[i-1][j+1]*a[i-2][0];
else
n-=a[i][j]*a[i-1][j+1]*a[i-2][j+2];
}
cout << "\n\nThe determinant of matrix A is= " << n;
getch();
if(n!=0) x=1.0/n;
else
{
cout << "\nDivision by 0, not good!\n";
return *this;
}
printf("\n========== The transpose of a matrix A ==============================\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
B.a[i][j]=a[j][i];
}
}
B.display();
getch();
C.a[0][0]=B.a[1][1]*B.a[2][2]-(B.a[2][1]*B.a[1][2]);
C.a[0][1]=(-1)*(B.a[1][0]*B.a[2][2]-(B.a[2][0]*B.a[1][2]));
C.a[0][2]=B.a[1][0]*B.a[2][1]-(B.a[2][0]*B.a[1][1]);
C.a[1][0]=(-1)*(B.a[0][1]*B.a[2][2]-B.a[2][1]*B.a[0][2]);
C.a[1][1]=B.a[0][0]*B.a[2][2]-B.a[2][0]*B.a[0][2];
C.a[1][2]=(-1)*(B.a[0][0]*B.a[2][1]-B.a[2][0]*B.a[0][1]);
C.a[2][0]=B.a[0][1]*B.a[1][2]-B.a[1][1]*B.a[0][2];
C.a[2][1]=(-1)*(B.a[0][0]*B.a[1][2]-B.a[1][0]*B.a[0][2]);
C.a[2][2]=B.a[0][0]*B.a[1][1]-B.a[1][0]*B.a[0][1];
printf("\n========== The adjunct matrix of transpose of the matrix A ==========\n");
C.display();
getch();
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
X.a[i][j]=C.a[i][j]*x;
}
}
printf("\n========== The inverse matrix of the matrix you entered!!! ==========\n");
X.display();
return X;
}
matrix matrix :: operator *(int x)
{
matrix temp;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
temp.a[i][j] = x * a[i][j];
}
}
return temp;
}
matrix matrix :: operator +(matrix x)
{
matrix temp;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
temp.a[i][j] = a[i][j] + x.a[i][j];
}
}
return temp;
}
matrix matrix :: operator *(matrix x)
{
matrix temp;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
float sum=0;
for(int k=0;k<3;k++)
{
sum=sum+(a[i][k]*x.a[k][j]);
temp.a[i][j]=sum;
}
}
}
return temp;
}
matrix matrix :: operator -(matrix x)
{
matrix temp;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
temp.a[i][j] = a[i][j] - x.a[i][j];
}
}
return temp;
}
void matrix :: getdata()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout << "Enter" << i+1 << " row and " << j+1 <<" Column=";
cin >> a[i][j];
}
}
}
void matrix :: display()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout << setw(5) << a[i][j] << " ";
}
cout << endl;
}
}
void main()
{
clrscr();
matrix t1,t2,t3;
int ch;
cout << "\n\nEnter the value of 3D array 1.\n\n";
t1.getdata();
cout << "\n\nEnter the value of 3D array 2.\n\n";
t2.getdata();
do
{
clrscr();
cout << "\n1..Addition.";
cout << "\n2..Subtraction.";
cout << "\n3..Multiplication.";
cout << "\n4..Scalar Multiplication.";
cout << "\n5..Inverse Matrix.";
cout << "\n6..Display.";
cout << "\n7..Exit.";
cout << "\nEnter choice to what u want to do?";
cin >> ch;
switch(ch)
{
case 1: t3 = t1 + t2;
getch();
break;
case 2: t3 = t1 - t2;
getch();
break;
case 3: t3 = t1 * t2;
getch();
break;
case 4: int a;
cout << "Enter Scaler value= ";
cin >> a;
t3 = t1 * a;
cout << "\nAfter Scaler Multiplication by= " << a;
cout << "\nArray1=";
t1.display();
cout << "\nArray3=";
t3.display();
getch();
break;
case 5: t3 = t1.inverse();
getch();
break;
case 6: cout << "\nArray1=";
t1.display();
cout << "\nArray2=";
t2.display();
cout << "\nArray3=";
t3.display();
getch();
break;
case 7: return;
default: cout << "\nInvalid choice. Try 2 correct it.";
}
}while(ch);
getch();
}
(13) Construct a class distance having member variables int feets...
Shared By: DHAVAL M. DEVANI(B.H.GARDI COLLEGE - Rajkot) Show/Hide Program
#include<iostream>
using namespace std;
class len;
class dist
{
int feet;
int inch;
public:
void getdata()
{
cout<<" Enter Feet:";
cin>>feet;
cout<<" Enter Inch:";
cin>>inch;
}
operator int()
{
return inch+(feet*12);
}
dist (int f,int i)
{
feet=f;
inch=i;
}
double getfeet()
{
return feet;
}
double getinch()
{
return inch;
}
void show()
{
cout<<endl<<feet;
cout<<endl<<inch;
}
};
class len
{
int meters;
int centimeter;
public:
len(int x=0, int y=0)
{
meters=x;
centimeter=y;
}
len(dist d)
{
int feet=d.getfeet();
int inch=d.getinch ();
meters=feet;
centimeter=inch;
}
void show()
{
cout<<meters<<endl;
cout<<centimeter<<endl;
}
operator len()
{
return meters+(centimeter*100);
}
};
void main()
{
class dist c=dist(20,30);
cout<<" CONVERSION FROM INT TO OBJECT DATATYPE"<<endl;
cout<<c;
cout<<endl;
c.getdata();
cout<<" CONVERSION FROM OBJECT TO INT DATATYPE"<<endl;
cout<<c;
cout<<endl;
len l(10,20);
len l2;
l2=c;
l2.show();
c.show();
flushall();
cin.get();
}
Shared By:Mr.Adarsh Patel (Lecturer - AIIS - Anand) Show/Hide Program
/*
Written by :Adarsh Patel-This is Sample Program..
Construct a class distance having member variables int feets and int inches.
Design the class to make it possible :
(i) To convert this class into the basic data type int
which will represent the total no. of inches of the class.
(ii) To convert a basic data type int to distance class
(iii) To convert distance class to length class having member
variables int meters and int centimeters
Write a C++ program to test your class.
*/
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
class Length;
class Distance;
class Length
{
private:
int meter,centimeter;
public:
Length(int m,int c)
{
meter=m;
centimeter=c;
}
void display()
{
cout << endl << "Meter is : " << meter;
cout << endl << "Centimeter is : " << centimeter;
}
};
class Distance
{
private:
int feet,inch;
public:
Distance()
{
cout << "Constructor called..." << endl;
cout << "Enter distance feets and inches : ";
cin >> feet >> inch;
}
operator int()
{
return ((feet*12)+inch);
}
void operator =(int t)
{
feet = (t/12);
inch = t-(feet*12);
}
void display()
{
cout << endl << " ......" << endl;
cout << "Feet : " << feet << endl;
cout << "Inches : " << inch << endl;
}
operator Length()
{
int mm,cc;
int total;
total = (feet*12) + inch;
mm = total/36;
cc = total-(mm*36);
return Length(mm,cc);
}
};
void main()
{
clrscr();
Distance d1,d2;
Length l1(0,0);
// int ans;
// ans = d1;
// d1.display();
// cout << " total inches : " << ans;
d2 = 40;
d2.display();
l1 = d2;
l1.display();
getch();
}
Shared By ::Mohsin Baloch (SVICS - KADI) Show/Hide Program
/*CONSTRUCT CLASS DISTANCE HAVING MEMBER INT FEETS AND INT INCHES.
1...CONVERT BASIC TO DISTANCE CLASS.
2...CONVERT DISTANCE CLASS TO BASIC.
3.. CONVERT DISTANCE CLASS TO LENGTH CLASS
HAVING ELEMENT LIKE METER AND CENTIMETER.*/
#include <iostream.h>
#include <conio.h>
class length;
class distance
{
int feet;
int inches;
public:
distance(int f,int i) //basic to class
{
feet=f;
inches=i;
}
void display()
{
cout << "\nFeet is=" << feet;
cout << "\nInches are=" << inches;
}
operator int() //class to basic
{
return(inches);
}
int getmiter()
{
int temp;
temp = (feet / 34) + (inches / 400);
return(temp);
}
int getcentimiter()
{
int temp;
temp= ((feet / 34) + (inches / 400)) * 1000;
return(temp);
}
};
class length
{
int miters;
int centimiters;
public:
length(){}
length(distance x) //class to class
{
miters = x.getmiter();
centimiters = x.getcentimiter();
}
void display()
{
cout << "\nMetre is=" << miters << "m";
cout << "\nCentimiter are=" << centimiters << "cm";
}
};
void main()
{
//basic to class
int f,i;
clrscr();
cout << "Enter feet=";
cin >> f;
cout << "Enter inches=";
cin >> i;
cout << "\nbasic to class";
distance d1(f,i);
d1.display();
//class to basic
int tempinches;
tempinches=d1;
cout << "\n\nclass to basic\nInches are=" << tempinches;
//class to class
cout << "\n\nclass to class";
length l1;
l1=d1;
l1.display();
getch();
}