- Determine whether the following code has syntax errors or
not. If it is correct, predict its output. If it is
incorrect, point out the mistake(s).
// modulus
#include <iostream>
using std::cout;
using std::endl;
int mod(int a, int b)
{
do a -= b;
while (a >= b);
return a;
}
int main()
{
cout << mod(4, 7) << endl;
}
- Determine whether the following code has syntax errors or
not. If it is correct, predict its output. If it is
incorrect, point out the mistake(s).
// Caesar Cipher
#include <iostream>
using std::cout;
using std::endl;
void encrypt(char* a, int key)
{
while (*a)
{
if (*a >= 'A' && *a <='Z')
*a = (*a - 'A' + key) % 26 + 'A';
a++;
}
}
int main()
{
char msg[] = "HELLO NCNU";
encrypt(msg, 3);
cout << msg << "\n";
}
- Determine whether the following code has syntax errors or
not. If it is correct, predict its output. If it is
incorrect, point out the mistake(s).
// Structure
#include <iostream>
using std::cout;
struct Rectangle
{
int left;
int top;
int right;
int bottom;
};
int main()
{
Rectangle R1 = {0,0,10,20};
int area = (R1.right - R1.left) * (R1.bottom - R1.top);
cout << area << "\n";
}
- Determine whether the following code has syntax errors or
not. If it is correct, predict its output. If it is
incorrect, point out the mistake(s).
// Copy Constructor
#include <iostream>
using std::cout;
class CBox
{
public:
int volume;
char* color;
CBox(int v, char* c = "Violet"): volume(v)
{
color = new char[strlen(c) + 1];
strcpy(color, c);
}
CBox(const CBox& b)
{
color = new char[strlen(b.color) + 1];
strcpy(color, b.color);
volume = b.volume;
}
};
int main()
{
CBox cigar(10);
CBox chocolate(cigar);
cout << chocolate.color << "\n";
}
- Determine whether the following code has syntax errors or not.
If it is correct, predict its output.If it is incorrect, point out the
mistake(s).
// Copy
Constructor in a Derived Class
#include
<iostream>
using
std::cout;
class CBox
{
public:
int
volume;
CBox() {
volume = 10; }
};
class
CContainer : private CBox
{
char*
color;
public:
CContainer()
{
color = new char[strlen("Pink")+1];
strcpy(color, "Blue");
}
CContainer(CContainer& b)
{
color = new char[strlen(b.color)+1];
strcpy(color, b.color);
}
void ShowColor()
{
cout << color << "\n";
}
};
int main()
{
CContainer
cigar;
CContainer chocolate(cigar);
chocolate.ShowColor();
}
- Determine whether the following code has syntax errors or
not. If it is correct, predict its output. If it is
incorrect, point out the mistake(s).
// Operator Overloading
#include <iostream>
using std::cout;
class CList
{
public:
int count;
void Print() { cout << count << "\n"; }
CList(int i) : count(i) {}
CList& operator++() { count++; return *this; }
CList operator++(int) { count<<=1; return *this; }
};
int main()
{
CList L2(5);
++L2; L2.Print();
}
- Determine whether the following code has syntax errors or
not. If it is correct, predict its output. If it is
incorrect, point out the mistake(s).
// Friendship
#include <iostream>
using std::cout;
class CBox
{
private:
int volume;
public:
CBox() : volume(20) {};
friend int main();
};
int main()
{
CBox b1;
cout << b1.volume << "\n";
}
- Point out a printing error in the code on P.831.
- If we replace the OnDraw()
function in P.715 as below, what result will be shown on the screen
after you run the program?
void CSketcherView::OnDraw(CDC* pDC)
{
CSketcherDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
CList<CPoint, CPoint&> PointList;
PointList.AddHead(CPoint(50,30));
PointList.AddHead(CPoint(100,100));
PointList.AddHead(CPoint(150,30));
PointList.AddHead(CPoint(200,100));
POSITION aPosition = PointList.GetHeadPosition();
if (aPosition)
pDC->MoveTo(PointList.GetNext(aPosition));
PointList.GetNext(aPosition);
while (aPosition)
pDC->LineTo(PointList.GetNext(aPosition));
}
- If we replace the OnDraw()function
in P.715 as below, what result will be shown on the screen after you
run the program?
void
CSketcherView::OnDraw(CDC* pDC)
{
CSketcherDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
CList<CPoint, CPoint&>
PointList;
PointList.AddHead(CPoint(100,100));
PointList.AddHead(CPoint(150,30));
PointList.AddHead(CPoint(50,30));
PointList.AddTail(CPoint(200,100));
POSITION aPosition = PointList.GetHeadPosition();
if (aPosition)
pDC->MoveTo(PointList.GetNext(aPosition));
while (aPosition)
pDC->LineTo(PointList.GetNext(aPosition));
}
- In the movie "Mona Lisa Smiles", Katherine Ann Watson (played by
Julia Roberts) did not receive her doctor degree, so how does the
students call her?