Exam2_Review_Key.5 - 28. What problem do namespaces solve?...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
CPSC 121 Exam 2 Review Key 23. Explain in detail what cin.unget( ) (or putback ) does, since it can’t literally put a character back into the keyboard by popping out the last key pressed. The last character read from cin is placed back into the input buffer. 24. Circle the tokens in the following: 45 + 11.5 / 7 25. What is the most significant difference between a struct and a class , as far as the C++ compiler is concerned? In a struct, all members are made public by default, while a class makes them private. A class can also have public and private members, and a struct can only have public. 26. What does the phrase “to open a file” mean in C++? Explain in English. This means that the input pointer is placed at the beginning of the blocks of memory where that file’s data is stored. 27. Why is it usually better to not put definitions in a header file? Function names should be placed in the header file and the definitions placed in a .cpp file. By separating them, it makes the class structure easier to read.
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 28. What problem do namespaces solve? A namespace groups a set of classes, functions, variables, etc. . into one group, which can be referenced using a special name. This allows you to have multiple items with the same name and reference them all by prefixing it with its namespace. This is useful for libraries when you might use the same function/class names as another library. i.e. the namespace std for std::cout, std::cin, etc. . 29. Write a well-designed C++ class named Dog, which has fields for name, weight, and other appropriate items to create a Dog object. Get and set its fields, and output it with <<. class Dog { public: int weight() { return w; } string name() { return n; } Dog(int weight, string name) : w(weight), n(name) { } private: int w; string n; }; int main() { int weight; string name; cout << "Enter your dog's name and weight: "; cin >> name >> weight; Dog dog(weight, name); cout << "\nName: " << dog.name() << "\nWeight: " << dog.weight() << endl; }...
View Full Document

Ask a homework question - tutors are online