Welcome to my blog; if this is your first time here, be sure to look at my other posts and tell me what you think of them. Now, it's time to look at what we did the past week.
Kirby's not cool; he's ugly.
-Little Kid at Target
At the start of the week, we continued studying C++; we made it to the second section of the 'C++ All-in-One for Dummies,' which covers Advanced C++ features. The portions that we went over this week are how to make classes, what an enum is, what constants are and why we use them, how to make children classes, how to comment correctly, how to use 'cin', and, preprocessor directives.
I've decided to paste in the Code rather than take a picture, so if you need it in the future, it's as simple as 'Ctrl-c'.
main.ccp (Classes Maybe Objects)
#include <iostream>
#include "Pen.h"
#include "Oven.h"
#include "Cheese.h"
using namespace std;
void AddOne(int* number);
int main()
{
cout << "Hello World" << endl;
//enum MyColor {blue, red, black, green}; // enum creates a new type, such as an int or float
//MyColor inkcolor = blue;
//MyColor shellcolor = black;
int CheeseBlocks = 0;
const int MyInt = 3; // a constant inacted by using the "const" varriable unchangable even when you pass a reference to it or another function
Fruit* Apple = new Fruit();
Cheese* asiago = new Cheese("Meh.");
CheeseBlocks++;
Cheese* limburger = new Cheese("Atrocious. Bleh");
CheeseBlocks++;
asiago ->eat();
Apple->what();
CheeseBlocks --;
limburger->rot();
CheeseBlocks--;
cout << endl;
cout << "Cheese count: " << CheeseBlocks << endl;
cout << "asiago: " << asiago->status << endl;
cout << "limburger: " << limburger->status << endl;
delete asiago;
delete limburger;
cout << endl;
//Raw Pointers
Pen* MyPen;
MyPen = new Pen;
MyPen->inkColor = grey;
cout << MyPen->inkColor << endl;
delete MyPen;
MyPen = 0;
//Smart Pointers
unique_ptr<Pen> MyPen2;
MyPen2.reset(new Pen());
MyPen2->inkColor = red;
cout << MyPen2->inkColor << endl;
MyPen2.reset();
//Making String and Pointer Copies (Aliases)
string OrigStr = "Hello";
const string& StringCopy(OrigStr); // The copy that cannot be modified
OrigStr = "Goodbye";
cout << OrigStr << endl;
cout << StringCopy << endl;
Oven Tom;
Pen FP; //This is your instance of the class Pen (Favorite Pen) is an Object
FP.inkColor = red;
FP.shellColor = red;
FP.capColor = grey;
FP.style = ballpoint;
FP.length = 6.0;
FP.brand = "Apperture Science";
FP.inkLevelPercent = 35;
Pen WP; //This is your instance of the class Pen (Worst Pen)
WP.inkColor = black;
WP.shellColor = grey;
WP.capColor = grey;
WP.style = felt_tip;
WP.length = 3.0;
WP.brand = "Apperture Science";
WP.inkLevelPercent = 75;
cout << "This is my favorite pen" << endl;
cout << "Color: " << FP.inkColor << endl;
cout << "Brand: " << FP.brand << endl;
cout << "Ink Level: " << FP.inkLevelPercent << "%" << endl;
FP.write_on_paper("Hello I'am the Mystic PEN OF POWER!");
cout << "Ink Level: " << FP.inkLevelPercent << "%" << endl;
cout << endl;
Tom.Bake(500);
return 0;
}
main.ccp (AdvancedC++Features)
#include <iostream>
#include <sstream> // the library that contains istringstream, ostringstream
#include <conio.h> // This library gives you better access to the console.
//istringstream - if you want to READ from a stringstream
//ostringstream - if you want to WRITE to a string stream
using namespace std;
// Note to self: put these in a class to utilize later.
int stringToNumber(string myString){
//Converts from string to number
istringstream converter(myString);
//Result of method
int result;
//function to perform and return the resuts
converter >> result;
return result;
}
string enterOnlyNumbers() { //The main use of this function is to use if you cannot for some reason use 'cin'
string numAsString = ""; //Holds the numeric string.
char ch = _getch(); //Obtains a single character
// Keep requesting characters until the user presses Enter.
while (ch != '\r') { // \r is the Enter Key
//Add characters only if they are numbers.
if (ch >= '0' && ch <= '9') {
cout << ch;
numAsString += ch;
}
//Get the next character from the user.
ch = _getch();
}
return numAsString;
}
string enterPassword() {
// Holds the password string.
string numAsString = "";
//Obtains a single character from the user.
char ch = _getch();
// Keep requesting characters until the user presses Enter.
while (ch != '\r') { //remeber that '\r' is the enter key.
cout << '*';
//Add the character to the password string.
numAsString += ch;
// Get the next character from the user.
ch = _getch();
}
return numAsString;
}
string numberToString(int number) {
//Converts from number to string.
ostringstream converter;
//Perform the conversion and return the results
converter << number;
return converter.str();
}
int main()
{
// Just a basic name-entering
string name;
cout << "What's your name? ";
cin >> name;
cout << "Hello " << name << endl;
//Now you are asked to enter a number,
//but the computer allow you to enter anything!
int x;
cout << endl;
cout << "Enter a number, any number! ";
cin >> x;
cout << "You chose " << x << endl;
//This time you can only enter a number
cout << endl;
cout << "This time enter a number!" << endl;
cout << "Enter a number, any number! ";
string entered = enterOnlyNumbers();
int num = stringToNumber(entered);
cout << endl << "You entered " << num << endl;
// Now enter a password!
cout << endl;
cout << "Enter your password! ";
string password = enterPassword();
cout << "Shhhh it's " << password << endl;
cout << endl;
//Contains Theoretical number of kids.
float numberOfKids;
//Contains an actual number of kids.
int actualKids;
/* You can theoretically have 2.5 kids, but in the real world, you can't.
* Convert the theoretical number of kids to a real number by truncating numberOfKids
and display the results.
*/
numberOfKids = 2.5;
actualKids = (int)numberOfKids;
cout << "Float to Interger" << "\tTruncated" << endl;
cout << numberOfKids << "\t\t\t" << actualKids << endl;
//This time we'll use 2.9 kids
numberOfKids = 2.9;
actualKids = (int)numberOfKids;
cout << numberOfKids << "\t\t\t" << actualKids << endl;
//The following process rounds the number rather than
//trunicating it. We do it using these two numbers.
numberOfKids = 2.5;
actualKids = (int)(numberOfKids + .5);
cout << "Float to Interger" << "\tRounded" << endl;
cout << numberOfKids << "\t\t\t" << actualKids << endl;
//This time we'll use 2.1 kids
numberOfKids = 2.1;
actualKids = (int)(numberOfKids + .5);
cout << numberOfKids << "\t\t\t" << actualKids << endl << endl;
//In this case we use stringToNumber() function to perform this conversion.
cout << "String to number" << endl;
int numx = stringToNumber("12345") * 50;
cout << numx << endl << endl; // TYhere is no typo here, this just makes the next cout push to the next line.
//In this case we use numberToString() function to perform this conversion.
cout << "Number to string" << endl;
string myString = numberToString(8675309);
cout << "Here's my number:" << "\t" << myString << endl;
return 0;
}
Near the end of the week, I decided to do a 'side-mission' of sorts. I wanted to make a Minecraft server since my friends and I have been getting really into the game recently. To accomplish this, I booted up my old Linux machine and connected it to my router. I started researching "How does one make a Minecraft server?". I eventually found a really helpful tutorial online that was really straightforward; I'll link it down below. However, this tutorial is only for Ubuntu, and I'm not quite sure about the rest of the OSes, but it seems pretty straightforward.
No comments:
Post a Comment