You already know most of C++
If you can write C, you can compile it as C++ almost unchanged. Same if, same loops, same functions, same pointers, same arrays, same operator precedence. C++ adds on top; it does not replace.
So the question is not "how do I learn a new language". It is "which additions actually earn their place". For a B.Tech student doing data structures, contests and lab work, there are five.
1. Containers that manage their own memory
This is the big one. In C, an array that grows means malloc, realloc, tracking capacity, and remembering to free. In C++ it is one line.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> marks; // empty, grows as needed
marks.push_back(90);
marks.push_back(75);
marks.push_back(88);
cout << marks.size() << " marks\n"; // 3
for (int m : marks) cout << m << " ";
cout << "\n";
return 0; // memory freed automatically
}
No malloc, no free, no leak. When marks goes out of scope its destructor runs and returns the memory. Half the bugs from the pointers course simply cannot occur here.
2. Streams instead of format specifiers
#include <iostream>
using namespace std;
int main() {
int roll;
double marks;
cout << "Roll and marks: ";
if (!(cin >> roll >> marks)) {
cout << "Bad input\n";
return 1;
}
cout << "Roll " << roll << " scored " << marks << "\n";
return 0;
}
cin and cout know the type of what you give them, so there is no %d versus %f to get wrong. Reading into a double with the wrong specifier — a real printf bug — is impossible here. The failed-read check is if (!(cin >> x)), which is the C++ version of checking scanf's return value.
"\n" and endl both end a line. endl also flushes the buffer, which is slower. In a loop printing 100,000 lines, endl can be the difference between passing and failing a time limit. Use "\n".
3. References
#include <iostream>
using namespace std;
void increment(int &x) { x++; } // & in the parameter, not at the call
int main() {
int n = 5;
increment(n); // no & here
cout << n << "\n"; // 6
return 0;
}
In C this needs a pointer, a & at the call site and a * on every use. A reference is another name for the same variable. Lesson 2 is entirely about when to use which.
4. Function overloading and default arguments
#include <iostream>
using namespace std;
int area(int side) { return side * side; }
int area(int l, int b) { return l * b; }
double area(double r) { return 3.14159 * r * r; }
void greet(string name, string title = "student") {
cout << "Hello " << title << " " << name << "\n";
}
int main() {
cout << area(4) << "\n"; // 16
cout << area(3, 5) << "\n"; // 15
cout << area(2.0) << "\n"; // 12.5664
greet("Adnan"); // Hello student Adnan
greet("Priya", "topper"); // Hello topper Priya
return 0;
}
Three functions share one name. The compiler picks by the number and type of arguments. In C you would need area_square, area_rect, area_circle. Note that the return type alone cannot distinguish overloads — only parameters can.
5. Classes
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
int marks[5];
public:
Student(string n) : name(n) {
for (int i = 0; i < 5; i++) marks[i] = 0;
}
void setMark(int subject, int value) {
if (subject >= 0 && subject < 5 && value >= 0 && value <= 100)
marks[subject] = value;
}
double average() const {
int sum = 0;
for (int i = 0; i < 5; i++) sum += marks[i];
return sum / 5.0;
}
string getName() const { return name; }
};
int main() {
Student s("Adnan");
s.setMark(0, 91);
s.setMark(1, 84);
cout << s.getName() << " average " << s.average() << "\n"; // 35
return 0;
}
private means only the class's own functions can touch name and marks. setMark validates before writing, so a mark of 250 cannot get in. In C a struct field is open to everyone and every piece of code has to remember the rules.
The : name(n) part is the initialiser list — it initialises members before the constructor body runs. const after a member function means "this function does not modify the object".
What does not change
Pointers still exist and still work the same way. Arrays still decay. sizeof is unchanged. Integer overflow still wraps silently. Everything in the C course still applies — C++ gives you better defaults, not different hardware.
using namespace std; is fine for lab work, contests and small files. In a large project or a header file it drags every standard name into scope and causes hard-to-read collisions. Professional code writes std::vector, std::cout. Know both; use the short form only in a single-file program.
Compiling
/* Save as prog.cpp, then:
*
* g++ -std=c++17 -Wall -Wextra prog.cpp -o prog
* ./prog
*/
#include <iostream>
int main() {
std::cout << "C++ is working\n";
return 0;
}
Use g++, not gcc, so the C++ standard library gets linked. -std=c++17 gives you a modern language; without it your compiler may default to something older and reject valid code.
Do not try to learn all of C++. Templates, inheritance, virtual functions, move semantics and smart pointers are real and useful, but you do not need them to top your data structures lab. Learn vector, string, map, set, sort and references properly. That set covers the overwhelming majority of what you will write in the next two years.