Дахин
тодорхойлогдсон оператор гэдэг энгийн операторын объектын илэрхийлэлд
ашиглахаар тодорхойлохыг хэлнэ.
(+ - * / = <> += -= *= /= << >> == != <= >= ++ -- & ! && // [] () new Delete ) дахин тодорхойлох боломжтой.
Бичигдэх хэлбэр:
<Төрлийн нэр> оператор<тэмдэглэгээ>(параметр)
жишээ: Float
operator+(teacher t1)
Оролт
гаралтын оператор
OOstream
& operator<<(ostream
& Student s1)
OIstream
& operator<<(isrteam
& Student s1)
жишээ:1
class
Student{
string
name;
int
age;
string
aimag;
public:
Student(string n, int
a, string m)
{
name=n; age=a; aimag=m; }
Student operator=(Student S1)
{
name=S1.name;
age=S1.age;
aimag=S1.aimag;
return *this;
}
friend ostream
& operator<<(ostream
& out ,Student S1)
{ out<<"\nner="<<S1.name;
out<<"\nage="<<S1.age;
out<<"\naimag="<<S1.aimag;
return out;
}
};
main()
{
string n,m; int
a,r,i;
cout<<"too
oruulnauu=";
cin>>r;
for(i=0;i<=r;i++)
cout<<"ner=";
cin>>n;
cout<<"nage=";
cin>>a;
cout<<"naimag=";
cin>>m;
Student S1(n, a, m);
Student S2("",0,"");
S2=S1;
cout<<"S1"<<S1;
cout<<"S2"<<S2;
}
жишээ:2
OClass
Teacher{
Public:
Float
salary;
Teacher
operator+(teacher t1)
{Teacher
t2;
T2.salary=salary+t1.salary;
Return
t2;}
};
Main(){
Teacher
t1,t2;
Cout<<t2;
Cout<<t1;
No comments:
Post a Comment