|
ex3-8.cpp - Example 3-8 Enums and classes |
// File: ex3-8.cpp
void makeit6(int& I)
{
I = 6;
}
class ABC
{
int x;
public:
void funk();
void gunk() const; // const member function
void hunk(int&);
void junk(int&) const; // const member function
void lunk(const int&);
void munk(const int&) const; // const member function
};
void ABC::funk()
{
x = 6;
makeit6(x);
makeit6(6); // error: cannot convert const int to int&
}
void ABC::gunk() const
{
x = 6; // error: cannot change data member in CMF
makeit6(x); // error: cannot pass const int as int& parameter
}
void ABC::hunk(int &I)
{
x = 6;
makeit6(I);
makeit6(x);
}
void ABC::junk(int &I) const
{
x = I; // error: cannot change data member in CMF
makeit6(I);
makeit6(x); // error: cannot pass const int as int& parameter
}
void ABC::lunk(const int &I)
{
x = I;
makeit6(I); // error: cannot pass const int& as int& parameter
makeit6(x);
}
void ABC::munk(const int &I) const
{
x = I; // error: cannot change data member in CMF
makeit6(I); // error: cannot pass const int& as int& parameter
makeit6(x); // error: cannot pass const int as int& parameter
}
int main()
{
ABC object;
int i = 3;
object.funk();
object.gunk();
object.hunk(i);
object.junk(i);
object.lunk(i);
object.munk(i);
return 0;
}
CIS27: Programming in C++ Instructor: Joe Bentley