Simple C++ Features
This part of the assignment is intended to illustrate some of the simpler C++ features, and to show some of the differences between C and C++.
Step 1
Write a single program to determine the sizeof each of the built-in types for C/C++ (char, short, int, long, float, double, long double).
For example:
sizeof(long)
sizeof(char)
and also the sizes of literals (not variables) of those types:
| Type | Example of literal of that type |
|---|---|
char | 'a' |
short | <There is no constant of type short> |
int | 35 |
long | 79L |
float | 1.3F |
double | 93.4 |
long double | 37.9L |
for example:
sizeof(1.3F)
sizeof('a')
In this latter case (of sizes of literals) it should not be necessary to use any variables for that part of the code.
Note: I don’t want to see two different programs. I want to see a single program, identical in all respects for both C and C++. The only difference is how you indicate to the compiler that it’s a C or a C++ program.
Compile and run the program using a C compiler, and then with a C++ compiler on the same platform. (If your C++ compiler is capable of acting as a plain C compiler, then use its plain C mode).
Hint: Many different C++ compilers use a very simple device to determine whether a program is written in C or in C++: it looks at the filetype. If the program has a .c filetype, then it assumes it must be a C program; if the program has a .cpp filetype, then it assumes it must be C++.
Unix systems (most likely using the GNU C++ compiler), employ a similar convention. C programs usually have a .c filetype; C++ programs have a .C filetype (that is, an uppercase C — remember, everything on Unix is case-sensitive). However, in that environment, you may find that you may have to use different compilers for C and C++.
In either situation, you will likely have to create a single .c file, and then, once you have it working satisfactorily, copy that file to one with a .cpp filetype (MS VC++) or a .C filetype (Unix), maintaining the same filename. Then use the appropriate compiler to re-compile it in C++ and determine the differences.
What differences do you observe? Explain them.
Step 2
Here is a legal ANSI C program. Your task is to convert it to C++.
//
// friends.c
// Assignment 1b
//
// Created by Bryan Higgs on 10/28/24.
//
#include <stdio.h>
#include <stdlib.h>
enum sex { UNKNOWN, MALE, FEMALE };
struct friend
{
char *name;
int age;
enum sex sex;
};
void print_friends(struct friend this[])
{
static char *class[] = { "?", "M", "F" };
int i;
for (i = 0; this[i].name != 0; i++)
{
printf(" %s, Age: %3d, %s\n",
this[i].name,
this[i].age,
class[this[i].sex]);
}
}
int main(int argc, const char * argv[])
{
struct friend friends[] =
{
{ "Mabel Smith", 23, 2 },
{ "Fred Jones", 42, 1 },
{ 0, 0, 0 }
};
struct friend new[] =
{
{ "Clarence Brown", 25, 1 },
{ "Florence Tutwiler", 56, 2 },
{ 0, 0, 0 }
};
printf("I have the following friends:\n");
print_friends(friends);
printf("I just met the following new people:\n");
print_friends(new);
return 0;
}- First, compile and run it under plain C, check that it works, and observe what it outputs.
- Then copy the
friends.cfile tofriends.cpp(or whatever is appropriate for your C++ compiler) and try to compile it using C++. - Determine the cause of each problem you encounter, fix it, add a comment to the source code at that point, explaining what you did, and try again.
To maintain your sanity, I strongly suggest that you make only one change at a time, rather than a wholesale set of changes! - Repeat the above step until done.
- Document each change you make, in your source code, and explain why you had to make that change. Include in the description part of your submission a description of the actions you took (including any blind alleys you went down) to fix the problems you encountered.
- Eventually, you should be able to compile the program with no errors, warnings, or informationals, and be able to run it to produce the exact same results as the original program.
- Document each change you make, in comments in your source code, and explain why you had to make that change.
Include in the description part of your submission a description of the actions you took (including any blind alleys you went down) to fix the problems you encountered. - Eventually, you should be able to compile the program with no errors, warnings, or informationals, and be able to run it to produce the exact same results as the original program.
- Now convert the C-style I/O to C++ stream I/O, to produce exactly the same output. Do not do this until you have completed all the other steps. I want to see a separate version with just the I/O changes.
Step 3
The following C++ function uses parameters which are of type pointer to char, and uses pointer dereferencing to implement a circular shift of three characters:
/* shift.cpp */
#include "shift.h"
void shift(char *a, char *b, char *c)
{
char temp = *a;
*a = *b;
*b = *c;
*c = temp;
}
The header file shift.h contains the function prototype for the shift() function:
/* shift.h */
void shift(char *a, char *b, char *c);
Convert the shift function into one that uses reference parameters instead of pointers, and rewrite the shift.h file to match this new form of the function.
To test that your function works correctly, write a separately compiled (that is, the main function will be in a separate .cpp file from the above function) C++ test program. It will print the before and after values of a shift.
Step 4
Here are some C++ structure declarations for Person, for Fruit, and for Building:
/* person.h */
enum gender { UNKNOWN, MALE, FEMALE };
struct Person
{
char *name;
int age;
gender sex;
};
/* fruit.h */
enum fruit_type { BANANA, APPLE };
enum hue { BROWN, GREEN, YELLOW };
struct Fruit
{
fruit_type type;
hue color;
double weight;
};
/* building.h */
struct Building
{
char *name;
int floors;
int rooms;
};
Write three C++ (non-member) functions, called Print(), each of which accepts a parameter of type reference to Person, Fruit, or Building, respectively. Each Print() function will print out information about the contents of the structure passed to it.
Ensure that all of the functions can coexist in the same compilation, and in the same program.
The Person, Fruit, and Building types are totally independent of each other. Therefore, each Print() function will be placed in its own separate source file, and separately compiled. For each type, there will be a header file that contains the structure declaration, and all other publicly visible interfaces (including the function prototype for the Print() function).
NOTE: Use the
printf()function rather than C++ stream I/O. You won’t have covered stream I/O at this point, yet.
[Hint: Use function name overloading with regular functions, not member functions.]
The following main() function should work correctly, when linked against your code:
//
// main.cpp
// Assignment 1b
//
// Created by Bryan Higgs on 10/28/24.
//
#include "person.h"
#include "fruit.h"
#include "building.h"
int main(int argc, const char * argv[])
{
Person p = { "Archie Bunker", 55, MALE };
Fruit f = { BANANA, YELLOW, 3.5 };
Building b = { "World Trade Center", 110, 15000 };
Print(p);
Print(f);
Print(b);
return 0;
}It should output the following:
Person:
Name: Archie Bunker
Age: 55
Sex: Male
Fruit:
Type: Banana
Color: Yellow
Weight: 3.5
Building:
Name: World Trade Center
Floors: 110
Rooms: 15000