Вы находитесь на странице: 1из 19

 Matrix multiplication using operator overloading without friend function

Code:

#include<iostream>
using namespace std;
class Matrix
{
public:int r1,r2,c1,c2,i,j,a[10][10],c[10][10],k;
void getMatrix(int n1,int n2)
{
for(i=0;i<n1;i++)
for(j=0;j<n2;j++)
cin>>a[i][j];
}
void getDimensions()
{
cout<<"Enter dimension of matrix 1=";
cin>>r1>>c1;
cout<<"Enter dimension of matrix 2=";
cin>>r2>>c2;
}
bool check()
{
if(c1==r2)
return true;
else
return false;
}
void operator*(Matrix obj)
{
getDimensions();
if(!check())
cout<<"Multiplication not applicable";
else
{
getMatrix(r1,c1);
obj.getMatrix(r2,c2);
for (i = 0; i < r1; i++)
{
for (j = 0; j < c2; j++)
{
c[i][j] = 0;
for (k = 0; k < r2; k++)
{
c[i][j] += a[i][k] * obj.a[k][j];
}
}
}
cout << "Product of matrices\n";
for (i = 0; i < r1; i++)
{
for (j = 0; j < c2; j++)
cout << c[i][j] << " ";
cout << "\n";
}
}
}
};
int main()
{
Matrix obj1;
Matrix obj2;
obj1*obj2;
}

OUTPUT:

Enter dimension of matrix 1=2 2


Enter dimension of matrix 2=2 2
13
45
56
34
Product of matrices
14 18
35 44
 Matrix multiplication using operator overloading with friend function

Code:

#include<iostream>
using namespace std;
class Matrix
{
public:int r1,r2,c1,c2,i,j,a[10][10],c[10][10],k;
void getMatrix(int n1,int n2)
{
for(i=0;i<n1;i++)
for(j=0;j<n2;j++)
cin>>a[i][j];
}
void getDimensions()
{
cout<<"Enter dimension of matrix 1=";
cin>>r1>>c1;
cout<<"Enter dimension of matrix 2=";
cin>>r2>>c2;
}
bool check()
{
if(c1==r2)
return true;
else
return false;
}
friend void operator*(Matrix,Matrix);
};
void operator*(Matrix obj1,Matrix obj2)
{
obj1.getDimensions();
if(!obj1.check())
cout<<"Multiplication not applicable";
else
{
obj1.getMatrix(obj1.r1,obj1.c1);
obj2.getMatrix(obj1.r2,obj1.c2);
for (obj1.i = 0; obj1.i < obj1.r1; obj1.i++)
{
for (obj1.j = 0; obj1.j < obj1.c2; obj1.j++)
{
obj1.c[obj1.i][obj1.j] = 0;
for (obj1.k = 0; obj1.k < obj1.r2; obj1.k++)
{
obj1.c[obj1.i][obj1.j] += obj1.a[obj1.i][obj1.k] *
obj2.a[obj1.k][obj1.j];
}
}
}
cout << "Product of matrices\n";
for (obj1.i = 0; obj1.i < obj1.r1; obj1.i++)
{
for (obj1.j = 0; obj1.j < obj1.c2; obj1.j++)
cout << obj1.c[obj1.i][obj1.j] << " ";
cout << "\n";
}
}
}
int main()
{
Matrix obj1;
Matrix obj2;
obj1*obj2;
}

OUTPUT:

Enter dimension of matrix 1=2 2


Enter dimension of matrix 2=2 2
13
45
56
34
Product of matrices
14 18
35 44
 Creation of file and menu driven operations

Code:

#include<iostream>
#include<fstream>
#include<string>
#include<cstring>
#include<sstream>
using namespace std;
class File
{
public:ofstream o;
ifstream i;
public:void createFile()
{
o.open("numbers.txt");
o<<"John\t24007896"<<endl;
o<<"Ahmed\t24781507"<<endl;
o<<"Ruchi\t24431258"<<endl;
o.close();
}
void getNumber(char nm[])
{
i.open("numbers.txt");
while(!i.eof())
{
string sent("");
getline(i,sent);
char p[20];
int t=0;
while(sent[t]!='\0')
{
p[t]=sent[t];
t++;
}
char* point;
point=strtok(p,"\t");
if(strcmp(point,nm)==0)
{
cout<<strtok(NULL,"\t")<<endl;
break;
}
}
i.close();
}
void update(char nm[],int n)
{
i.open("numbers.txt");
o.open("numbers1.txt");
while(!i.eof())
{
string sent("");
getline(i,sent);
char p[20];
int t=0;
while(sent[t]!='\0')
{
p[t]=sent[t];
t++;
}
p[t]='\0';
char* point1;
char* point2;
point1=strtok(p,"\t");
point2=strtok(NULL,"\t");
o<<point1<<"\t";
if(strcmp(point1,nm)==0)
o<<n<<endl;
else
o<<point2<<endl;
}
i.close();
o.close();
}
void getName(long int n)
{
i.open("numbers.txt");
stringstream ss;
ss<<n;
string newstring=ss.str();
char news[20];
int k=0;
while(newstring[k]!='\0')
{
news[k]=newstring[k];
k++;
}
news[k]='\0';
while(!i.eof())
{
string sent("");
getline(i,sent);
char p[20];
int t=0;
while(sent[t]!='\0')
{
p[t]=sent[t];
t++;
}
p[t]='\0';
char* point1;
char* point2;
point1=strtok(p,"\t");
point2=strtok(NULL,"\t");
if(strcmp(point2,news)==0)
{
cout<<point1<<endl;
break;
}
}
i.close();
}
};
int main()
{
File ob;
ob.createFile();
int op=1,ch;
char nm[20];
long int n;
while(op==1)
{
cout<<"1.Determine telephone no. by name"<<endl;
cout<<"2.Determine name by telephone no."<<endl;
cout<<"3.Update telephone no."<<endl;
cout<<"4.Exit\nSelect your choice=";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter name=";
cin>>nm;
ob.getNumber(nm);
break;
case 2:
cout<<"Enter number=";
cin>>n;
ob.getName(n);
break;
case 3:
cout<<"Enter name=";
cin>>nm;
cout<<"Enter new number=";
cin>>n;
ob.update(nm,n);
break;
case 4:
op++;
break;
default:cout<<"\nWrong choice\n";
}
}
return 0;
}
 Database of an institution

Code:

#include<iostream>
#include<string.h>
using namespace std;
class Staff
{
int code;
char *nm;
public:void setCode(int n)
{
code=n;
}
void setName(char *a)
{
nm=new char();
strcpy(nm,a);
}
int getCode()
{
return code;
}
char* getName()
{
return nm;
}
};
class Officer:public Staff
{
char grade;
public:void setGrade(char a)
{
grade=a;
}
char getGrade()
{
return grade;
}
};
class Teacher:public Staff
{
char* sub;
char* qual;
public:void setSub(char *s)
{
sub=new char();
strcpy(sub,s);
}
void setQual(char *q)
{
qual=new char();
strcpy(qual,q);
}
char* getSub()
{
return sub;
}
char* getQual()
{
return qual;
}
};
class Typist:public Staff
{
int speed;
public:void setSpeed(int s)
{
speed=s;
}
int getSpeed()
{
return speed;
}
};
class Casual:public Typist
{
double dailywages;
public:void setWages(double w)
{
dailywages=w;
}
double getWages()
{
return dailywages;
}
};
class Regular:public Typist
{
};
class Education:virtual public Teacher,virtual public Officer
{
char* aqual;
char* pqual;
public:void setAqual(char *s)
{
aqual=new char();
strcpy(aqual,s);
}
void setPqual(char *q)
{
pqual=new char();
strcpy(pqual,q);
}
char* getAqual()
{
return aqual;
}
char* getPqual()
{
return pqual;
}
};
int main()
{
Staff st;
Officer of;
Teacher t;
Typist ty;
Casual cs;
Regular re;
Education ed;
int op=1,ch,ch2,op2;
int code,sp;
char* nm=new char();
char* sub=new char();
char* qual=new char();
char gr;
double dw;
char* aqual=new char();
char* pqual=new char();
while(op==1)
{
op2=1;
cout<<"----DATABASE OF FUTURE INSTITUTE-----";

cout<<"\n1.Staff\n2.Teacher\n3.Officer\n4.Typist\n5.Casual\n6.Regular\n7.Education\n8.Exit\n";
cout<<"Select any field=";
cin>>ch;
switch(ch)
{
case 1:
while(op2==1)
{
cout<<"\n1.SET CODE\n2.SET NAME\n3.GET CODE\n4.GET NAME\n5.EXIT";
cin>>ch2;
switch(ch2)
{
case 1:
cout<<"Enter code=";
cin>>code;
st.setCode(code);
break;
case 2:
cout<<"Enter Name=";
cin>>nm;
st.setName(nm);
break;
case 3:
cout<<st.getCode();
break;
case 4:
cout<<st.getName();
break;
case 5:
op2++;
break;
default:cout<<"\nWrong choice\n";
}}
break;

case 2:
while(op2==1){
cout<<"\n1.SET CODE\n2.SET NAME\n3.GET CODE\n4.GET NAME\n5.SET
SUBJECT\n6.SET QUALIICATION\n7.GET SUBJECT\n8.GET QUALIFICATION\n9.EXIT";
cin>>ch2;
switch(ch2)
{
case 1:
cout<<"Enter code=";
cin>>code;
t.setCode(code);
break;
case 2:
cout<<"Enter Name=";
cin>>nm;
t.setName(nm);
break;
case 3:
cout<<t.getCode();
break;
case 4:
cout<<t.getName();
break;
case 5:
cout<<"Enter subject name=";
cin>>sub;
t.setSub(sub);
break;
case 6:
cout<<"Enter qualification=";
cin>>qual;
t.setQual(qual);
break;
case 7:
cout<<t.getSub();
break;
case 8:
cout<<t.getQual();
break;
case 9:
op2++;
break;
default:cout<<"\nWrong choice\n";
}}
break;
case 3:
while(op2==1){
cout<<"\n1.SET CODE\n2.SET NAME\n3.GET CODE\n4.GET NAME\n5.SET
GRADE\n6.GET GRADE\n7.EXIT";
cin>>ch2;
switch(ch2)
{
case 1:
cout<<"Enter code=";
cin>>code;
of.setCode(code);
break;
case 2:
cout<<"Enter Name=";
cin>>nm;
of.setName(nm);
break;
case 3:
cout<<of.getCode();
break;
case 4:
cout<<of.getName();
break;
case 5:
cout<<"Enter grade=";
cin>>gr;
of.setGrade(gr);
break;
case 6:
cout<<of.getGrade();
break;
case 7:
op2++;
break;
default:cout<<"\nWrong choice\n";
}}
break;
case 4:
while(op2==1){
cout<<"\n1.SET CODE\n2.SET NAME\n3.GET CODE\n4.GET NAME\n5.SET
SPEED\n6.GET SPEED\n7.EXIT";
cin>>ch2;
switch(ch2)
{
case 1:
cout<<"Enter code=";
cin>>code;
ty.setCode(code);
break;
case 2:
cout<<"Enter Name=";
cin>>nm;
ty.setName(nm);
break;
case 3:
cout<<ty.getCode();
break;
case 4:
cout<<ty.getName();
break;
case 5:
cout<<"Enter wpm speed=";
cin>>sp;
ty.setSpeed(sp);
break;
case 6:
cout<<ty.getSpeed()<<" wpm";
break;
case 7:
op2++;
break;
default:cout<<"\nWrong choice\n";
}}
break;
case 5:
while(op2==1){
cout<<"\n1.SET CODE\n2.SET NAME\n3.GET CODE\n4.GET NAME\n5.SET
SPEED\n6.GET SPEED\n7.SET DAILY WAGES\n8.GET DAILY WAGES\n9.EXIT";
cin>>ch2;
switch(ch2)
{
case 1:
cout<<"Enter code=";
cin>>code;
cs.setCode(code);
break;
case 2:
cout<<"Enter Name=";
cin>>nm;
cs.setName(nm);
break;
case 3:
cout<<cs.getCode();
break;
case 4:
cout<<cs.getName();
break;
case 5:
cout<<"Enter wpm speed=";
cin>>sp;
cs.setSpeed(sp);
break;
case 6:
cout<<cs.getSpeed()<<" wpm";
break;
case 7:
cout<<"Enter daily wages=";
cin>>dw;
cs.setWages(dw);
break;
case 8:
cout<<cs.getWages();
break;
case 9:
op2++;
break;
default:cout<<"\nWrong choice\n";
}}
break;
case 6:
while(op2==1){
cout<<"\n1.SET CODE\n2.SET NAME\n3.GET CODE\n4.GET NAME\n5.SET
SPEED\n6.GET SPEED\n7.EXIT";
cin>>ch2;
switch(ch2)
{
case 1:
cout<<"Enter code=";
cin>>code;
re.setCode(code);
break;
case 2:
cout<<"Enter Name=";
cin>>nm;
re.setName(nm);
break;
case 3:
cout<<re.getCode();
break;
case 4:
cout<<re.getName();
break;
case 5:
cout<<"Enter wpm speed=";
cin>>sp;
re.setSpeed(sp);
break;
case 6:
cout<<re.getSpeed()<<" wpm";
break;
case 7:
op2++;
break;
default:cout<<"\nWrong choice\n";
}}
break;
case 7:
while(op2==1){
cout<<"\n1.SET CODE\n2.SET NAME\n3.GET CODE\n4.GET NAME\n5.SET
SUBJECT\n6.SET QUALIICATION\n7.GET SUBJECT\n8.GET QUALIFICATION\n9.SET GRADE\n10.GET
GRADE\n11.SET ACADEMIC QUALIFICATION\n12.SET PROFESSIONAL QUALIFICATION\n13GET ACADEMIC
QUALIFICATION\n14.GET PROFESSIONAL QUALIFICATION.15.EXIT";
cin>>ch2;
switch(ch2)
{
case 1:
cout<<"Enter code=";
cin>>code;
ed.Teacher::setCode(code);
break;
case 2:
cout<<"Enter Name=";
cin>>nm;
ed.Teacher::setName(nm);
break;
case 3:
cout<<ed.Teacher::getCode();
break;
case 4:
cout<<ed.Teacher::getName();
break;
case 5:
cout<<"Enter subject name=";
cin>>sub;
ed.setSub(sub);
break;
case 6:
cout<<"Enter qualification=";
cin>>qual;
ed.setQual(qual);
break;
case 7:
cout<<ed.getSub();
break;
case 8:
cout<<ed.getQual();
break;
case 9:
cout<<"Enter grade=";
cin>>gr;
ed.setGrade(gr);
break;
case 10:
cout<<ed.getGrade();
break;
case 11:
cout<<"Enter academic qualification=";
cin>>aqual;
ed.setAqual(aqual);
break;
case 12:
cout<<"Enter professional qualification=";
cin>>pqual;
ed.setPqual(pqual);
break;
case 13:
cout<<ed.getAqual();
break;
case 14:
cout<<ed.getPqual();
break;
case 15:
op2++;
break;
default:cout<<"\nWrong choice\n";
}}
break;
case 8:
op++;
break;
default:cout<<"\nWrong Choice\n";
}

}
}

Output:

----DATABASE OF FUTURE INSTITUTE-----


1.Staff
2.Teacher
3.Officer
4.Typist
5.Casual
6.Regular
7.Education
8.Exit
Select any field=7

1.SET CODE
2.SET NAME
3.GET CODE
4.GET NAME
5.SET SUBJECT
6.SET QUALIICATION
7.GET SUBJECT
8.GET QUALIFICATION
9.SET GRADE
10.GET GRADE
11.SET ACADEMIC QUALIFICATION
12.SET PROFESSIONAL QUALIFICATION
13GET ACADEMIC QUALIFICATION
14.GET PROFESSIONAL QUALIFICATION.15.EXIT1
Enter code=23

1.SET CODE
2.SET NAME
3.GET CODE
4.GET NAME
5.SET SUBJECT
6.SET QUALIICATION
7.GET SUBJECT
8.GET QUALIFICATION
9.SET GRADE
10.GET GRADE
11.SET ACADEMIC QUALIFICATION
12.SET PROFESSIONAL QUALIFICATION
13GET ACADEMIC QUALIFICATION
14.GET PROFESSIONAL QUALIFICATION.15.EXIT3
23

 C++ type casting and RTTI


Implicit conversion
Implicit conversions do not require any operator. They are automatically performed when a value is copied to a
compatible type. For example:

short a=2000;
int b;
b=a;

Explicit conversion
C++ is a strong-typed language. Many conversions, specially those that imply a different interpretation of the value,
require an explicit conversion.

short a=2000;
int b;
b = (int) a;
b = int (a);

dynamic_cast

dynamic_cast can be used only with pointers and references to objects. Its purpose is to ensure that the result of the
type conversion is a valid complete object of the requested class.

Therefore, dynamic_cast is always successful when we cast a class to one of its base classes.

static_cast
static_cast can perform conversions between pointers to related classes, not only from the derived class to its base,
but also from a base class to its derived. This ensures that at least the classes are compatible if the proper object is
converted, but no safety check is performed during runtime to check if the object being converted is in fact a full
object of the destination type. Therefore, it is up to the programmer to ensure that the conversion is safe. On the
other side, the overhead of the type-safety checks of dynamic cast is avoided.

reinterpret_cast
reinterpret_cast converts any pointer type to any other pointer type, even of unrelated classes. The operation
result is a simple binary copy of the value from one pointer to the other. All pointer conversions are allowed: neither
the content pointed nor the pointer type itself is checked.

It can also cast pointers to or from integer types. The format in which this integer value represents a pointer is
platform-specific. The only guarantee is that a pointer cast to an integer type large enough to fully contain it, is
granted to be able to be cast back to a valid pointer.

The conversions that can be performed by reinterpret_cast but not by static_cast are low-level operations,
whose interpretation results in code which is generally system-specific, and thus non-portable.

const_cast
This type of casting manipulates the constness of an object, either to be set or to be removed. For example, in order
to pass a const argument to a function that expects a non-constant parameter

RTTI (Run-time type Information) in C++


In C++, RTTI (Run-time type information) is a mechanism that exposes information about an object’s data type at runtime
and is available only for the classes which have at least one virtual function. It allows the type of an object to be determined
during program execution

For example, dynamic_cast uses RTTI and following program fails with error “cannot dynamic_cast `b’ (of type `class B*’)
to type `class D*’ (source type is not polymorphic) ” because there is no virtual function in the base class B.

// CPP program to illustrate

#include<iostream>
using namespace std;
class B { };
class D: public B {};

int main()
{
B *b = new D;
D *d = dynamic_cast<D*>(b);
if(d != NULL)
cout<<"works";
else
cout<<"cannot cast B* to D*";
getchar();
return 0;
}

Adding a virtual function to the base class B makes it working.

// C++ program to illustrate

#include<iostream>
using namespace std;
class B { virtual void fun() {} };
class D: public B { };

int main()
{
B *b = new D;
D *d = dynamic_cast<D*>(b);
if(d != NULL)
cout << "works";
else
cout << "cannot cast B* to D*";
getchar();
}

Вам также может понравиться