(1)
#include <iostream>
using namespace std;
class Student
{
public:
Student(int n,float s):num(n),score(s){}
void change(int n,float s)
{
num=n;
score=s;
}
void display()const
{
cout<<num<<" "<<score<<endl;
}
private:
int num;
float score;
};
int main()
{
//Student stud(101,78.5);
const Student stud(101,78.5);
stud.display();
//stud.change(101,80.5);
//stud.display();
return 0;
}<img src="https://img-blog.csdn.net/20150420215537863" alt="" />
<pre name="code" class="cpp">(2) #include <iostream>
using namespace std;
class Student
{
public:
Student(int n,float s):num(n),score(s){}
//void change(int n,float s)
void change(int n,float s)const
{
num=n;
score=s;
}
//void display()
void display()const
{
cout<<num<<" "<<score<<endl;
}
private:
//int num;
mutable int num;
//float score;
mutable float score;
};
int main()
{
//Student stud(101,78.5);
const Student stud(101,78.5);
stud.display();
stud.change(101,80.5);
stud.display();
return 0;
}
<pre name="code" class="html"> (3)#include <iostream>
using namespace std;
class Student
{
public:
Student(int n,float s):num(n),score(s){}
void change(int n,float s)
{
num=n;
score=s;
}
void display()
{
cout<<num<<" "<<score<<endl;
}
private:
int num;
float score;
};
int main()
{
Student stud(101,78.5);
Student *p=&stud;
p->display();
p->change(101,80.5);
p->display();
return 0;
}
<pre name="code" class="cpp"> (4)#include <iostream>
using namespace std;
class Student
{
public:
Student(int n,float s):num(n),score(s){}
//void change(int n,float s)
void change(int n,float s)const
{
num=n;
score=s;
}
//void display()
void display()const
{
cout<<num<<" "<<score<<endl;
}
private:
//int num;
mutable int num;
//float score;
mutable float score;
};
int main()
{
//Student stud(101,78.5);
const Student stud(101,78.5);
stud.display();
const Student *p = &stud;
//stud.change(101,80.5);
p->change(101,80.5);
//stud.display();
p->display();
return 0;
}
(5)#include <iostream>
using namespace std;
class Student
{
public:
Student(int n,float s):num(n),score(s){}
//void change(int n,float s)
void change(int n,float s)const
{
num=n;
score=s;
}
//void display()
void display()const
{
cout<<num<<" "<<score<<endl;
}
private:
//int num;
mutable int num;
//float score;
mutable float score;
};
int main()
{
//Student stud(101,78.5);
const Student stud(101,78.5);
stud.display();
const Student *p = &stud;
//stud.change(101,80.5);
p->change(101,80.5);
//stud.display();
p->display();
return 0;
}