Forumite Members › General Topics › Tech › Makers & Builders › Programming/Code tips › C++ Programming
- This topic has 258 replies, 5 voices, and was last updated 2 years, 12 months ago by
Wheels-Of-Fire.
-
AuthorPosts
-
March 11, 2021 at 2:44 pm #67904
I am new to smart pointers and I am still struggling with the various types of constructor/destructor that a class can contain but I would have said that you could not use malloc with a smart pointer because the smart pointers default destructor relies on the objects type to do its work.
However I have been reading that there are ways and means around the problem, I am looking into it 😁
March 11, 2021 at 3:02 pm #67908If the C++ committee are driven by a desire to eliminate major sources of hacker attack then automatically releasing allocated memory has to have been high up on the list.
March 12, 2021 at 3:02 pm #67916I just wanted to give an example of a simple class using constructors, so here is one below:
class.h file
class rectangle
{
public://The default constructor. Required if we define any other constructors
rectangle() = default;//Constructor for rectangle created with 3 argumants
rectangle(int initial_width, int initial_hight, int initial_nothing) : width{ initial_width }, hight{ initial_hight }, nothing{ initial_nothing } {}int get_area()
{
return width * hight;
}int get_width() { return width; }
int get_hight() { return hight; }
int nothing = 10;
~rectangle() {}
private:
int width = 0;
int hight = 0;
};
main.cpp file
#include<iostream>
#include “class.h””
using namespace std;
int main()
{
//Create rectangle object with the default constructor
rectangle test{};int a = test.get_area();
int h = test.get_hight();
int w = test.get_width();//Prints 0 0 0 10
cout << a << ” ” << h << ” ” << w << ” ” << test.nothing << endl;// Create rectangle object using the constructor that takes 3 arguments {int width, int hight, int nothing)
rectangle test1{ 4, 5, 100 };// All functions are public so I can use the dot operator
int a1 = test1.get_area();
int h1 = test1.get_hight();
int w1 = test1.get_width();//”nothing” is public so this is fine too
//Prints 20 4 5 100
cout << a1 << ” ” << w1 << ” ” << h1 << ” ” << test1.nothing << endl;//”hight” is private so I can’t do this
//cout << test.hight << endl;}
See if you can pick the bones out of that 🙂
March 12, 2021 at 5:20 pm #67922Heres a hint.
A constructor is a function with the same name as its class. The constructor contains an argument list enclosed by brackets, like any function, followed by a colon and then followed by a list of parameters that are initialised by the arguments. The direct form of initialisation must be used so we enclose the arguments in braces instead of using “=”.
When we create a class object we follow it with a list of arguments and that calls the constructor with the same number of parameters.
Destructors start with a ~ and then the name of the class, they take no arguments but they do have a body so deleting objects created with “new” could be done there.
The destructor in the example is the default so its body is empty.
March 14, 2021 at 6:09 pm #67948I have been looking at providing my classes with overloaded operators but it turns out they are of limited use. I am especially interested in the “<<” output operator.
You cant just do “cout << MyClass;” because the “<<” operator wont have a clue what members of your class you want printed of how to format the output.
The solution is to provide a special operator overload function that starts with the keyword “operator” and follows it with a function name which must be of the form “operator” followed by the operator symbol, so in this case “operator<<” (no space). I wont bore you with the details but eventually you end up defining the data members you want printing (it can only handle data members, it can’t call functions) and how you want them printed.
The function must be external to the class definition so it must be declared a “friend” within the class so it can access private members. The reason this function must be external is also the reason it is of limited use.
Every operator overloading function names a particular instance of a class so it must be in the same scope as that instance. You can put your class declaration in a header file and include it in your main.cpp (as you should) but you will be creating named instances of that class in main() so the operator overloading function for that instance has to go in main() too.
Now if you have a class called “Student” and an instance of it called “StudentOne” then you really can write:
cout << StudentOne;
You may have spotted an obvious limitation here, if you create another instance of your class with a different name, then you will have to write another operator overload function naming that instance too !
March 15, 2021 at 12:02 pm #67965As is not unusual, I was actually wrong about having to declare a separate operator overloading function for each class instance. The class instance name declared in the function is confined to its scope and a new reference for it is created for every new class object you create. It was just unfortunate that the example in my book used the same name as the class object they created.
March 16, 2021 at 8:04 am #67970This article explains how C++ overloading works:
https://preshing.com/20210315/how-cpp-resolves-a-function-call/
I’m afraid, I look at that and just think of all the overhead that has to be resolved for each function call because of programmer laziness! I’ll grant that this all disappears when the program is compiled, but it points out that you should never overload in-line function calls unless you want to make the compiler explode its brain!
March 16, 2021 at 3:03 pm #67972Most of that is also explained (briefly) in the C++ primer book so, although I CAN now make overloading the “<<” operator work for my classes, I think I will just keep doing what I was doing, include a “print” function as a member of the class. I can then use “class object name”.print anywhere in the scope where I created the object (most often main()) and no overloading is required.
March 17, 2021 at 1:50 pm #67977Do you have Visual Studio installed anywhere Ed ?
March 17, 2021 at 7:31 pm #67989Not now, I tend to do all my programming either in Python or Delphi. Python as it has some great AI and OpenCV libraries, and Delphi for its RAD and GUI library.
I gave up on Visual Studio in the early noughties when both C++ and C started to get somewhat bloated, and M$ started to ram C# down our throats – I rejected that as it was not really compatible with developing CGI/Video or games programming (too ‘safe’).
March 18, 2021 at 3:30 pm #68009I ask because if you had had a copy of VS installed then I could have just posted links to solution.sln files instead of copying and pasting in their various .h and .cpp bits 😃
I think C# may be in trouble, MS would still LIKE us to use it because it limits what us naughty programmers can do but mostly people actually used it so they could access the Windows runtime components for building a GUI.
C++/WinRT allows C++ to access runtime components in the same way a managed language can.
March 20, 2021 at 10:11 am #68047I put up a flame bait post on Stack Overflow, asking why their members were so mean to obvious beginners, closing their questions and denying ME a chance to answer them. I said that maybe they should spend a couple of minutes analysing the question instead of 2 seconds matching it with a site rule that it broke.
The steam of venom that recieved was beyond even my expectations 😆
March 20, 2021 at 11:46 am #68049I said that maybe they should spend a couple of minutes analysing the question instead of 2 seconds matching it with a site rule that it broke.
+1 Stack Overflow REALLY pees me off with the way most forums treat beginners. OK there are a few posters who are obviously lazy and just seeking to get their homework done. However, even these often contain questions that could usefully be answered for others. I generally avoid Stack Overflow like the plague for that reason.
Many moons ago when I first started coding , I was told that the real art of coding was intelligent plagiarism. ‘Do not reinvent the wheel’ was the mantra at that time.
March 20, 2021 at 2:29 pm #68052There are indeed a fair few questions just asking for answers to home work and lots of them do not bother posting what they have tried so far.
Those questions do not deserve the time of day but if you really ARE a beginner then you may not know how to frame a question.
The self satisfied members of Stack Overflow could take that into account.
March 24, 2021 at 3:33 pm #68072The more I look into C++ class inheritance the less I can see me using it. If I were writing a library class that was going to be used by other people then the tight encapsulation that higherarchys can provide would be very useful. But i’m not 😁
Classes are fine and even friends of classes are fine but derived classes seem to be a lot of effort for not much gain in the sort of programs I am likely to write.
March 24, 2021 at 4:25 pm #68073Inheritance has some good but limited uses. Accounting programs and databases where there are a lot of similar (but different) structures to handle are prime examples. It is also useful for reusing code, but so is ‘copy & paste’!
If you want to see some real world examples of C++ in a gaming environment check out the Unreal 4 C++ tutorials.
March 26, 2021 at 1:34 pm #68095I won’t be writing any 3D games for some time yet but Visual Studio is the recommended IDE for Unreal Engine 4 and its producers have written VS intellisense plugin’s for it so you get on the fly error highlighting during the editing phase.
Microsoft also recommend UE4 as the default 3D engine for VS and they provide support material too, including installation and setup guides.
March 29, 2021 at 7:38 pm #68107I came across an annoying situation in Atari BASIC many many years ago and it still seems to be there in C++.
In BASIC I wanted to dimension a string with the name of an input string, but no, the name of a string must be a literal.
It would also have been nice to be able to have name$(1), name$(2), etc, but no again, the name MUST be a plain literal.
Now I come up against the same thing in C++. I define a class and then I want to create an unknown, at compile time, number of instances of it but I can’t because the name of the instance must again be a literal.
In Atari BASIC I gave up and resorted to a disk file but all this time later I had hoped that C++ could do better.
Unless it can ?
April 5, 2021 at 8:04 pm #68130As an alternate to StackOverflow I recommend CodeProject the posters and moderators are far more balanced than SO’s bigots. It is very VisualStudio biased (I think it had some M$ tie-in early on). I have been a member for a number of years. There is often something interesting on the site (assuming you are biased towards Windows and VS).
April 12, 2021 at 3:37 pm #68161Had a look Ed, quite good.
I just came across another “helpful” feature of C++11 that I can’t see a use for, decltype.
We have “auto” that let’s the compiler determine types for us and “decltype” seems to be a less flexible way of doing nearly the same thing. Here is an example:
string s = “Hello”;
decltype (s.size()) length = s.size();
In the above example “lengeth” gets the type size_t . The reason is that decltype evaluates the expression s.size() to determine its return type but it does not actually call it.
I get exactly the same result with:
auto length = s.size();
Why the need for another system determined type ?
-
AuthorPosts
- You must be logged in to reply to this topic.
