Please tell me in C++ how does return *this; relates to returning a reference in assignment operator overloading
I am confused because this is a pointer to the current object and the return value of the function is ClassName %26amp; which is a reference. so how can ClassName%26amp; be the return type when we are returning using return *this; statement.
sample code:
MyClass%26amp; MyClass::operator=(const MyClass %26amp;rhs) {
... // Do the assignment operation!
return *this; // Return a reference to myself.
}
C++ question?
well a reference is like a pointer whose address you can't set so *this returns a MyClass Object and c++ can automatically make a MyClass reference from an objec.t
Tuesday, July 14, 2009
Static cast in C++?
guys how can I use The static cast to write this C++ program:
Write a Program that prompts the user to input an integer between 0 and 35. If the number is less than or equal to 9, the program should output the number; otherwise, it should output A for 10, B for 11, C for 12, . . ., and Z for 35. (Hint: Use the cast operator, static_cast%26lt;char%26gt; ( ), for numbers%26gt;=10.
Static cast in C++?
The static_cast can be used to take a data type (like integer, float, etc) and cast it (aka convert it) to another data type like making an integer into a character like you need for this example.
The trick here is that the integer 35 isn't the ascii number for Z, so you will have to do a bit of addition before converting.
For example... if you look at an ascii chart you will notice that the capital "Z" is number 90.
static_cast%26lt;char%26gt;(90) will then give you the capital Z. So what we need to do is take the difference between 90 and 35... which is 55 and add it to every number the user types in greater than or equal to 10, then convert it.
So if the user entered.... 11, we would add 55 and get 66 then go static_cast%26lt;char%26gt;(66) to get a character "B". We then print out the character to screen.
As a side note, lower case letters start at 97 for "a" and go to 122 for "z" so that offset will need to be figured out if you want to use lower case.
I will let you figure that one out.
Hope this helps you out. Enjoy!
Reply:Please make few modifications to support your needs. The lines are broken.
To contact me mail me at m_gopi_m@yahoo.co.in.
I hope this is the program you are expecting.
//program for static casting.
#include%26lt;iostream.h%26gt;
#include%26lt;stdio.h%26gt;
#include%26lt;conio.h%26gt;
char static_cast(int);
void main()
{
int num;
char a;
clrscr();
cout%26lt;%26lt;"\n Enter a number: ";
cin%26gt;%26gt;num;
if(num%26gt;=0 %26amp;%26amp; num%26lt;=9)
{
cout%26lt;%26lt;"\n\n The given number: "%26lt;%26lt;num;
cout%26lt;%26lt;"\n\n The corresponding character: "%26lt;%26lt;num;
}
else if(num%26gt;9 %26amp;%26amp; num%26lt;36)
{
a=static_cast(num);
cout%26lt;%26lt;"\n\n The given number: "%26lt;%26lt;num;
cout%26lt;%26lt;"\n The corresponding character: "%26lt;%26lt;a;
}
else
cout%26lt;%26lt;"\n\n Cannot predict the value.";
getch();
}
char static_cast(int num)
{
int first='A'; //static casting is invoked here.
char a;
num-=10;
a=first+num;
return(a);
}
Write a Program that prompts the user to input an integer between 0 and 35. If the number is less than or equal to 9, the program should output the number; otherwise, it should output A for 10, B for 11, C for 12, . . ., and Z for 35. (Hint: Use the cast operator, static_cast%26lt;char%26gt; ( ), for numbers%26gt;=10.
Static cast in C++?
The static_cast can be used to take a data type (like integer, float, etc) and cast it (aka convert it) to another data type like making an integer into a character like you need for this example.
The trick here is that the integer 35 isn't the ascii number for Z, so you will have to do a bit of addition before converting.
For example... if you look at an ascii chart you will notice that the capital "Z" is number 90.
static_cast%26lt;char%26gt;(90) will then give you the capital Z. So what we need to do is take the difference between 90 and 35... which is 55 and add it to every number the user types in greater than or equal to 10, then convert it.
So if the user entered.... 11, we would add 55 and get 66 then go static_cast%26lt;char%26gt;(66) to get a character "B". We then print out the character to screen.
As a side note, lower case letters start at 97 for "a" and go to 122 for "z" so that offset will need to be figured out if you want to use lower case.
I will let you figure that one out.
Hope this helps you out. Enjoy!
Reply:Please make few modifications to support your needs. The lines are broken.
To contact me mail me at m_gopi_m@yahoo.co.in.
I hope this is the program you are expecting.
//program for static casting.
#include%26lt;iostream.h%26gt;
#include%26lt;stdio.h%26gt;
#include%26lt;conio.h%26gt;
char static_cast(int);
void main()
{
int num;
char a;
clrscr();
cout%26lt;%26lt;"\n Enter a number: ";
cin%26gt;%26gt;num;
if(num%26gt;=0 %26amp;%26amp; num%26lt;=9)
{
cout%26lt;%26lt;"\n\n The given number: "%26lt;%26lt;num;
cout%26lt;%26lt;"\n\n The corresponding character: "%26lt;%26lt;num;
}
else if(num%26gt;9 %26amp;%26amp; num%26lt;36)
{
a=static_cast(num);
cout%26lt;%26lt;"\n\n The given number: "%26lt;%26lt;num;
cout%26lt;%26lt;"\n The corresponding character: "%26lt;%26lt;a;
}
else
cout%26lt;%26lt;"\n\n Cannot predict the value.";
getch();
}
char static_cast(int num)
{
int first='A'; //static casting is invoked here.
char a;
num-=10;
a=first+num;
return(a);
}
Need help fixing the errors in this Template C++Program. I'm lost.Seems to be the same error repeatedly.Thanks
#include %26lt;iostream%26gt;
using namespace std;
template %26lt;class T%26gt;
class Complex
{
friend ostream %26amp;operator%26lt;%26lt;( ostream %26amp;, const Complex %26amp; );
friend istream %26amp;operator%26gt;%26gt;( istream %26amp;, Complex %26amp; );
public:
Complex(const T = 0.0, const T = 0.0 ); // constructor
T%26amp; operator+( const Complex%26amp; ) const; // addition
T%26amp; operator-( const Complex%26amp; ) const; // subtraction
T%26amp; operator*( const Complex%26amp; ) const; // multiplication
T%26amp; operator=( const Complex%26amp; ); // assignment
T%26amp; operator==( const Complex%26amp; ) const;
T%26amp; operator!=( const Complex%26amp; ) const;
// T%26amp; operator[](double);
private:
double real; // real part
double imaginary; // imaginary part
};
// Constructor
template %26lt;class T%26gt;
Complex%26lt;T%26gt;::Complex(T r, T i)
{
T real = r;
T imaginary = i;
}
// Overloaded addition operator
template %26lt;class T%26gt;
T %26amp;Complex%26lt;T%26gt;::operator+(const Complex %26amp;operand2) const
{
Complex sum;
sum.real = real + operand2.real;
sum.imaginary = imaginary + operand2.imaginary;
return sum;
}
// Overloaded subtraction operator
template %26lt;class T%26gt;
T %26amp;Complex%26lt;T%26gt;::operator-(const Complex %26amp;operand2) const
{
Complex diff;
diff.real = real - operand2.real;
diff.imaginary = imaginary - operand2.imaginary;
return diff;
}
// Overloaded multiplication operator
template %26lt;class T%26gt;
T %26amp;Complex %26lt;T%26gt;::operator*(const Complex %26amp;operand2 ) const
{
Complex times;
times.real = real * operand2.real + imaginary * operand2.imaginary;
times.imaginary = real * operand2.imaginary + imaginary * operand2.real;
return times;
}
// Overloaded = operator
template %26lt;class T%26gt;
T %26amp;Complex%26lt;T%26gt;::operator=(const Complex %26amp;right )
{
real = right.real;
imaginary = right.imaginary;
return *this; // enables concatenation
}
template %26lt;class T%26gt;
T %26amp;Complex%26lt;T%26gt;::operator==( const Complex %26amp;right ) const
{ return right.real == real %26amp;%26amp; right.imaginary == imaginary ? true : false; }
template %26lt;class T%26gt;
T %26amp;Complex%26lt;T%26gt;::operator!=( const Complex %26amp;right ) const
{ return !( *this == right ); }
template %26lt;class T%26gt;
ostream%26amp; operator%26lt;%26lt;( ostream %26amp;output, const Complex%26lt;T%26gt; %26amp;complex )
{
output %26lt;%26lt; complex.real %26lt;%26lt; " + " %26lt;%26lt; complex.imaginary %26lt;%26lt; 'i';
return output;
}
template %26lt;class T%26gt;
istream%26amp; operator%26gt;%26gt;( istream %26amp;input, Complex%26lt;T%26gt; %26amp;complex )
{
input %26gt;%26gt; complex.real;
input.ignore( 3 ); // skip spaces and +
input %26gt;%26gt; complex.imaginary;
input.ignore( 2 );
return input;
}
//Use the following main function to test your program
int main()
{
double a=4.3, b = 8.2, c = 3.3, d = 1.1;
Complex%26lt;double%26gt; x, y(a,b), z(c,d), k;
cout %26lt;%26lt; "Enter a complex number in the form: a + bi\n? ";
cin %26gt;%26gt; k;
cout %26lt;%26lt; "x: " %26lt;%26lt; x %26lt;%26lt; "\ny: " %26lt;%26lt; y %26lt;%26lt; "\nz: " %26lt;%26lt; z %26lt;%26lt; "\nk: "
%26lt;%26lt; k %26lt;%26lt; '\n';
(x = y + z);
cout %26lt;%26lt; "\nx = y + z:\n" %26lt;%26lt; x %26lt;%26lt; " = " %26lt;%26lt; y %26lt;%26lt; " + " %26lt;%26lt; z %26lt;%26lt; '\n';
(x = y - z);
cout %26lt;%26lt; "\nx = y - z:\n" %26lt;%26lt; x %26lt;%26lt; " = " %26lt;%26lt; y %26lt;%26lt; " - " %26lt;%26lt; z %26lt;%26lt; '\n';
(x = y * z);
cout %26lt;%26lt; "\nx = y * z:\n" %26lt;%26lt; x %26lt;%26lt; " = " %26lt;%26lt; y %26lt;%26lt; " * " %26lt;%26lt; z %26lt;%26lt; "\n\n";
if ( x != k )
cout %26lt;%26lt; x %26lt;%26lt; " != " %26lt;%26lt; k %26lt;%26lt; '\n';
cout %26lt;%26lt; '\n';
x = k;
if ( x == k )
cout %26lt;%26lt; x %26lt;%26lt; " == " %26lt;%26lt; k %26lt;%26lt; '\n';
int e=4, f=8, g=3, h=2;
Complex%26lt;int%26gt; l, m( e,f), n(g,h ), p;
cout %26lt;%26lt; "Enter a complex number in the form: a + bi\n? ";
cin %26gt;%26gt; p;
cout %26lt;%26lt; "x: " %26lt;%26lt; l %26lt;%26lt; "\ny: " %26lt;%26lt; m %26lt;%26lt; "\nz: " %26lt;%26lt; n %26lt;%26lt; "\nk: "
%26lt;%26lt; p %26lt;%26lt; '\n';
(l = m + n);
cout %26lt;%26lt; "\nx = y + z:\n" %26lt;%26lt; l %26lt;%26lt; " = " %26lt;%26lt; m %26lt;%26lt; " + " %26lt;%26lt; n %26lt;%26lt; '\n';
(l = m - n);
cout %26lt;%26lt; "\nx = y - z:\n" %26lt;%26lt; l %26lt;%26lt; " = " %26lt;%26lt; m %26lt;%26lt; " - " %26lt;%26lt; n %26lt;%26lt; '\n';
(l = m * n);
cout %26lt;%26lt; "\nx = y * z:\n" %26lt;%26lt; l %26lt;%26lt; " = " %26lt;%26lt; m %26lt;%26lt; " * " %26lt;%26lt; n %26lt;%26lt; "\n\n";
if ( l != p )
cout %26lt;%26lt; l %26lt;%26lt; " != " %26lt;%26lt; p %26lt;%26lt; '\n';
cout %26lt;%26lt; '\n';
l = p;
if ( l == p )
cout %26lt;%26lt; l %26lt;%26lt; " == " %26lt;%26lt; p %26lt;%26lt; '\n';
return 0;
}
Need help fixing the errors in this Template C++Program. I'm lost.Seems to be the same error repeatedly.Thanks
Haven't compiled your code, but looking at the definitions:
-Your +,-,* operators should return Complex%26lt;T%26gt;, not T - when you add two Complex%26lt;T%26gt;, you want to get a Complex%26lt;T%26gt; object and not a T object back.
-Your ==,!= operators should return bool, as these are logical operators.
-Your private members should be of type T - otherwise, when you assign them, you are implicitly saying that T is a specific type.
-In your constructor, you are redeclaring your members; use something like
Complex( T r, T i ) : real( r ), imaginary( i ) { }
to initialize them.
-In your operator implementations, you need to set a type for your return object. You'll need to declare them as Complex%26lt;T%26gt; objects, not just Complex.
Reply:It might help to know what the error message is./
local florist
using namespace std;
template %26lt;class T%26gt;
class Complex
{
friend ostream %26amp;operator%26lt;%26lt;( ostream %26amp;, const Complex %26amp; );
friend istream %26amp;operator%26gt;%26gt;( istream %26amp;, Complex %26amp; );
public:
Complex(const T = 0.0, const T = 0.0 ); // constructor
T%26amp; operator+( const Complex%26amp; ) const; // addition
T%26amp; operator-( const Complex%26amp; ) const; // subtraction
T%26amp; operator*( const Complex%26amp; ) const; // multiplication
T%26amp; operator=( const Complex%26amp; ); // assignment
T%26amp; operator==( const Complex%26amp; ) const;
T%26amp; operator!=( const Complex%26amp; ) const;
// T%26amp; operator[](double);
private:
double real; // real part
double imaginary; // imaginary part
};
// Constructor
template %26lt;class T%26gt;
Complex%26lt;T%26gt;::Complex(T r, T i)
{
T real = r;
T imaginary = i;
}
// Overloaded addition operator
template %26lt;class T%26gt;
T %26amp;Complex%26lt;T%26gt;::operator+(const Complex %26amp;operand2) const
{
Complex sum;
sum.real = real + operand2.real;
sum.imaginary = imaginary + operand2.imaginary;
return sum;
}
// Overloaded subtraction operator
template %26lt;class T%26gt;
T %26amp;Complex%26lt;T%26gt;::operator-(const Complex %26amp;operand2) const
{
Complex diff;
diff.real = real - operand2.real;
diff.imaginary = imaginary - operand2.imaginary;
return diff;
}
// Overloaded multiplication operator
template %26lt;class T%26gt;
T %26amp;Complex %26lt;T%26gt;::operator*(const Complex %26amp;operand2 ) const
{
Complex times;
times.real = real * operand2.real + imaginary * operand2.imaginary;
times.imaginary = real * operand2.imaginary + imaginary * operand2.real;
return times;
}
// Overloaded = operator
template %26lt;class T%26gt;
T %26amp;Complex%26lt;T%26gt;::operator=(const Complex %26amp;right )
{
real = right.real;
imaginary = right.imaginary;
return *this; // enables concatenation
}
template %26lt;class T%26gt;
T %26amp;Complex%26lt;T%26gt;::operator==( const Complex %26amp;right ) const
{ return right.real == real %26amp;%26amp; right.imaginary == imaginary ? true : false; }
template %26lt;class T%26gt;
T %26amp;Complex%26lt;T%26gt;::operator!=( const Complex %26amp;right ) const
{ return !( *this == right ); }
template %26lt;class T%26gt;
ostream%26amp; operator%26lt;%26lt;( ostream %26amp;output, const Complex%26lt;T%26gt; %26amp;complex )
{
output %26lt;%26lt; complex.real %26lt;%26lt; " + " %26lt;%26lt; complex.imaginary %26lt;%26lt; 'i';
return output;
}
template %26lt;class T%26gt;
istream%26amp; operator%26gt;%26gt;( istream %26amp;input, Complex%26lt;T%26gt; %26amp;complex )
{
input %26gt;%26gt; complex.real;
input.ignore( 3 ); // skip spaces and +
input %26gt;%26gt; complex.imaginary;
input.ignore( 2 );
return input;
}
//Use the following main function to test your program
int main()
{
double a=4.3, b = 8.2, c = 3.3, d = 1.1;
Complex%26lt;double%26gt; x, y(a,b), z(c,d), k;
cout %26lt;%26lt; "Enter a complex number in the form: a + bi\n? ";
cin %26gt;%26gt; k;
cout %26lt;%26lt; "x: " %26lt;%26lt; x %26lt;%26lt; "\ny: " %26lt;%26lt; y %26lt;%26lt; "\nz: " %26lt;%26lt; z %26lt;%26lt; "\nk: "
%26lt;%26lt; k %26lt;%26lt; '\n';
(x = y + z);
cout %26lt;%26lt; "\nx = y + z:\n" %26lt;%26lt; x %26lt;%26lt; " = " %26lt;%26lt; y %26lt;%26lt; " + " %26lt;%26lt; z %26lt;%26lt; '\n';
(x = y - z);
cout %26lt;%26lt; "\nx = y - z:\n" %26lt;%26lt; x %26lt;%26lt; " = " %26lt;%26lt; y %26lt;%26lt; " - " %26lt;%26lt; z %26lt;%26lt; '\n';
(x = y * z);
cout %26lt;%26lt; "\nx = y * z:\n" %26lt;%26lt; x %26lt;%26lt; " = " %26lt;%26lt; y %26lt;%26lt; " * " %26lt;%26lt; z %26lt;%26lt; "\n\n";
if ( x != k )
cout %26lt;%26lt; x %26lt;%26lt; " != " %26lt;%26lt; k %26lt;%26lt; '\n';
cout %26lt;%26lt; '\n';
x = k;
if ( x == k )
cout %26lt;%26lt; x %26lt;%26lt; " == " %26lt;%26lt; k %26lt;%26lt; '\n';
int e=4, f=8, g=3, h=2;
Complex%26lt;int%26gt; l, m( e,f), n(g,h ), p;
cout %26lt;%26lt; "Enter a complex number in the form: a + bi\n? ";
cin %26gt;%26gt; p;
cout %26lt;%26lt; "x: " %26lt;%26lt; l %26lt;%26lt; "\ny: " %26lt;%26lt; m %26lt;%26lt; "\nz: " %26lt;%26lt; n %26lt;%26lt; "\nk: "
%26lt;%26lt; p %26lt;%26lt; '\n';
(l = m + n);
cout %26lt;%26lt; "\nx = y + z:\n" %26lt;%26lt; l %26lt;%26lt; " = " %26lt;%26lt; m %26lt;%26lt; " + " %26lt;%26lt; n %26lt;%26lt; '\n';
(l = m - n);
cout %26lt;%26lt; "\nx = y - z:\n" %26lt;%26lt; l %26lt;%26lt; " = " %26lt;%26lt; m %26lt;%26lt; " - " %26lt;%26lt; n %26lt;%26lt; '\n';
(l = m * n);
cout %26lt;%26lt; "\nx = y * z:\n" %26lt;%26lt; l %26lt;%26lt; " = " %26lt;%26lt; m %26lt;%26lt; " * " %26lt;%26lt; n %26lt;%26lt; "\n\n";
if ( l != p )
cout %26lt;%26lt; l %26lt;%26lt; " != " %26lt;%26lt; p %26lt;%26lt; '\n';
cout %26lt;%26lt; '\n';
l = p;
if ( l == p )
cout %26lt;%26lt; l %26lt;%26lt; " == " %26lt;%26lt; p %26lt;%26lt; '\n';
return 0;
}
Need help fixing the errors in this Template C++Program. I'm lost.Seems to be the same error repeatedly.Thanks
Haven't compiled your code, but looking at the definitions:
-Your +,-,* operators should return Complex%26lt;T%26gt;, not T - when you add two Complex%26lt;T%26gt;, you want to get a Complex%26lt;T%26gt; object and not a T object back.
-Your ==,!= operators should return bool, as these are logical operators.
-Your private members should be of type T - otherwise, when you assign them, you are implicitly saying that T is a specific type.
-In your constructor, you are redeclaring your members; use something like
Complex( T r, T i ) : real( r ), imaginary( i ) { }
to initialize them.
-In your operator implementations, you need to set a type for your return object. You'll need to declare them as Complex%26lt;T%26gt; objects, not just Complex.
Reply:It might help to know what the error message is./
local florist
Java Conditional Operator Problem PLS help?
Hi I have a problem that I cant solve. We have a school exercise in Java abt conditional operator (?:) We were asked to code the following:
a=1
b=2
c=3
highest number is = 3
I was able to code this correctly after a LONG time, and with some help. Now I am trying to do more practice and is trying to code the following, and it seems like I am stuck in deep mud again. Pls help me. I am trying to code:
a=1
b=2
c=3
d=4
highest number is = 4
pls help me
Java Conditional Operator Problem PLS help?
just remember, all the ?: operator is a short hand for if-then-else
or in this case multiple if then elseif's(don't know the exact java syntax)
but would look something like
(a%26gt;b)?((a%26gt;c)?((a%26gt;d)?print a : print d):((c%26gt;d)? print c : print d)...
if you got the above part all this last part is one more check at each level of the if then elseif all you doing is writing as you comparing a to the other 3 first... if true in all cases a is highest if false in last case d is highest... if false before then you need to check that variable with the rest down the line gave you if c%26gt;a case all you need to do is finish with if b%26gt;a using paranthesis and even adding lines might help for readability
a=1
b=2
c=3
highest number is = 3
I was able to code this correctly after a LONG time, and with some help. Now I am trying to do more practice and is trying to code the following, and it seems like I am stuck in deep mud again. Pls help me. I am trying to code:
a=1
b=2
c=3
d=4
highest number is = 4
pls help me
Java Conditional Operator Problem PLS help?
just remember, all the ?: operator is a short hand for if-then-else
or in this case multiple if then elseif's(don't know the exact java syntax)
but would look something like
(a%26gt;b)?((a%26gt;c)?((a%26gt;d)?print a : print d):((c%26gt;d)? print c : print d)...
if you got the above part all this last part is one more check at each level of the if then elseif all you doing is writing as you comparing a to the other 3 first... if true in all cases a is highest if false in last case d is highest... if false before then you need to check that variable with the rest down the line gave you if c%26gt;a case all you need to do is finish with if b%26gt;a using paranthesis and even adding lines might help for readability
C++ syntax help?
i'm trying to write a matrix class for c++, but i'm a bit rusty.
I have the following operators defined in my main program file but i'd like to include them in my header and implementation file instead. my syntax is a bit rusty but I think it should look something like this:
template%26lt;class itemType%26gt;
apmatrix%26lt;itemType%26gt; operator+(const apmatrix%26lt;itemType%26gt;%26amp; a, const apmatrix%26lt;itemType%26gt;%26amp; b)
{
//declare a matrix temp of type T to store the result and return this matrix
apmatrix%26lt;int%26gt; temp;
temp.resize(a.numrows(),b.numcols());
for(int i = 0; i %26lt; a.numrows(); i++)
for(int j = 0; j %26lt; a.numcols(); j++)
temp[i][j] = a[i][j] + b[i][j];
return temp;
}
I need to know how to change this to make it work, and what to put in my header.
the apmatrix class already has the operator = defined, but when i tried copying the similar format for my + operator in my header file i got this error:
C++ syntax help?
the plus operator is a binary not a tertiary operator - so it only takes one argument. in other words it's an operation between the current class and another parameter, so it wouldn't work with 3 things to operate on (this, a and b). Try this instead-
template%26lt;class T%26gt;
class m {
m%26lt;T%26gt; operator+(const m%26lt;T%26gt;%26amp; b) const
{
// use "(*this)" instead of "a"
// the function is const because it doesn't alter "this"
}
};
I have the following operators defined in my main program file but i'd like to include them in my header and implementation file instead. my syntax is a bit rusty but I think it should look something like this:
template%26lt;class itemType%26gt;
apmatrix%26lt;itemType%26gt; operator+(const apmatrix%26lt;itemType%26gt;%26amp; a, const apmatrix%26lt;itemType%26gt;%26amp; b)
{
//declare a matrix temp of type T to store the result and return this matrix
apmatrix%26lt;int%26gt; temp;
temp.resize(a.numrows(),b.numcols());
for(int i = 0; i %26lt; a.numrows(); i++)
for(int j = 0; j %26lt; a.numcols(); j++)
temp[i][j] = a[i][j] + b[i][j];
return temp;
}
I need to know how to change this to make it work, and what to put in my header.
the apmatrix class already has the operator = defined, but when i tried copying the similar format for my + operator in my header file i got this error:
C++ syntax help?
the plus operator is a binary not a tertiary operator - so it only takes one argument. in other words it's an operation between the current class and another parameter, so it wouldn't work with 3 things to operate on (this, a and b). Try this instead-
template%26lt;class T%26gt;
class m {
m%26lt;T%26gt; operator+(const m%26lt;T%26gt;%26amp; b) const
{
// use "(*this)" instead of "a"
// the function is const because it doesn't alter "this"
}
};
C for beginners problem....supposed to determine output of relational operators?
My homework asks "What is the output of the following?" and i'm thinking something is horribly wrong with this code or they skipped this in class.
the only thing vaguely tickling my brain is the true/false (1 or 0) thing. but if that's so, i didn't realize you could assign 1 or 0 to integers using true / false tests.
int main()
{
int a = 3, b = -1, c = 0, i, j, k, n;
i = a || b || c;
j = a %26amp;%26amp; b %26amp;%26amp; c;
k = a || b %26amp;%26amp; c;
n = a %26amp;%26amp; !(b || c);
printf("\ni = %d, j = %d, k = $d, n = %d\n", i, j, k, n);
}
C for beginners problem....supposed to determine output of relational operators?
Actually, this code prints out 1, 0, "$d", and 1 (k's value after "n ="), because you have "k = $d" instead of "%d". The output is:
i = 1, j = 0, k = $d, n = 1
Unless that's your typo, maybe you are being taught the value of matching up printf arguments. :-)
If you meant "%d", it's:
i = 1, j = 0, k = 1, n = 0
Reply:C treats anything other than 0 as true even negative values as true.
so if you write anything like i=2%26amp;%26amp;5 it sets value of i to 1.
so i=1 bcoz a and b are true, but j=0 boz of c which is zero means false.
%26amp;%26amp; operator has higher precedence than || so it is true again and hence k=1.
not operator has highest precedence. b||c=true as b is non zero
!(b||c) is false which when anded with a gives false setting n to 0
hence
your result is
i=1
j=0
k=1
n=0
Reply:C has no real concept of true and false. 0 is false and everything else is true. As a matter of fact and (%26amp;%26amp;) is an operator that says if either operand is 0 then return 0 otherwise return 1. Or (||) is an operator that says if either operand is not zero then return 1 else return 0.
This is the concept your teacher is testing you on with this question.
Reply:i = true
j, k, n = false
I think, just by looking at it.
Now, integers can be booleans; %26lt;= 0 is false (example, 0 and -1 are false, anything 0 or below)
And a true integer is anything above 0.
the only thing vaguely tickling my brain is the true/false (1 or 0) thing. but if that's so, i didn't realize you could assign 1 or 0 to integers using true / false tests.
int main()
{
int a = 3, b = -1, c = 0, i, j, k, n;
i = a || b || c;
j = a %26amp;%26amp; b %26amp;%26amp; c;
k = a || b %26amp;%26amp; c;
n = a %26amp;%26amp; !(b || c);
printf("\ni = %d, j = %d, k = $d, n = %d\n", i, j, k, n);
}
C for beginners problem....supposed to determine output of relational operators?
Actually, this code prints out 1, 0, "$d", and 1 (k's value after "n ="), because you have "k = $d" instead of "%d". The output is:
i = 1, j = 0, k = $d, n = 1
Unless that's your typo, maybe you are being taught the value of matching up printf arguments. :-)
If you meant "%d", it's:
i = 1, j = 0, k = 1, n = 0
Reply:C treats anything other than 0 as true even negative values as true.
so if you write anything like i=2%26amp;%26amp;5 it sets value of i to 1.
so i=1 bcoz a and b are true, but j=0 boz of c which is zero means false.
%26amp;%26amp; operator has higher precedence than || so it is true again and hence k=1.
not operator has highest precedence. b||c=true as b is non zero
!(b||c) is false which when anded with a gives false setting n to 0
hence
your result is
i=1
j=0
k=1
n=0
Reply:C has no real concept of true and false. 0 is false and everything else is true. As a matter of fact and (%26amp;%26amp;) is an operator that says if either operand is 0 then return 0 otherwise return 1. Or (||) is an operator that says if either operand is not zero then return 1 else return 0.
This is the concept your teacher is testing you on with this question.
Reply:i = true
j, k, n = false
I think, just by looking at it.
Now, integers can be booleans; %26lt;= 0 is false (example, 0 and -1 are false, anything 0 or below)
And a true integer is anything above 0.
Sample C++ code?
whats the best website to find sample programs of C++ code. I need to know how to do operator overloading, and i need to know about classes. the book is very confusing, and doesn't show code of how these things are correctly used in a program.
Sample C++ code?
Hello
You are looking to use this code / learn. There are two websites.
http://www.cplusplus.com
http://www.codeproject.com
floral deliveries
Sample C++ code?
Hello
You are looking to use this code / learn. There are two websites.
http://www.cplusplus.com
http://www.codeproject.com
floral deliveries
Subscribe to:
Posts (Atom)