This will be my first post in this subsection this month, and it will be about the implementation of Strings and Classes.
Strings:
Strings are a type of variable that can be very useful while programming any kind of application that requires user input, because it is better to use than a char variable and it will help if you are reading info from a text file. Here is some code for a basic implementation of a string variable and its use in an application:
This is a pretty simple program, but the uses for strings are almost endless in coding applications. Next up are classes and how to initialize them.
Classes:
Classes in C++ are very useful, especially if you want to eventually make a game or a program that keeps track of employees, people you need to kill, local fruit fuckers, etc.
that wraps up this post, I hope you got something out of it, have a good day
Strings:
Strings are a type of variable that can be very useful while programming any kind of application that requires user input, because it is better to use than a char variable and it will help if you are reading info from a text file. Here is some code for a basic implementation of a string variable and its use in an application:
- Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
//makes the string, which is named "username"
string username;
//prints a question to the console
cout << "Hello, what is your name? ";
//receives input to change the string username's value
cin >> username;
//prints text, then the string's value, then more text, then ends the line
cout << "Hello, " << username << "! Have a nice stay in hell." << endl;
// returns 0 if program ran successfully
return 0;
}
This is a pretty simple program, but the uses for strings are almost endless in coding applications. Next up are classes and how to initialize them.
Classes:
Classes in C++ are very useful, especially if you want to eventually make a game or a program that keeps track of employees, people you need to kill, local fruit fuckers, etc.
- Code:
#include <iostream>
#include <string>
using namespace std;
//makes the class "Person"
class Person {
//everything under public is accesible by the program
public:
//the start of the variables
Person(string name, int age) {
this->name = name;
this->age = age;
}
string getName() {
return name;
}
int getAge() {
return age;
}
//everything under private is accessible only through the get<name>() commands, if initialized in the public section
private:
string name;
int age;
};
int main() {
cout << "Creating a person..." << endl;
//makes an instance of the class "Person"
Person johnDoe("John Doe", 25);
cout << "Person's name: " << johnDoe.getName() << endl;
cout << "Person's age: " << johnDoe.getAge() << endl;
http://johnDoe.getName and getAge are able to access the private variables
return 0;
}
that wraps up this post, I hope you got something out of it, have a good day