First to answer frunys questions:
1. Write a program that print your name on the screen.
#include <iostream>using namespace std;int main(){ cout << "Name: alex" << endl; return 0;}
2. Track the errors on this code then fix the errors and guess what is this code do without using the compiler. There are 13 errors in this code
#include <iostream>using namespace std;int main(){ cout << "There are many mistakes in this code." << endl; cout << "Can you help me fix all the bugs in this code" << endl; cout << "Thank you!" << endl; return 0;}
3. Write a program that display something like this:
#include <iostream>using namespace std;int main(){ cout << "*******\n *****\n*******\n *****" << endl; return 0;}
4. Write a program that ask the age of the user and display it on the screen.
#include <iostream>using namespace std;int main(){ int age; cout << "How old are you? : "; cin >> age; cout << "You are " << age << " years old" << endl; return 0;}
5. Write a program that ask user to insert 2 numbers and then display the summary, subtraction and multiplication between those two numbers.
#include <iostream>using namespace std;int main(){ int x, y; cout << "Please enter your first number: "; cin >> x; cout << "Please enter your second number: "; cin >> y; cout << "The summary is: " << x+y << endl; cout << "The subtraction is: " << x-y << endl; cout << "The multiplication is: " << x*y << endl; return 0;}
Could have added functions for the arithmetic but wrote these answers quick :P
Answers to strings questions:
1: Both functions are missing a return statement.
2: Main missing a return statement.
However both the above should still compile.
3. You cant overload the main function.
4. Nothing wrong here, functions can be overloaded so long as their parameters are different.
5. Needs to be a function prototype if the function is declared after the main function.
6. Prototypes parameters do not match the function signature.
7. Nothing wrong here.
8. Prototype is miss spelled (I.e should be lower case f).
9. Like above, prototype does not match with function signature.
10. 9 Will produce a compiler error because the function declared by the prototype cannot be found (out of scope). While 8 will not be able to link with the function declared by the prototype.
(I dont currently have the sams c++ book, using an old C++ programming language by Stroustrup, book coming next week).
Answers to jwalsh's questions:
Chapter 1
What is an interpreter?
An interpreter is program that interprets code and executes it.
What is a compiler?
A compiler is a program that takes source code files and translates them into machine code (object code), which can be executed at a later time.
Why is compiled code faster than translated code?
Compiled code is faster than translated code because the compiler can optimise the code.
What does structured programming (procedural programming) consist of?
Procedural programming consists of writing procedures (functions, methods etc..) to give a more modular approach to programming.
What were the primary problems object oriented programming was designed to solve?
Object orientated programming was developed to give greater flexibility and make it easier to maintain complexed programs.
What are the three pillars of object oriented programming? Does C++ support them all?
Encapsulation, Inheritance and Polymotphism are the three pillars of OO programming. C++ supports them all.
What is encapsulation?
Encapsulation is data hiding, and providing certain access methods for objects.
What is inheritance?
Inheritance is an object can inherit functionality from whatever it has derived.
What is polymorphism?
This means an object can take many forms.
Should you learn C before C++, why or why not?
(Is this not just personnel opinion?) I think you should not learn C before C++ because it would be harder to get into the OO style of thinking required.
// No idea about these two, could be anything (don't have Sams book :P)
What is the FIRST question you ask when beginning your designs?
What is the SECOND question you ask when beginning your designs?
What are the two primary steps in creating an executable? What is the output of each step?
Compiling and Linking are the two main steps. The output of the compilation step is object code. After it has been linked, the output is an executable binary.
If your compiler requires <iostream.h> does it follow the ANSI standard? What IS the standard?
No it does not follow ANSI standard, the standard is just as #include <iostream>.
Chapter 2
When you issue a command to compile your code, what is run first? What does it do?
A lexical analyser is run first on the source code. This tokenises and outputs a series of symbols(tokens).
What symbol indicates a preprocessor directive?
# Indicates a preprocessor directive.
What does an "include" directive instruct the preprocessor to do?
An include instructs the preprocessor to include a separate file into the code.
What is the necessary function which all console C++ programs have?
A main() method.
Who calls that function?
The system calls the main function, this marks the entry point to the program.
Can you declare main as void? Why use int instead?
Because int is used to check to see if that function has executes correctly. (I.E 0 = no error)
Are "curly braces" optional when enclosing a function?
No.
What is the Standard Library?
The standard library is a set of functions, algorithms, classes etc.. that have been standardised and included in a library.
How do you instruct the compiler that you want to use part of the Standard Library?
To use part of the standard library you must include to relevant header files.
[Extra Credit] What's the difference between "\n" and endl?
(Guessing :-) \n creates a new line but keeps buffer open. endl flushes the buffer.
What are the 3 ways to use a class in a namespace? Use "cout" as an example.
std::cout << “test”;
Put, using std::cout; at top of source.
Put, using namespace std; at the top then use cout << “test”; etc as normal.
What are the two types of comments?
The two types of comments are // and /* */
[Extra Credit] What is self-documenting code? Does that remove the need for comments?
Self documenting code is code that reads as clearly/easily as possible. This does not however remove the needs for comments. Complicated algorithms/functions/hacks should always be commented.
Are the 3 components of a function declaration (function header)?
Yes return type, identifier, and parameters.
What do you call a function that is part of a class?
A member function.
[Extra Credit]What does a compiler do with "white space?"
Uh, the white space is handled by the lexical parser I guess.
[Edited by - alexjp on June 12, 2006 3:00:40 AM]