Monday, February 27, 2023

C++ Returns as Immutable

 Welcome to my Blog!

Quote of the Week

I'd Rather Piss than Pink
-Matt

Hello, lovey people who come to read my blog! This is what I've been working on in the past week. I was mostly working on the gameshow. However, I did get a chance to slip in some C++ code. Huzzah we're finally back to learning C++! Let's get into it.

 Game Show:

So, to start I've designed a new logo for the show and generally cleaned up the UI a bit more. I'm taking a class on graphic design, I try to use principals of this to make my programs look, and I quote "less disgusting". Here's what the design looks like right now.

If you notice the top, it looks like I've got a professional logo done for it. I don't know how professional you could call it since I made it with an online text generator, but I think the colors mesh well.

 

C++ Code:

Now onto the more technical stuff! This week I started a new section of the "C++ all in one book for dummies." This section claims to focus on Functional Programming. Currently, I'm learning about the principles of what makes functional programming different. This includes understanding lambdas, working with "struct", constant data, the application state, and more. This is the first time I'm seeing new built-in header libraries being used. These are <fstream> and <algorithm>. I'm excited to learn more about this and use it in my future coding ventures.

Here's a snip it of code that I was working on.

 #include <iostream>

#include <fstream>
#include <algorithm>

 

using namespace std;

 

//Avoiding Use of State Directly
//Application state is a condition that occurs when the application performs tasks that modify global data 
//It doesn't have one when using functional programming, which is good and bad. It's bad as the application has no memeory

 

int lineCount1(string filename) {
int lineCount = 0;
char c = ' ';
ifstream thisFile(filename);
while (thisFile.get(c)) {
if (c == '\n') lineCount++;
}
thisFile.close();
return lineCount;
}
int lineCount2(string filename) {
ifstream thisFile(filename);
return std::count
(istreambuf_iterator<char>(thisFile), istreambuf_iterator<char>(), '\n');
}
int main() {
const string filename = "ToeBeans.txt";
cout << lineCount1(filename) << endl;
cout << lineCount2(filename) << endl;
return 0;
}


//Making Imutable or Constant Data 
/*
struct Immutable {
int val{ 7 };
};
int main() {
const int *test1 = new int(5);
*test1 = 10;
const int test2{ 6 };
test2 = 11;
const Immutable test3;
test3.val = 12;
//The code should give an error as they should be immutable with means they cannot be re-assigned.
cout << *test1 << test2 << test3.val << endl;
return 0;
};
*/

That will conclude this week's post. I hope you enjoyed reading it! The main goal of this site is to show myself how far I've come about a year from now, so I really appreciate you reading this. It's like you're on a coding journey with me! But being a nerd aside, I'll see you guys next week!

-PeacešŸ’£ 

No comments:

Post a Comment

DOCTOR! The Calculator is Terminal!! The C++ variety ;)

    Hello, and Welcome to my Blog!  If you're a returning visitor, ...Hello Again! Quote of the Week: ..but fear itself isn't worthy...