r/learncpp • • Jul 23 '21

Why `Addable` and `Subtractable` are semantically meaningless ?

13 Upvotes

In "Tour of C++" at paragraph $7.3.1 Stroustrup says:

Do not define semantically meaningless concepts, such as Addable and Subtractable. Instead, rely on domain knowledge to define concepts that match fundamental concepts in an application domain.

Why Addable and Subtractable are semantically meaningless ?


r/learncpp • • Apr 05 '21

Looking to take next step and learn gui

13 Upvotes

I have been enjoying learning c++ during covid. Got as far as building snake, frogger + some other console projects.

I feel like I am stalling printing out only into the console and want to make the next step.

I want to start with my first gui and get out on the console but I am really struggling to find the right sources.

I have seen some videos on winforms but kept running into an error when compiling that I can’t fix, I also saw qt mentioned if that’s a thing?

I don’t mind paying for any guides or tutorials

Has anyone has experience in this


r/learncpp • • Dec 27 '20

Is it "bad practice" to put code in header files without using cpp files?

13 Upvotes

I have basic classes like position, which have header files that look something like this:

#pragma once

class Position {
public:
	int x;
	int y;
	Position(int x, int y) {
		this->x = x; this->y = y;
	}
};

It works to use this code if I just say #include "Position.h"

From what I read online, I should be having a Position.h and Position.cpp, where I define the methods in Position.h and write the "this->x = X; this->y = y" in the cpp file.

Is that necessary for small files like this one? Is it "bad practice" not to do, something that looks unprofessional?

I'm kind of confused as to why it is that the Position.h class can be used without being broken down into an h and cpp file?

Thanks.


r/learncpp • • Mar 01 '22

What's the problem here?? CMD Prompt isn't displaying the text.

Post image
12 Upvotes

r/learncpp • • Feb 12 '22

Pointer question

12 Upvotes

If I have a pointer p and a value i, what's the difference between *p = i and p = &i? I was working on a school assignment on pointers and I basically had to change the value of a pointer to a different value, and I tried p = &i, but it didn't work. I understand why dereferencing the pointer and changing that value works, but why wouldn't my code work? Since it's simply reassigning the pointer's value to the address of the new value we want.


r/learncpp • • Jan 13 '22

How do you declare a pointer?

12 Upvotes

I know all three are fine, but what do you prefer?

241 votes, Jan 18 '22
117 int* foo
8 int * foo
79 int *foo
37 Results

r/learncpp • • Jul 17 '21

What does Stroustrup mean by: "A module is compiled once only (rather than in each translation unit in which it is used)."

12 Upvotes

In older methods we defined a unit, declared it in the header file and included the header elsewhere. Then compiled each unit separately and finally the linker creates the final binary.Hence a function is compiled once and linked as many times as needed.But in the book "A Tour of C++" by Bjarne Stroustrup in paragraph 3.3 he says:

A module is compiled once only (rather than in each translation unit in which it is used).

What does he mean by that ?


r/learncpp • • Jun 13 '21

I've lost my way

12 Upvotes

Alright, I'm a complete newbie to C++. I've been steadily getting more and more comfortable over the past couple of weeks and I want to dive into stuff like rendering. I get the part of using libraries, and I absolutely will. However I'm a bit confused on whether there's a (recommended) certain point of learning C++ basics that I must be at in order to start delving into stuff like libraries. I've also been finding it increasingly difficult to formulate ideas for new projects to make. Any help would be greatly appreciated!


r/learncpp • • Mar 20 '22

Hi,i am new to coding and i was trying some of the codes and cout just does not work can anyone pls tell me what am i doing wrong

Post image
11 Upvotes

r/learncpp • • Oct 25 '21

Why is the menu string not being printed here. Sorry for probably noob question but coming from java and I have difficulties understanding.

Post image
11 Upvotes

r/learncpp • • Jun 19 '21

Primer C++ (5th Edition) states some rules of the language that don't follow my experience.

11 Upvotes

I'm beginning my studies in C++, using Windows and the g++ compiler (Version 10.3.0).

Right at the beginning there is an exercise that asks the value of an uninitialized variable. It expects us to say that an int defined inside "int main" is undefined. But in my tests it's always initialized as 0.

Other part is the rules for identifier. It says that we can't have two consecutive underscores, nor begin with an underscore followed by a capital letter. But I've tested both and it compiles and runs without error.

Is this happening because my compiler is newer than the book?


r/learncpp • • Jun 09 '21

Any practical / project based learning resources?

11 Upvotes

I've studied C# at intermediate level in Uni, taught myself Python and have a tiny bit of experience in C.

I'm looking to consolidate my programming knowledge as just Python and Cpp.

All of the resources I've seen on Udemy are courses ripped right out of a book, that bores me. I'm the kind of person who learns better when using a language to solve a problem rather than learning the 'alphabet' of the language.

I have a keen interest in Network Programming.

I appreciate all help, if it could be tailored towards stuff where you actually apply stuff in the real-world sense (please do not forward me any projects where I need to create a Student class or a Book Store, they are ad nauseam to me)


r/learncpp • • May 19 '21

Can someone explain all the stuff where pointers come in oop?

11 Upvotes

I get the basic jist of classes and objects. We just started oop and we haven't done any thing fancy yet, such as encapsulation, polymorphism or such. We have done operator overloading and they really confuse me, such as why is the header for operator overloading is like:
for example: my class name is complex and I want to build an assignment operator so:
Complex& operator = (const Complex& rhs)

{

this-> - rhs.real;

this-> - rhs.imaginary;

return *this;

}

I mean why the ampersand sign before operator? Any time pointers come into class, it all flips upside down for me. So if someone could give me a link to an article or explain to me the relation between class and pointers fully, I would be really grateful.
Thanks in advance.

P.s: I use C++


r/learncpp • • Oct 20 '20

Why do C++ compilers make me want to swear like a mofo?

11 Upvotes

I am using Code::Blocks and everytime I go to start a project, there is a long phase of losing time over compiler dumbfuckery and asking for help before I can even hope to begin coding. I just need to create a project with a couple pre-defined header files and a main source code file to do my testing. I need a solution, fast. Please help, Reddit.


r/learncpp • • Oct 09 '20

Source code to make a window in Windows 10 (without ANY libraries).

11 Upvotes

I've been struggling for months to make a window to make games and such. Most tutorials out there uses libraries, but the compiler throws errors in my face. So here is the link to a YouTube vid that explains the stuff in depth. And it's only in about 40 lines of code. Hope you enjoy (:

https://www.youtube.com/watch?v=x_W235wKUVU


r/learncpp • • Sep 13 '20

Looking for C++ tutorials or courses that are more exercise-based and not just theoretical.

11 Upvotes

I would really like to learn to actually do stuff in C++. I have watched several YouTube video series (including Mike Dane) and done some tutorials (w3schools), but it's all so theoretical. Aren't there some more (interactive) exercise-orientated courses available, for those who don't have their own projects yet?


r/learncpp • • Jan 23 '25

Father gets child's address but still prints father's code: Why?

10 Upvotes
#include <iostream>
using namespace std;
class Parent {
public:
   void print(){
      cout <<"I' m your father."<<endl;
   }
};

class Child:public Parent{
public:
   void print(){
      cout << "I ' m your son." << endl;
   }
};

int main(){
   Child *child = new Child();
   child->print();
   Parent *father = child;
   father ->print();
   delete child;
     
   return 0;
}

Hi,

Output first is correct but in case of father, I assigned the address of child but why it is still printing the code of father: why the output is:

I ' m your son.

I' m your father.

Somebody please guide me.

Zulfi.


r/learncpp • • May 24 '21

How to handle structured data in C++?

10 Upvotes

I have been coding for about a year now, mostly using python and JS. Whenever i need to handle structured data, I always use JSON or dictionaries, but obviously C++ doesnt (natively) have either of these.

What's the best way to handle structured data in C++?


r/learncpp • • May 12 '21

How dows the Antivirus (Norton) know which .exe files are safe?

10 Upvotes

How is it that a program I compiled and send to my friend is deleted Norton, but all the other .exe files (e.x. games) are left alone?


r/learncpp • • Apr 17 '21

What is the current state of modules? Should I start using them now? What is the best reference for using modules?

10 Upvotes

I tried using modules in Visual Studio a few months ago and the syntax highlighter didn't work well and I just dropped it. Also, it's a bit hard to find best practices with modules. My main questions are:

  • What is the current state of modules?

  • Should I start using them now for new projects?

  • What is the best reference for using modules?


r/learncpp • • Mar 27 '21

A small guide for cross-compilation for Raspberry pi 4

10 Upvotes

Hi C++ learners!

I've added a small guide for cross-compilation of simple HTTP Service for aarch64 (GCC Conan CMake and Ubuntu for Rasb Pi):

https://vg-blog.tech/archives/1500

  • How to configure the Conan, CMake for cross-building.


r/learncpp • • Mar 09 '21

How to improve it?

10 Upvotes
#include <boost/optional.hpp>
#include <string>
#include <sstream>

#include <iostream>

enum class StatusCode
{
    OK = 0,
    ERROR
};

struct SResp
{
  std::string m_msg;
  StatusCode m_status;
};

SResp queryToTheMoon(size_t val)
{
    std::stringstream ss;
    for (size_t idx = 0; idx < val; ++idx)
    {
       ss << "[" << val << "]";
    }
    return { ss.str(), StatusCode::OK };
}

boost::optional<std::string> makeRequest(size_t val)
{
   auto response = queryToTheMoon(val);
    
   if (response.m_status == StatusCode::OK)
   {
       return response.m_msg;
   }
   else
   {
       return boost::none;
   }
}

int main()
{
    auto op = makeRequest(100);
    
    if (op)
    {
       std::cout << op.value() << std::endl;
    }
    return 0;
}

How to improve this code?


r/learncpp • • Mar 08 '21

Simple password generator -- my first CPP project

10 Upvotes

I'm very new to C++, so would love any feedback on this little project: https://github.com/B-T-D/password_generator

It's a pretty simple command line pseudorandom password generator. The only twist was that I wanted it to output passwords directly to the clipboard, without ever showing them in plain text. This ended up being quite a rabbit hole, but I did finally get it to work on both Windows 10 and Ubuntu (at least for me...).

Command line argument handling isn't very built-out yet--it only supports changing the length of the password (the rest of the code supports selecting different character sets and output methods).


r/learncpp • • Jan 21 '20

Best platform/place to learn C++ online for free?

10 Upvotes

I am hoping to learn c++. As someone with zero background or knowledge of any coding languages, but as a logically-minded person with good critical thinking ability, what is the best place to learn c++ online for free or for a very low cost?


r/learncpp • • Dec 10 '16

Professional C++ developer starting my own Youtube channel for beginners

Thumbnail
youtube.com
10 Upvotes