a
and
a + n
mean?
What (if anything) goes wrong in the following C++ code?
const int a[] {
0,
10,
20,
30,
40
};
const size_t n {size(a)}; //number of elements in the array
for (const int *p {a}; p < a + n; ++p) {
if (p[0] == p[1]) {
cout << "Found 2 consecutive copies of the same value.\n";
}
}
player.
Then it creates one structure named
player1
of this new type.
Then it passes the address of this structure
down to the two functions
moveDown
and
moveRight.
Do the underlined
++p’s
increment
p?
If not,
what prevents them from doing so?
Rewrite the program with the following three changes.
player
from a type of structure to a class of objects.
Since the class will have two
int
data members,
it should have a constructor with two
int
arguments.
The class will not need a destructor.
player1
from a structure to an object.
moveDown
and
moveRight
to member functions of the class.
When the program calls these member functions,
they should increment the
row
and
col
data members of the object.
#include <cstdlib> //for the macro EXIT_SUCCESS;
struct player {
int row;
int col;
};
void moveDown(player *p); //function declarations
void moveRight(player *p);
int main()
{
player player1 {0, 0};
moveDown(&player1);
moveRight(&player1);
return EXIT_SUCCESS;
}
void moveDown(player *p) //function definitions
{
++p->row;
}
void moveRight(player *p)
{
++p->col;
}
const int a[] {
0,
10,
20,
30,
40
};
const size_t n {size(a)}; //number of elements in the array
//Output all the elements in the array, but do not change them.
for (int *const p {a}; p < a + n; ++p) {
cout << *p << "\n";
}
minimum
and
maximum,
over and over in the member functions of this class,
please create them in a better way.
The two variables should be accessible only to the member functions
and friend functions of the class.
In other words,
the names of the two variables should be mentionable only in the member functions
and friends of the class.
class shoe {
private:
int shoeSize; //in the range 6 to 16 inclusive
public:
shoe(int init_shoeSize);
void changeSize(int newShoeSize);
};
shoe::shoe(int init_shoeSize)
: shoeSize {init_shoeSize} //Initialize the data member.
{
const int minimum { 6};
const int maximum {16};
if (shoeSize < minimum || shoeSize > maximum) {
cerr << "Can't create shoe with size " << init_shoeSize << "\n";
exit(EXIT_FAILURE);
}
}
void shoe::changeSize(int newShoeSize)
{
const int minimum { 6};
const int maximum {16};
if (newShoeSize < minimum || newShoeSize > maximum) {
cerr << "Can't change shoe to size " << newShoeSize << "\n";
return;
}
shoeSize = newShoeSize; //Update the data member.
}
equals
that will take two objects of the same class,
and return
true
or
false
to tell me if the two objects
have the same value.
In order to do its work,
this function will need to use the
private members of the class.
Therefore the function will have to be either a member function or a
friend function of the class.
Should
equals
be a member function or a friend function,
and why?
Instead of
equals,
what would be a better name for this function in C++?
(“Better” means the name that everyone would expect the function to have.)
#ifndef,
#define,
and
#endif,
directives at the start and end of a
.h
file?
#include <iostream>
#include <cstdlib>
using namespace std;
void f(int a); //function declaration
int main()
{
int i {10};
f(i);
cout << i << "\n";
return EXIT_SUCCESS;
}
void f(int a) //function definition
{
++a;
}
const int i {1};
cout << (i << 4) << "\n";
1101 1110 1010 1101 1011 1110 1110 1111
int a[] {
0,
10,
20
};
int *p1 {a};
++p1; //Statement 1
++*p1; //Statement 2
int *const p2 {a};
++p2; //Statement 3
++*p2; //Statement 4
const int *p3 {a};
++p3; //Statement 5
++*p3; //Statement 6
const int *const p4 {a};
++p4; //Statement 7
++*p4; //Statement 8
ints
into a
vector<int>
instead of into a plain old array of
ints?
int i {0x4 | 0x2};
cout << i << "\n";
cout << "How many ints do you want to store? ";
size_t n {0};
cin >> n;
const int *p {new int[n]}; //Get a block of memory from the operating system.
//Don't make any other pointers.
//Some time later,
delete[] p; //Give the block back to the operating system.
operator<<
function to the following class,
so that the output of the program will be
(3, 4)
#include <iostream>
#include <cstdlib>
using namespace std;
class point {
private:
double x;
double y;
public:
point(double init_x, double init_y): x {init_x}, y {init_y} {}
};
int main()
{
const point A {3, 4};
cout << A << "\n";
return EXIT_SUCCESS;
}
A lot of weird stuff went down in the old Soviet Union. In Red Plenty, a quirky book by Francis Spufford about the Soviet economy, I saw a reference to “glorious eccentricities, like Brusentsov’s trinary processor at the University of Moscow, the only one in the world to explore three-state electronics”. What advantages would a base 3 computer have over a plain old base 10 computer? Any disadvantages?
.C
file, instead of breaking it up realistically into several
.h
and
.C
files.
Show me the lines of output that the program produces,
in the correct order.
#include <iostream>
#include <cstdlib>
using namespace std;
class littleObject {
private:
int i;
public:
littleObject(int init_i): i {init_i} {cout << "littleObject born\n";}
~littleObject() {cout << "littleObject dies\n";}
};
class mediumObject {
private:
littleObject lo;
public:
mediumObject(int init_lo): lo {init_lo} {cout << "mediumObject born\n";}
~mediumObject() {cout << "mediumObject dies\n";}
};
class bigObject {
private:
mediumObject mo;
public:
bigObject(int init_mo): mo {init_mo} {cout << "bigObject born\n";}
~bigObject() {cout << "bigObject dies\n";}
};
int main()
{
bigObject bo {10};
return EXIT_SUCCESS;
}