전체 페이지뷰

레이블이 Cpp인 게시물을 표시합니다. 모든 게시물 표시
레이블이 Cpp인 게시물을 표시합니다. 모든 게시물 표시

2017년 3월 2일 목요일

SamyPesse/How-to-Make-a-Computer-Operating-System

https://samypesse.gitbooks.io/how-to-create-an-operating-system/

How to Make a Computer Operating System

Online book about how to write a computer operating system in C/C++ from scratch.
Caution: This repository is a remake of my old course. It was written several years ago as one of my first projects when I was in High School, I'm still refactoring some parts. The original course was in French and I'm not an English native. I'm going to continue and improve this course in my free-time.
Book: An online version is available at http://samypesse.gitbooks.io/how-to-create-an-operating-system/ (PDF, Mobi and ePub). It was generated using GitBook.
Source Code: All the system source code will be stored in the src directory. Each step will contain links to the different related files.
Contributions: This course is open to contributions, feel free to signal errors with issues or directly correct the errors with pull-requests.
Questions: Feel free to ask any questions by adding issues or commenting sections.
You can follow me on Twitter @SamyPesse or GitHub.

What kind of OS are we building?

The goal is to build a very simple UNIX-based operating system in C++, not just a "proof-of-concept". The OS should be able to boot, start a userland shell, and be extensible.
Screen

2013년 2월 6일 수요일

namespace , File I/O Statements

• You can define a class and place the definition of the class and the implementation of its member functions in separate files. You can then compile the class separately from any program that uses it, and you can use this same class in any number of different programs.
• A namespace is a collection of name definitions, such as class definitions and variable declarations.
• There are three ways to use a name from a namespace: by making all the names in the namespace available with a using directive, by making the single name available with a using declaration for the one name, or by qualifying the name with the name of the namespace and the scope resolution operator.
• You place a definition in a namespace by placing the definition in a namespace grouping for that namespace.
• The unnamed namespace can be used to make a name definition local to a compila- tion unit.

namespace S1
{
namespace S2
{
void sample( )
{
. . .
} .
.
.
} //S2
}//S1

Unnamed Namespace
You can use the unnamed namespace to make a definition local to a compilation unit. Each compilation unit has one unnamed namespace. All the identifiers defined in the unnamed namespace are local to the compilation unit. You place a definition in the unnamed namespace by placing the definition in a namespace grouping with no namespace name, as shown next:
namespace
{
Definition_1
Definition_2
. . .
Definition_Last
}
You can use any name in the unnamed namespace without qualifiers anyplace in the compilation unit.

Summary of File I/O Statements
In this example the input comes from a file with the name infile.txt, and the output goes to a file with the name outfile.txt.
■ Place the following include directives in your program file:
#include
#include
#include
For file I/O For cout
For exit
Add the following using directives (or something similar):
using std::ifstream;
using std::ofstream;
using std::cout;
using std::endl; //if endl is used.
■ Choose a stream name for the input stream and declare it to be a variable of type ifstream. Choose a stream name for the output file and declare it to be of type ofstream. For example,
ifstream inStream;
ofstream outStream;
■ Connect each stream to a file using the member function open with the external file name as an argument. Remember to use the member function fail to test that the call to open was successful:
inStream.open("infile.txt");
if (inStream.fail( ))
{
cout << "Input file opening failed.\n";
exit(1); }
outStream.open("outfile.txt");
if (outStream.fail( ))
{
cout << "Output file opening failed.\n";
exit(1); }
■ Use the stream inStream to get input from the file infile.txt just like you use cin to get input from the keyboard. For example,
inStream >> someVariable >> someOtherVariable;
■ Use the stream outStream to send output to the file outfile.txt just like you use
cout to send output to the screen. For example, outStream << "someVariable = "
<< someVariable << endl;
■ Close the streams using the function close:
inStream.close( );
outStream.close( );

• A stream of type ifstream can be connected to a file with a call to the member function open. Your program can then take input from that file.
• A stream of type ofstream can be connected to a file with a call to the member function open. Your program can then send output to that file.
• You should use the member function fail to check whether a call to open was successful.
• Stream member functions, such as width, setf, and precision, can be used to format output. These output functions work the same for the stream cout, which is connected to the screen, and for output streams connected to files.
• A function may have formal parameters of a stream type, but they must be call-by- reference parameters. They cannot be call-by-value parameters. The type ifstream can be used for an input-file stream, and the type ofstream can be used for an output-file stream. (See the next summary point for other type possibilities.)
• If you use istream (spelled without the "f") as the type for an input stream param- eter, then the argument corresponding to that formal parameter can be either the stream cin or an input-file stream of type ifstream (spelled with the "f"). If you use ostream (spelled without the "f") as the type for an output stream parameter, then the argument corresponding to that formal parameter can be either the stream cout, the stream cerr, or an output-file stream of type ofstream (spelled with the "f").
• The member function eof can be used to test when a program has reached the end of an input file.
• The same input and output functions you use to manipulate files can be used to manipulate strings through the stringstream class. The stringstream class pro- vides a simple way to create a string from variables of other data types or to read variables of other data types from a string.

Pointers and Dynamic Arrays


• A pointer is a memory address, so a pointer provides a way to indirectly name a variable by naming the address of the variable in the computer’s memory.
• Dynamic variables (also called dynamically allocated variables) are variables that are created (and destroyed) while a program is running.
• Memory for dynamic variables is in a special portion of the computer’s memory called the freestore manager. When a program is finished with a dynamic variable, the memory used by the dynamic variable can be returned to the freestore manager for reuse; this is done with a delete statement.
• A dynamically allocated array (also called simply a dynamic array) is an array whose size is determined when the program is running. A dynamic array is implemented as a dynamic variable of an array type.
• A destructor is a special kind of member function for a class. A destructor is called automatically when an object of the class passes out of scope. The main reason for destructors is to return memory to the freestore manager so the memory can be reused.
• A copy constructor is a constructor that has a single argument that is of the same type as the class. If you define a copy constructor, it will be called automatically whenever a function returns a value of the class type and whenever an argument is plugged in for a call-by-value parameter of the class type. Any class that uses pointers and the operator new should have a copy constructor.
• When overloading the assignment operator, it must be overloaded as a member operator. Be sure to check that your overloading works when the same variable is on both sides of the overloaded assignment operator.

class StringClass
{
public:
...
void someProcessing( );
...
StringClass& operator=(const StringClass& rtSide);
...
private:
char *a; //Dynamic array for characters in the string int capacity; //size of dynamic array a
int length; //Number of characters in a
};

StringClass& StringClass::operator=(const StringClass& rtSide)
{
if (this == &rtSide)
//if the right side is the same as the left side
{
return *this;
}
else
{
capacity = rtSide.capacity;
length = rtSide.length;
delete [] a;
a = new char[capacity];
for (int i = 0; i < length; i++)
a[i] = rtSide.a[i];
return *this;
}
}

PFArrayD& PFArrayD::operator =(const PFArrayD& rightSide)
{
if (capacity != rightSide.capacity)
{
delete [] a;
a = new double[rightSide.capacity];
}
capacity = rightSide.capacity;
used = rightSide.used;
for (int i = 0; i < used; i++)
a[i] = rightSide.a[i];
return *this;
}
Note that this also checks for the case of having the same object on both sides of the assignment operator.
PFArrayD::~PFArrayD( )
{
delete [] a;
}