🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

C++ Workshop - Week 1 (Ch. 1 & 2) - Quizzes and Extra Credit

Started by
27 comments, last by Dbproguy 16 years, 2 months ago
OK now i'd like to post my poor solution ...

i checked the code in stdlib.h and i find : (the source code are sorted ... )

#ifndef _CRT_TERMINATE_DEFINED
#define _CRT_TERMINATE_DEFINED

#ifdef exit // this is not defined ...
#undef exit
_CRTIMP __declspec(noreturn) void __cdecl exit(__in int _Code);
#endif

_CRTIMP __declspec(noreturn) void __cdecl _exit (__in int _Code); // what should i do for strengthening the code ? … <br> _CRTIMP void __cdecl abort(void);<br>#endif<br><br>in the empty new project without and MACRO defined and you will find no 'exit' function defined … <br><br>in that case i did the poor decision in code : <br><br>// cstdlib_ques.cpp<br>#define exit<br><br>#include &lt;iostream&gt;<br>using namespace std ; <br><br>int main( void )<br>{<br> cout &lt;&lt; "ok now … but …" &lt;&lt; endl ; <br> return (0) ; <br>}<br><br>yes, ok now … but … <br><br>of course, as someone said, we should not change '_exit' to 'exit' …
À´×ÔÖйúµÄÇóѧÕß¡­¡­
Advertisement
another test ... for <cstdlib> under visual C++ .net

// cstdlib_ques.cpp

// my declaration ...
void exit(int) ;

#include <cstdio>
#include <cstdlib>

int main( void )
{
printf_s( "ok now ... but ... \n" ) ;
system( "pause" ) ;
return (0) ;
}
À´×ÔÖйúµÄÇóѧÕß¡­¡­
Quote: Original post by littlewater
another test ... for <cstdlib> under visual C++ .net

// cstdlib_ques.cpp

// my declaration ...
void exit(int) ;

#include <cstdio>
#include <cstdlib>

int main( void )
{
printf_s( "ok now ... but ... \n" ) ;
system( "pause" ) ;
return (0) ;
}


This is a bad example. We're not using cstdlib or cstdio at this point. As well, you're using C-Functions for printing to the screen (printf) and we're using C++ streams. Finally, as was discussed previously, dont use "pause" to trap a keystroke at the end of your executable. Its bad for the shell.

Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Greetings all!

Every Monday in addition to posting the chapter thread for that week I will post the answers for the quiz from two weeks ago. Today I will post the quiz answers for week 1. This coming Monday will be week 4, and I will post the answers for the week 2 quiz.

Chapter 1
Q. What is an interpreter?
A. An interpreter translates and executes a program as it reads it, turning the program instructions, or source code, directly into actions.

Q. What is a compiler?
A. A compiler translates source code into an intermediary form. This step is called compiling, and it produces an object file. The compiler then invokes a linker, which combines the object files into an executable program.

Q. Why is compiled code faster than translated code?
A. Compiled programs can run very fast because the time consuming task of interpreting the code into machine language is done once when the executable is built, not when the program is run.

Q. What does structured programming (procedural programming) consist of?
A. A series or procedures that act upon data, where the data is kept separate from the procedures. The programmer must keep track of which procedures call which other procedures and where the data is modified. Any procedure that was too large to manage was simply broken down into smaller, more manageable procedures.

Q. What were the primary problems object oriented programming was designed to solve?
A. The lack of pairing between data and procedures. The inability to re-use code due to incompatibility with the data types.

Q. What are the three pillars of object oriented programming? Does C++ support them all?
A. Encapsulation, Inheritance, and Polymorphism

Q. What is encapsulation?
A. Encapsulation is the property of being a self-contained unit and allows data hiding and abstraction. It frees developers from the need to understand how something works and allows them to focus on what it does.

Q. What is inheritance?
A. Inheritance is the ability to create a new data type that is an extension of an existing type. The new data type can have some or all of the properties and functionality of the existing type, while allowed developers to add or override unique functionality in the derived data type.

Q. What is polymorphism?
A. Polymorphism is the ability of an object to take on many multiple forms. Technically speaking, its the ability for a derived class to be treated as its parent type, while functioning as its derived type.

Q. Should you learn C before C++, why or why not?
A. No. Nearly all industry professionals agree that learning C before C++ is not only NOT helpful, but can also be disadvantageous to do so. The concepts of C, ie. procedural programming had many problems and encouraged what most today would consider bad programming practice. Remember that C++ was developed as a result of the deficiencies and limitations of the C language. Don’t think of C++ as being built 'on top' of C. Think of C++ as being built to be used INSTEAD of C.

Q. What is the FIRST question you ask when beginning your designs?
A. "What is the problem I'm trying to solve?"

Q. What is the SECOND question you ask when beginning your designs?
A. "Can this be accomplished without resorting to writing custom software?"

Q. What are the two primary steps in creating an executable? What is the output of each step?
A. Compiling and Linking. Compiling outputs object files, linking outputs either a library or an executable.

Q. If your compiler requires <iostream.h> does it follow the ANSI standard? What IS the standard?
A. No. The ANSI standard is to remove the '.h' from all Standard Library headers. Thus, <iostream> is the standard.


Chapter 2
Q. When you issue a command to compile your code, what is run first? What does it do?
A. The preprocessor is run first. The preprocessor reads through your code looking for lines that begin with the # symbol. When it finds those lines in performs some operation on the line depending on the command following the # symbol.

Q. What symbol indicates a preprocessor directive?
A. The # symbol

Q. What does an "include" directive instruct the preprocessor to do?
A. The include directive instructs the preprocessor to find the named file, read it, and dump its contents into the current file before compilation begins.

Q. What is the necessary function which all console C++ programs have?
A. All C++ console applications must have a function called 'main'.

Q. Who calls that function?
A. The Operating System calls the main function.

Q. Can you declare main as void? Why use int instead?
A. No. main should always return an integer so that the shell knows how the program exited, that is - with or without errors.

Q. Are "curly braces" optional when enclosing a function?
A. No. They are required.

Q. What is the Standard Library?
A. The Standard Library is the standard collection of classes and global objects that comes with every ANSI-compliant compiler.

Q. How do you instruct the compiler that you want to use part of the Standard Library?
A. By including the header file required by the object or type you want to use, and then specifying the use of the standard namespace 'std'.

Q.[Extra Credit] What's the difference between "\n" and endl?
A. \n inserts a 'new line' into the stream. endl inserts a new line as well as flushing the stream.

Q. What are the 3 ways to use a class in a namespace? Use "cout" as an example.
A. using namespace std; using std::cout; std::cout << ...

Q. What are the two types of comments?
A. Single line comments. ie. // and multi-line comments ie. /* */

Q.[Extra Credit] What is self-documenting code? Does that remove the need for comments?
A. Self-documenting code is code written such that function and variable names give a clear picture of what the code is doing. No, this does not remove the need for comments. Many people feel that comments are meant to explain WHY something is be doing done, while the code itself clearly explains WHAT is being done. Clearly written comments in addition to well formed and illustrative function and variable names can go a long way in working together to make your code more readable and easier to understand.

Q. What Are the 3 components of a function declaration (function header)?
A. Return type, identifier (function name), and argument list.

Q. What do you call a function that is part of a class?
A. A method of the class.

Q.[Extra Credit] What does a compiler do with "white space?"
A. It ignores all non-relevant white space, while preserving white space within literals.

Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Invisal's Extra Exercises


1. Write a program that print your name on the screen.
#include <iostream>#define name "Coury Ditch"int main(){   std::cout << name << std::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>int main(){   std::cout << "There are many mistakes in this code." << std::endl;   std::cout << "Can you help me fix all the bugs in this code" << std::endl;   std::cout << "Thank You!" << std::endl;    return 0;}

3. Write a program that display something like this:
*******
*****
*******
*****
#include <iostream>int star(){  std::cout << "*******\n" << " ***** \n";   return 0;}int main(){  star();  star();   return 0;}


4. Write a program that ask the age of the user and display it on the screen.
#include <iostream>int main(){  int age;  std::cout << "How old are you?: ";  std::cin >> age;  std::cout << "You are " << age << " years old." << std::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>int Add(int x, int y) {  std::cout << "The summary is: " << x+y << std::endl;   return 0; }int Sub(int x, int y) {  std::cout << "The subtraction is: " << x-y << std::endl;   return 0; }int Mul(int x, int y) {  std::cout << "The multiplication is: " << x*y << std::endl;   return 0; }int main() {  int a, b;  std::cout << "Please enter your first number: ";  std::cin >> a;  std::cout << "Please enter your second number: ";  std::cin >> b;  Add(a, b);  Sub(a, b);  Mul(a, b);   return 0; }



JWalsh's Quizz

CHAPTER 1

1. What is an interpreter?
Interpreter translates code directly into actions.

2.What is a compiler?
Compiler translates code to intermediary form, constructs an object file.

3.Why is compiled code faster than translated code?
Its not interpreted, and ran on the spot. It stays interpreted and can be run anytime.

4.What does structured programming (procedural programming) consist of?
A function, or set of instructions executed line after line. Meant to keep track of what functions call other functions, and what data is changed.

5.What were the primary problems object oriented programming was designed to solve?
Managing enormous complexity, and coupling data with the tasks that manipulate that data.

6.What are the three pillars of object oriented programming? Does C++ support them all?
encapsulation, data hiding, inheritance, and polymorphism. Trick question? C++ fully supports object-oriented programming.

7.What is encapsulation?
Self contained unit of code.

8.What is inheritance?
New types, based off other types, or extensions of other types, can be declared.

9.What is polymorphism?
Same name taking many forms.

10.Should you learn C before C++, why or why not?
No. The language has stayed very similar, but some of the syntax has changed. Learning C before C++ might make learning C++ confusing.

11.What is the FIRST question you ask when beginning your designs?
What is the problem I am trying to solve.

12.What is the SECOND question you ask when beginning your designs?
Can this be accomplished without resorting to writing custom software?

13.What are the two primary steps in creating an executable? What is the output of each step? Compile the source to an object file, link the object and create the executable.

14.If your compiler requires <iostream.h> does it follow the ANSI standard? What IS the standard? No, <iostream> is the correct way, many older books will state syntax not compliant with the ANSI C++ standards.


CHAPTER 2

1.When you issue a command to compile your code, what is run first? What does it do?
The preprocessor is run, this runs through all of your code and searches for #, it will make sure all your #define, #include , etc. are introduced in your program before anything else.

2.What symbol indicates a preprocessor directive? #

3. What does an "include" directive instruct the preprocessor to do?
Include the exact contents of that certain file in that spot of your code.

4. What is the necessary function which all console C++ programs have?
main()

5. Who calls that function?
The compiler auto-calls it.

6.Can you declare main as void? Why use int instead?
Void is not ANSI compliant, you must return an integer to the function to see if it passes or errors out.

7.Are "curly braces" optional when enclosing a function?
No, everything within {} is your function.

8.What is the Standard Library?
A collection of classes and global objects that comes with every ANSI-compliant compiler.

9.How do you instruct the compiler that you want to use part of the Standard Library?
By either putting std:: in front of the class/object. Or declaring "using namespace standard"

10.[Extra Credit] What's the difference between "\n" and endl?
\n creates a new line, endl clears the buffer and starts a new line.

11.What are the 3 ways to use a class in a namespace? Use "cout" as an example.
std::cout, using namespace standard; cout; , using namespace std::cout.

12.What are the two types of comments?
/* */ and //

13.[Extra Credit] What is self-documenting code? Does that remove the need for comments?
Self-documenting code is an IDE option, in inserts a comments section before your code to better document it. Comments are still recomended for use throughout the code, explaining differant functions, etc.

14.What Are the 3 components of a function declaration (function header)?
Return type, identifier (function name), and argument list.

15.What do you call a function that is part of a class?
A Method of the Class.

16.[Extra Credit] What does a compiler do with "white space?"
Ignores it unless its used in '' ir "" literals.




Hey as am0K says below, i got the same error, exept with "using ::rand", i know that my code is correct, because i didn't change it, it was working fine, compiling fine, i changed the SSE2 compile setting in the "Release" (this is after 2 other progs i made that i thought should of worked but couldn't remember) and it compiled... and i changed some code... compiled, then i looked and saw that SSE2 compiling never turned on, so i was like hmm :S odd, so i turned it on, didn't compile, came up with the "error C2039: 'rand' : is not a member of 'operator``global namespace''' " so, i was like **** heh, turned it off, compiled, same error, changed back the code i had changed before, same error, it just wont go away, this is running off msvs 2003, on a box that was left off for about 2 months untell i got a KVM, some how, some way, MSVS seems to corrupt, the main reason in my post is to let you all know, this can acually happen.

i will try reinstalling, seeing amok didn't post back, i'll assume it worked, i will post back and tell you.

(i'm trying to help you help others)

EDIT::

simple reinstall didn't help, i will try a fresh install or a fresh install of MSVS2005 (8.0)

EDIT::

Delete and then install you're MSVS to correct the problem, a simple reinstall does not do it, i can now build correctly, i'm very happy that it's working again, :)

[Edited by - bear21 on November 17, 2006 7:52:49 AM]
Chapter 1.

.What is an interpreter?
A) An interpreter translates and executes a program as it reads it, turning the program instructions, or source
code, directly into actions. Because interpreters read the source code as it is written and execute the code on the
spot, interpreters can be easier for the programmer to work with. Today, most interpreted programs are referred to
as scripts, and the interpreter itself is often called a "script engine".

.What is a compiler?
A) A compiler translates source code into an intermediary form. This step is called compiling, and it produces
an object file. The compiler then invokes a linker, which combines the object file inot an executable program.
Compilers introduce the extra steps of compiling the source code (which is readable by humans) into object code
(which is readable by machines).

.Why is compiled code faster than translated code?
A) Compiled code is faster because the time-consuming task of translating the source code into machine language has
already been done once, at compile time.

.What does structured programming (procedural programming) consist of?
A) Structured or procedural programming consists of a set of specific instructions executed one after the other.

.What were the primary problems object oriented programming was designed to solve?
A) Primary problems where not being able to reuse functions for different types of data.

.What are the three pillars of object oriented programming? Does C++ support them all?
A) Encapsulation, Inheritance and reuse, polymorphism and those three pillars of OOB are supported by C++.

.What is encapsulation?
A) Encapsulation is hidding the data so that an object can be used without the user knowing or caring how it works
internally.

.What is inheritance?
A) Inheritance is a type that is an extension of an existing type.

.What is polymorphism?
A) Polymorphism refers to the same name taking many forms.

.Should you learn C before C++, why or why not?
A) No, you shouldn't, the reason is that when you would learn C first, you would have to unlearn the bad habits
fostered by by C.

.What is the FIRST question you ask when beginning your designs?
A) First question should be "What is the problem I'm trying to solve?".

. What is the SECOND question you ask when beginning your designs?
A) Second question should be "Can this be accomplished without resorting to writing custom software?".

.What are the two primary steps in creating an executable? What is the output of each step?
A) Creating an Object File with the Compiler with the output being a file with the extension .obj or .o is the
first step and creating an Executable File with the linker with the output being an executable program (.exe).

.If your compiler requires <iostream.h> does it follow the ANSI standard? What IS the standard?
A) No, if you have to use <iostream.h> it means that your compiler is pre-ANSI. The ANSI standard is an attempt to
ensure that C++ is portable--ensuring, for example, that ANSI-standard-compliant code (stands for American National
Standards Institute) you write for Microsoft's compiler will compile without errors using a compiler from any other
vendor.


Chapter 2.

.When you issue a command to compile your code, what is run first? What does it do?
A) The preprocessor is run first, it reads through your source code and searches, reads and places the found file
right where it found it.

.What symbol indicates a preprocessor directive?
A) The # pound symbol indicates this.

.What does an "include" directive instruct the preprocessor to do?
A) Find, read and place the found file right where it found the "include" directive.

.What is the necessary function all console C++ programs have?
A) The function main() is what all C++ console programs have.

.Who calls that function?
A) It is called automatically.

.Can you declare main as void? Why use int instead?
A) Some compilers let you declare main() to return void. This however is no longer legal C++, and you should not get
into bad habits. Have main() return int, and simply return 0 as the last line in main().

.Are "curly braces" optional when enclosing a function?
A) No, you have to use them with every function you declare, define.

.What is the Standard Library?
A) The Standard Library is the standard collection that comes with every ANSI-compliant compiler.

.How do you instruct the compiler that you want to use part of the Standard Library?
A) You instruct the compiler by using the namespace std.

.What's the difference between "\n" and endl?
A) The difference is that endl flushes the buffer to the screen and is adapted to the operating system,"\n" isn't.

.What are the 3 ways to use a class in a namespace? Use "cout" as an example.
A) Use the entire standard namespace is the first, specifying using std:: for cout is the second and the
third is using std:: every time you write cout.

.What are the two types of comments?
A) First is the C-style multiline forward slash aseterisk version (/* comment...*/) and the second is the C++
single-line double forward slashes (// comment).

.What is self-documenting code? Does that remove the need for comments?
A) In my view, self-documenting code is code that is written in a manner that you can determine it's function by the
use of expressive variable names. It does not remove the need for comments because comments should explain what the
code does, not what the variable names mean.

.What are the 3 components of a function declaration(function header)?
A) It consists of the return type, the function name, and the parameters to that function.

.What do you call a function that is part of a class?
A) A member function.

.What does a compiler do with "white space"?
A) It ignores all non-relevant white space, while preserving white space within literals.
Hey guys/gals, I understand the difference between the \n and endl in terms of the new line vs. new line and flushing the stream.

1. Ok, by stream you are referring to what? Are there more than just the output streams? example maybe...

2. In working on the age program, I noticed something strange with the cin command. It appears that after a cin, either a \n or a endl is performed. Which is it?

For example, with this, the age prompt is on the same line as the first cout. The cin executes, still no \n or endl, but, the next cout is printed on a new line.

#include <iostream>int main(){	using namespace std;	int age;	cout <<"Please enter your age: ";	cin >>age;	cout <<"You are "<<age<<"years old."<<endl;	return(0);}


Thanks, sorry I am keeping this alive still, I am just starting from the beginning.

Shawn
Invisal's Extra Excercises

1. Write a program that print your name on the screen.
[source lang=cpp]#include <iostream>int main(){	std::cout << "Marcus Johnson";	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
The fixed code:
[source lang=cpp]#include <iostream>int main(){   std::cout << "There are many mistakes in this code." << std::endl;   std::cout << "Can you help me fix all the bugs in this code" << std::endl << std::endl;   std::cout << "Thank You!" << std::endl;}


On the screen it will have:
There are many mistakes in this code.Can you help me fix all the bugs in this codeThank You!

3. Write a program that display something like this:

******* ************ *****

[source lang=cpp]#include <iostream>int main(){	std::cout << "*******\n *****\n*******\n *****";	return 0;}

4. Write a program that ask the age of the user and display it on the screen.
[source lang=cpp]#include <iostream>int main(){	short userAge;	std::cout << "Please enter your age: ";	std::cin >> userAge;	std::cout << std::endl << "You are " << userAge << " years old." << std::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.
[source lang=cpp]#include <iostream>int main(){	short a, b;	std::cout << "Please input two numbers: ";	std::cin >> a;	std::cin >> b;	std::cout << std::endl << "Added\t\t" << a+b << std::endl;	std::cout << "Subtracted\t" << a-b << std::endl;	std::cout << "Multiplied\t" << a*b << std::endl;	return 0;}


Chapter 1

What is an interpreter?Converts the human readable code into machine language, and is required alongside the code each time the program is run.
What is a compiler?Somewhat like an interpreter except you only need the compiler once, then after that the executable file which the compiler created can be used instead of using the interpreter every time the program is run.
Why is compiled code faster than translated code? Translated code has to be translated each time the program is run
What does structured programming (procedural programming) consist of? You can break everything down into smaller objects or procedures to make it easier to understand.
What were the primary problems object oriented programming was designed to solve? To manage enormous projects using structured programming.
What are the three pillars of object oriented programming? Does C++ support them all?Encapsulation, Inheritance/Reuse, and Polymorphism. C++ supports them all.
What is encapsulation? Adding already created objects
What is inheritance? Reusing an object you previously made
What is polymorphism? Several different lines of code could virtually do the exact same thing.
Should you learn C before C++, why or why not? C could introduce bad habits so C++ should be learned without previous knowledge of C.
What is the FIRST question you ask when beginning your designs? What do I want to accomplish with this.
What is the SECOND question you ask when beginning your designs? Can this be accomplished without using software already made
What are the two primary steps in creating an executable? What is the output of each step? Creating an object file with the compiler and creating the executable with the linker.
If your compiler requires <iostream.h> does it follow the ANSI standard? What IS the standard? No it does not follow the standard. The standard is just <iostream> with no ".h"

Chapter 2

When you issue a command to compile your code, what is run first? What does it do?
What symbol indicates a preprocessor directive? #
What does an "include" directive instruct the preprocessor to do? Place the file in the brackets right here
What is the necessary function which all console C++ programs have? main()
Who calls that function? It is called automatically at the start of the program
Can you declare main as void? Why use int instead? No, int and return 0 to indicate that the program terminated properly.
Are "curly braces" optional when enclosing a function? No, they are required.
What is the Standard Library?The library that comes with C+ and has cout, cin, endl, etc.
How do you instruct the compiler that you want to use part of the Standard Library? by "using namespace std;" or "using std::cout;" or just "std::cout <<..."
[Extra Credit] What's the difference between "\n" and endl? endl will ensure the data gets displayed properly.
What are the 3 ways to use a class in a namespace? Use "cout" as an example. using namespace std; using std::cout; std::cout << "Hello World!";
What are the two types of comments? Single-line or C++-style and Multiline or C-style. single line are // and multiline are /* and */
[Extra Credit] What is self-documenting code? Does that remove the need for comments? It will describe every line of code or what it does as the source code is run. Comments should state why so it doesn't remove the need for comments.
Are the 3 components of a function declaration (function header)? Type, (int) the name (funcName) and the parameters (int a, int b) in the example: "int funcName (int a, int b)"
What do you call a function that is part of a class? Method
[Extra Credit]What does a compiler do with "white space?"Ignores it really.

--Dbproguy - My Blog - Tips, Opinions and Reviews about C++, Video Games, and Life

This topic is closed to new replies.

Advertisement