C/C++ Beginner's Guide and Post Basic Questions here

quicky008

Technomancer
How about those who use linux on a daily basis?I dont suppose they can utilize VS to code their C/C++ programs now,can they?
 

Nerevarine

Incarnate
Surprisingly they can.
1) you can use VS Code (which is a glorified text editor) with a few debugger plugins to code . This is a hobby IDE at best.
2) Use regular VS Community/Pro/Enterprise on a windows machine and attach debugger to a VM or another machine on network running linux. VS has full linux library compatibility, sadly the application itself is not on Linux
 

quicky008

Technomancer
Can someone please explain to me how this program works:

C++:
#include<iostream>

using namespace std;

class Vector
{
  public:
    int x;
    int y;
    Vector(int x, int y) : x(x), y(y) {}
    Vector operator+(const Vector &right)
    {
        Vector result(x + right.x, y + right.y);
        return result;
    }
};

std:: ostream &operator<<(std:: ostream &Str, Vector const &v)
{
    Str << "[" << v.x << "," << v.y << "]";
    return Str;
}

int main()
{
    Vector x(2, 3);
    Vector y(4, 5);
    cout << "Sum of vectors " << x << " and " << y << " is " << x + y << endl;
    return 0;
}

Its based on operator overloading-but my knowledge of C++ is a bit rusty as i haven't been using it for a long time.So any explanation would have been really helpful

Mod edit: Fixed code formatting using code blocks.
 
Last edited by a moderator:

Desmond

Destroy Erase Improve
Staff member
Admin
Can someone please explain to me how this program works:

#include<iostream>

using namespace std;

class Vector
{
public:
int x;
int y;
Vector(int x, int y) : x(x), y(y) {}
Vector operator+(const Vector &right)
{
Vector result(x + right.x, y + right.y);
return result;
}
};

std:: ostream &operator<<(std:: ostream &Str, Vector const &v)
{
Str << "[" << v.x << "," << v.y << "]";
return Str;
}

int main()
{
Vector x(2, 3);
Vector y(4, 5);
cout << "Sum of vectors " << x << " and " << y << " is " << x + y << endl;
return 0;
}

Its based on operator overloading-but my knowledge of C++ is a bit rusty as i haven't been using it for a long time.So any explanation would have been really helpful

I haven't coded in C++ in a long time, but from what I remember, here you are overloading the + operator and giving it the functionality to add two vectors. Note that the + operator historically is defined to add two numbers, floats or doubles, etc. it is not defined for custom types such as Vector here. Therefore, you have to define the functionality of + for user defined types in this case and that is what is going on.

Here, the operator + has been defined to add the x of the first operand vector to the x of the second operand vector and the y of the first operand vector to the y of the second operand vector. The result is a new vector object having the resultant values of x and y.
 

Nerevarine

Incarnate
Same thing happens with << operator. It is used here to pass a vector object to a stream, by converting that vector object into a string format.
 

quicky008

Technomancer
^thank you both for the inputs,but i can't understand what's going on in this part

C++:
Vector operator+(const Vector &right)
{
    Vector result(x + right.x, y + right.y);
    return result;
}

Whats the purpose of using the const Vector &right as the parameter? Is &right a pointer variable of type Vector?If so,why are they using a pointer variable and why is it being declared as a constant?

Are the variables x and y being initialized with 2 and 3 respectively,and then 4 and 5 are added to them later via (x + right.x, y + right.y)?

Also i am having a hard time understanding the significance of this :

C++:
std:: ostream &operator<<(std:: ostream &Str, Vector const &v)
{
    Str << "[" << v.x << "," << v.y << "]";
    return Str;
}
 
Last edited by a moderator:

Desmond

Destroy Erase Improve
Staff member
Admin
Whats the purpose of using the const Vector &right as the parameter?
It represents the RHS operand of that operator. There is no LHS because the current class is considered the LHS operand.
Are the variables x and y being initialized with 2 and 3 respectively,and then 4 and 5 are added to them later via (x + right.x, y + right.y)?
It's a bit confusing since you have named your vectors x and y. Suppose if you named them v1 and v2, then v1 + v2 will be equals to [(v1.x + v2.x),(v1.y + v2.y)]. Therefore, in this case, suppose if v3 = v1 + v2, then v3 will be (2+4, 3+5) = (6, 8)
Also i am having a hard time understanding the significance of this :
Here you are overloading the insertion operator. Here the operands are an output character stream and a Vector user defined type. Basically, this operator will format your Vector (right operand) in this format: [x,y] and insert it into the character stream variable in the left operand.
 

skeletor

Chosen of the Omnissiah
Can someone please explain to me how this program works:

~ coded snipped out ~

Its based on operator overloading-but my knowledge of C++ is a bit rusty as i haven't been using it for a long time.So any explanation would have been really helpful

Mod edit: Fixed code formatting using code blocks.

Purpose of std:: ostream &operator<<(std:: ostream &Str, Vector const &v):

What this does is, it modifies the behaviour of << operator in std:: ostream. So when you use std::cout << and you have an object of class Vector, it will use this new definition of the operator <<.

Why? To add support in std::cout so that it print the object of class Vector in the desired format. The desired format being what you see in the definition. Remove this definition and your code will fail to compile and report that there is "no match for operator<< in std:: ostream when Vector is an operand".

Purpose of Vector operator+(const Vector &right):

To add binary operator +. The way this works is when you do a + b where both a and b are objects of class Vector, a is considered the caller object. b is considered a parameter to the operator's definition, i.e. "right" in the code. Then what the operator is supposed to do is. Create and return a new Vector object that adds x and y members of the caller (i.e. a) and the operand (i.e. b).

For simplicity, I suggest that in the main function you replace x with a and y with b (or v1 and v2 like Desmond suggested).
 

quicky008

Technomancer
i was trying to write an int value to a file using the fputc command in the following way:

fputc(count,p) where count is the integer variable and p is the file pointer.

the program is supposed to write 5(the value of count),but after running the program,all i can find in the file is a garbage/unrecognizable value.

however if i attempt to write a char value to a file instead of an int value(eg '5'),its written correctly and the value appears in the file after running the program.

Can anyone explain why this happens?

also i am bit confused about the syntax of fputc-acc. to some sources it should be int fputc(int char, FILE *pointer) (Where char is the character value being written). If i am trying to write a char value,why not use char datatype directly,rather than an int value?Whats the reasoning/logic behind this?
 

aaruni

The Linux Guy
Characters are just variants of integers, encoded in ASCII.
Code:
char '5'
is the same value as
Code:
int 53
. The number 5 refers to the character ENQ ( Enquiry character - Wikipedia ).

In C++, you can write to files in text mode, or binary mode. When writing in text mode, all writing is done in characters.

Important thing to remember is that every character is actually a number behind the scenes.
 

Nerevarine

Incarnate
It could be writing the ascii value of 5 onto the file, which is

505005^EEnquiry (ENQ)


Can you try 65 and see if A (in caps) is being written ?

Edit : anyway aaruni beat me to it, i think thats whats happening
 

quicky008

Technomancer
^indeed,when i entered 65,it did write A to the file.

Why then is it writing some sort of unrecognizable character if i try to write an int variable containing the value 5?Is it writing the ASCII character that corresponds to the value of 5?

so what should one do if he wishes to write some numeric value to a file?Does one need to derive its ASCII value then write it to the file-wont that become quite cumbersome?
 
Last edited:

quicky008

Technomancer
Characters are just variants of integers, encoded in ASCII.
Code:
char '5'
is the same value as
Code:
int 53
. The number 5 refers to the character ENQ ( Enquiry character - Wikipedia ).

In C++, you can write to files in text mode, or binary mode. When writing in text mode, all writing is done in characters.

That was my understanding too,but why then is the first argument to fputc function an integer as opposed to a character value(as i have pointed out in my earlier post)?
 

Nerevarine

Incarnate
^indeed,when i entered 65,it did write A to the file.

Why then is it writing some sort of unrecognizable character if i try to write an int variable containing the value 5?Is it writing the ASCII character that corresponds to the value of 5?

That is exactly what it is doing, the ascii equivalent of 5 is ENQ which doesnt have a corresponding human readable letter.

so what should one do if he wishes to write some numeric value to a file?Does one need to derive its ASCII value then write it to the file-wont that become quite cumbersome?

Yes it is cumbersome but this particular function is meant to be a lower level function me thinks, this particular function writes character by character, sort of like a telegraph machine. find a slightly better inbuilt function to handle writing to file.
I am out of touch with cpp but check, you will find better funcs.
 

quicky008

Technomancer
there are other functions like fprintf() which can accomplish this more elegantly,but i am stuck with a project where the requirement is i must write an int value to a file using fputc () only-therein lies the problem.
 

Nerevarine

Incarnate
there are other functions like fprintf() which can accomplish this more elegantly,but i am stuck with a project where the requirement is i must write an int value to a file using fputc () only-therein lies the problem.
Then write a wrapper that efficiently handles the problem.

I.e. do all your integer or character logic outside and feed fputc all the final characters, character by character. If you need to feed in integer which is multi digit, then remember to convert to string or char array and feed it one by one.
 

aaruni

The Linux Guy
i must write an int value to a file using fputc

Nerevarine is right. You will have to convert your number into a character array, and then call fputc() character by character.

A hint here is, that the character value of a digit is just '0'+i (notice the single quotes around 0). 0 is 47 in ASCII, 1 is 48, etc. So, when you type something like
Code:
int a = '0'+1;
, the compiler will change it to 48+1 = 49 = '1'. Similar for all other digits.
 

quicky008

Technomancer
^thanks,your suggestion did work out for me and i was able to complete the assignment by following this method.


Does anyone know anything about the Curses library in C? What is it exactly and in which scenarios is it used?

It seems this library is included by default with C compilers in linux but is not present in popular C/C++ ides like DEV C for windows etc.

Is it used to work with graphics?

Are there any tutorials available for it that are friendly for beginners?
 

Nerevarine

Incarnate
curses and ncurses..
Yes, its used for rich terminal based GUIs. If you ever opened htop in linux, you would get an idea. Literally every OS in the world starting from lowly puppy linux, solaris to macos support ncurses or curses in their terminal. That gives them an edge over windows when it comes to designing terminal based apps. The command line apps on windows are bastardized versions.
Example :
Glances on Linux,mac runs beautifully because of ncurses support.. Run it in windows and it runs only in server mode.

I think I went on a tangential rant here.. but microsoft has included WSL and by extension curses should be supported although via an emulation layer. Microsoft should just get with the times and just add it natively.

Here's glances on Linux :p
*raw.githubusercontent.com/nicolargo/glances/develop/docs/_static/glances-summary.png

*github.com/nicolargo/glances
 

quicky008

Technomancer
what's the difference between curses and ncurses?Are they both similar to each other (as far as syntax and other parameters are concerned)?

Also can you post the link to a tutorial or guide on implementing curses in C ?
 
Top Bottom