what is the to practice to learn C++, i prefered self study

Guys,

Im rowell, guys what is the best way to learn c++, i have a book here dr. bjarne a c++ 3rd edition..i self study.
I would suggest getting the book called: "Absolute C++" by Walter Savitch. It's a big ass book and is pretty good, covers a range of topics. I think you can go through the book from start to finish.

Anyways while doing that set a bunch of goals for yourself, like making a calendar program, or a calculator or something like that. Then create those programs. Just practice your problem solving along the way and you should get pretty good at C++.
I recommend Accelerated C++ http://www.acceleratedcpp.com/

1
2
int x[10]={0,1,2,3,4,5,6,7,8,9}; //array of ten integer
int (&d)[10] = x; //reference to array of ten integer 
Last edited on
Or if you get bored with reading C++ books or maybe you're reading things you already know, and you have lots of bus time; The tutorial on this website is very cut down and a light read, read it on the bus again and again until you are familiar with all the various aspects of the language. This source was the most beneficial for me. After that I just worked on my own projects, learnt just as much on top of the tutorial.

So the tutorial then working on your own project is a very simple but effective way to learn.
a function that swap two integer using int * argument

int swapp(int a,int b)
{
int *c=&a;
int *d=&b;
c=&b;
d=&a;

return 0;

}
is this correct, i try coding it for two hours now, swapp(int *a, int *b)-its an error
the easier way i know to swap two numbers i like this
1
2
3
4
5
6
int swap( int &n1, int &n2 )
{
     int temp = n1;
     n1 = n2;
     n2 = temp;
}
Last edited on
@RSR102782

Your arguments are ints, rather than int pointers. I think you need this:
1
2
3
4
5
6
void swapp(int* a,int* b)
{

    // ... now swap the contents of pointers a and b

}

And work from there.
Last edited on
I see nothing wrong with his function, other than it is expected to return an int when none is. Aren't he passing them by reference already?

Replace it with
void swap( int& n1, int& n2 )
^^^

(it's also good practice to attach the * or & to the type)
Last edited on

#include <iostream>
#include <cstring>

using namespace std;

class x {
char *p;
public:
x(char *i)
{
int l;
l=strlen(i);
p=new char [l];
if(!p) { cout<<"Error"; exit(1); }
strcpy(p,i);
}
~x() { delete p; }
char *get() { return p;};
};
void show(x a)
{
cout<<a.get()<<endl;
}
int main()
{
x a("C++ World of Object"), b("Im Here");
show(a);
show(b);
cin.get();
return 0;
}

can anyone tell me what happen with this code its seems a signal for bug.
is this correct for int type swapping ???

1
2
3
4
5
6
7
8
int swap( int &n1, int &n2 )
{

n1 = n1 + n2 ;
n2 = n1 - n2 ;
n1 = n1 - n2 ;

}
closed account (zb0S216C)
Swapping two ints? Maybe this is Adequate:

1
2
3
4
5
6
void Switch( int &Left, int &Right )
{
    int Buffer( Left );
    Left = Right;
    Right = Buffer;
}


Wazzak
int a,b,c,d;

for(a=0;a<4;a++)
{
a=b;
b=c;
c=d;
d=a;
}

now swap the int type...
//taken from the book of c++everyone except for the class part
#include <iostream>
#include <string>
using namespace std;
class getkey
{
public:
~getkey()
{
cin.get();
}
};

int main()
{
getkey *x;
string credit_card_number = "4123-5678-9012-3450";
int i = 0;
while (i < credit_card_number.length())
{
string ch = credit_card_number.substr(i, 1);
if (ch == " " || ch == "-")
{
string before = credit_card_number.substr(0, i);
string after = credit_card_number.substr(i + 1);
credit_card_number = before + after;
}
else
{
i++;
}
}
cout << credit_card_number << endl;
x->~getkey();
return 0;
}

now i would like to ask a explanation how does this code remove the "-" and "space"?
azeesmile: interesting. Should work, although I think it's unnecessary (unless you don't have enough room on the stack for one single int variable.)
Last edited on
Topic archived. No new replies allowed.