Tips and Tricks...
why we need to know about some tricks in any(C++) language ?
we may use tips and tricks in our program .another one reason to learn the tips and tricks is
mostly we develope program based on sample code from the net...
at that time we need to understand what they have done in their program, for that reason alone
we need to know about the tips and tricks in any language...
1. x i = 10;
what is x ?can u make this bit of code as a meaningful one ?
How can we do it ? Think about it if u cant get an idea... see the results below.
.
.
.
.
.
.
.
.
1. typedef int x;
x i = 10;
2. x may be a class with parameterized constructor...
class x
{
int i;
public:
x(int a)
{
i = a;
}
};
2.
Is it possible to make use of this code in C or C++ ? replace() is an user defined function..
replace(5) = 'X';
try....
answer is ....
char s[80] = "Hello world";
char & replace(int i);
int main()
{
replace(5) = 'X'; // this will inserts the X value at space
// the content of s will be
// HelloXWorld
}
char& replace(int i)
{
return s[i];
}
3.Simple reference example in C++
void neg(int &i)
{
i = -i;
}
int main( )
{
int x = 10;
neg(x);
cout << x; // Here the output value is -10
//can't believe it test it with sample programs
}
we may use tips and tricks in our program .another one reason to learn the tips and tricks is
mostly we develope program based on sample code from the net...
at that time we need to understand what they have done in their program, for that reason alone
we need to know about the tips and tricks in any language...
1. x i = 10;
what is x ?can u make this bit of code as a meaningful one ?
How can we do it ? Think about it if u cant get an idea... see the results below.
.
.
.
.
.
.
.
.
1. typedef int x;
x i = 10;
2. x may be a class with parameterized constructor...
class x
{
int i;
public:
x(int a)
{
i = a;
}
};
2.
Is it possible to make use of this code in C or C++ ? replace() is an user defined function..
replace(5) = 'X';
try....
answer is ....
char s[80] = "Hello world";
char & replace(int i);
int main()
{
replace(5) = 'X'; // this will inserts the X value at space
// the content of s will be
// HelloXWorld
}
char& replace(int i)
{
return s[i];
}
3.Simple reference example in C++
void neg(int &i)
{
i = -i;
}
int main( )
{
int x = 10;
neg(x);
cout << x; // Here the output value is -10
//can't believe it test it with sample programs
}
Labels: Cpp
0 Comments:
Post a Comment
<< Home