C++ Program to Copy Strings
Uses assignment operator.
#include <iostream>
#include <string>
int main(){
std::string s="Sample"; std::string d = s;
std::cout << d << std::endl;
return 0;
}Sample
Key takeaway: Practice C++ with 63+ runnable code examples — I/O, loops, math, strings, arrays, pointers, classes, and operator overloading — all with expected output to verify your understanding.
C++ is a general-purpose, object-oriented programming language used for game engines, system software, embedded devices, and high-performance applications. These examples are for CS students, interview candidates, and developers building C++ skills.
Last updated: October 2026 · 63 examples · All platforms
Who is this for: CS students, game dev learners, competitive programmers, and job seekers preparing for C++ interviews. Use this when: you need verified C++ reference code or practice problem solutions.
Related:Online C++ Compiler · C++ Tutorial · C Examples
Uses assignment operator.
#include <iostream>
#include <string>
int main(){
std::string s="Sample"; std::string d = s;
std::cout << d << std::endl;
return 0;
}Sample
Prints a greeting to the console.
#include <iostream>
int main(){
std::cout << "Hello, World!" << std::endl;
return 0;
}Hello, World!
Demonstrates integer I/O (using a sample value).
#include <iostream>
int main(){
int n = 123;
std::cout << "You entered: " << n << std::endl;
return 0;
}You entered: 123
Adds two integers and prints the sum.
#include <iostream>
int main(){
int a=3,b=5;
std::cout << "Sum = " << (a+b) << std::endl;
return 0;
}Sum = 8
Integer division and modulo.
#include <iostream>
int main(){
int a=20,b=3;
std::cout << "Quotient = " << (a/b) << "\nRemainder = " << (a%b) << std::endl;
return 0;
}Quotient = 6 Remainder = 2
Uses sizeof operator.
#include <iostream>
int main(){
std::cout << "sizeof(char) = " << sizeof(char) << "\n";
std::cout << "sizeof(int) = " << sizeof(int) << "\n";
std::cout << "sizeof(float) = " << sizeof(float) << "\n";
std::cout << "sizeof(double) = " << sizeof(double) << std::endl;
return 0;
}sizeof(char) = 1 sizeof(int) = 4 sizeof(float) = 4 sizeof(double) = 8
Swaps using temp variable.
#include <iostream>
int main(){
int a=3,b=4,temp;
temp=a; a=b; b=temp;
std::cout << "a = " << a << ", b = " << b << std::endl;
return 0;
}a = 4, b = 3
Checks parity using modulo.
#include <iostream>
int main(){
int n=7;
std::cout << n << " is " << (n%2==0?"even":"odd") << std::endl;
return 0;
}7 is odd
Checks vowel set.
#include <iostream>
#include <cctype>
int main(){
char c='e';
c = (char)std::tolower((unsigned char)c);
bool v = (c=='a'||c=='e'||c=='i'||c=='o'||c=='u');
std::cout << c << " is " << (v?"vowel":"consonant") << std::endl;
return 0;
}e is vowel
Quadratic formula for ax^2+bx+c.
#include <iostream>
#include <cmath>
int main(){
double a=1,b=-3,c=2;
double d = b*b - 4*a*c;
if(d>=0){
double r1=(-b + std::sqrt(d))/(2*a);
double r2=(-b - std::sqrt(d))/(2*a);
std::cout << "Roots: " << r1 << ", " << r2 << std::endl;
} else {
std::cout << "Complex roots" << std::endl;
}
return 0;
}Roots: 2, 1
Checks leap year rules.
#include <iostream>
int main(){
int y=2024;
bool leap = (y%4==0 && y%100!=0) || (y%400==0);
std::cout << y << " is " << (leap?"leap year":"not leap year") << std::endl;
return 0;
}2024 is leap year
Demonstrates custom ++/-- for a class.
#include <iostream>
struct Counter{
int value;
Counter(int v=0):value(v){}
Counter& operator++(){ value++; return *this; } // pre-increment
Counter operator++(int){ Counter tmp=*this; value++; return tmp; } // post-increment
Counter& operator--(){ value--; return *this; }
Counter operator--(int){ Counter tmp=*this; value--; return tmp; }
};
int main(){
Counter c(5);
++c; c++; --c; c--;
std::cout << "Counter = " << c.value << std::endl;
return 0;
}Counter = 5
Defines Complex with operator-.
#include <iostream>
struct Complex{ double re, im; };
Complex operator-(const Complex& a, const Complex& b){ return {a.re-b.re, a.im-b.im}; }
int main(){
Complex a{3.5, 2.0}, b{2.0, 0.5};
Complex c = a - b;
std::cout << c.re << " + " << c.im << "i" << std::endl;
return 0;
}1.5 + 1.5i
Prints ASCII of a character.
#include <iostream>
int main(){
char ch='A';
std::cout << "ASCII of " << ch << " = " << (int)ch << std::endl;
return 0;
}ASCII of A = 65
Multiplies two numbers.
#include <iostream>
int main(){
int a=7,b=6;
std::cout << "Product = " << (a*b) << std::endl;
return 0;
}Product = 42
Simple star pyramid.
#include <iostream>
int main(){
int n=5;
for(int i=1;i<=n;i++){
for(int s=0;s<n-i;s++) std::cout << ' ';
for(int j=0;j<2*i-1;j++) std::cout << '*';
std::cout << '\n';
}
return 0;
}* *** ***** ******* *********
Computes population std dev.
#include <iostream>
#include <cmath>
int main(){
double a[]={2,4,4,4,5,5,7,9}; int n=8; double sum=0;
for(int i=0;i<n;i++) sum+=a[i]; double mean=sum/n;
double v=0; for(int i=0;i<n;i++) v+=(a[i]-mean)*(a[i]-mean); v/=n;
std::cout << "StdDev = " << std::sqrt(v) << std::endl;
return 0;
}StdDev = 2
Cyclic swap a->b->c.
#include <iostream>
void cyclic(int& a,int& b,int& c){ int t=c; c=b; b=a; a=t; }
int main(){
int a=1,b=2,c=3; cyclic(a,b,c);
std::cout << "a=" << a << " b=" << b << " c=" << c << std::endl;
return 0;
}a=3 b=1 c=2
Counts frequency of a given char.
#include <iostream>
#include <string>
int main(){
std::string s="hello world"; char ch='l'; int c=0;
for(char x: s) if(x==ch) c++;
std::cout << "'" << ch << "' occurs " << c << " times" << std::endl;
return 0;
}'l' occurs 3 times
Classifies characters.
#include <iostream>
#include <cctype>
int main(){
const char* s="Hello World 123!"; int v=0,cons=0,d=0,sp=0,oth=0;
for(int i=0;s[i];i++){ unsigned char c=s[i]; if(std::isalpha(c)){ char t=(char)std::tolower(c); if(t=='a'||t=='e'||t=='i'||t=='o'||t=='u') v++; else cons++; } else if(std::isdigit(c)) d++; else if(std::isspace(c)) sp++; else oth++; }
std::cout << "Vowels=" << v << " Consonants=" << cons << " Digits=" << d << " Spaces=" << sp << " Others=" << oth << std::endl;
return 0;
}Vowels=3 Consonants=7 Digits=3 Spaces=2 Others=1
Filters only letters.
#include <iostream>
#include <cctype>
int main(){
const char* s="H3ll0, W0rld!"; char o[100]; int j=0;
for(int i=0;s[i];i++){ if(std::isalpha((unsigned char)s[i])) o[j++]=s[i]; } o[j]='\0';
std::cout << o << std::endl;
return 0;
}HllWrld
Uses std::string length.
#include <iostream>
#include <string>
int main(){
std::string s="hello"; std::cout << "Length = " << s.length() << std::endl;
return 0;
}Length = 5
Uses string concatenation.
#include <iostream>
#include <string>
int main(){
std::string a="Hello", b=" World"; std::cout << (a+b) << std::endl;
return 0;
}Hello World
Sorts array of strings.
#include <iostream>
#include <vector>
#include <algorithm>
int main(){
std::vector<std::string> s = {"banana","apple","cherry"};
std::sort(s.begin(), s.end());
std::cout << s[0] << ' ' << s[1] << ' ' << s[2] << std::endl;
return 0;
}apple banana cherry
Defines struct and prints.
#include <iostream>
#include <string>
struct Student{ std::string name; int age; double marks; };
int main(){
Student s{"Alice",20,92.5};
std::cout << s.name << ' ' << s.age << ' ' << s.marks << std::endl;
return 0;
}Alice 20 92.5
Adds feet-inch distances.
#include <iostream>
struct Dist{ int feet; int inch; };
int main(){
Dist a{5,8}, b{3,11}, c{};
c.feet=a.feet+b.feet; c.inch=a.inch+b.inch;
if(c.inch>=12){ c.feet+=c.inch/12; c.inch%=12; }
std::cout << c.feet << " feet " << c.inch << " inch" << std::endl;
return 0;
}9 feet 7 inch
Adds complex numbers.
#include <iostream>
struct Complex{ double re, im; };
Complex add(Complex a,Complex b){ return {a.re+b.re, a.im+b.im}; }
int main(){
Complex a{1.5,2.0}, b{2.5,3.0}; Complex c=add(a,b);
std::cout << c.re << " + " << c.im << "i" << std::endl;
return 0;
}4 + 5i
Subtracts times with borrow.
#include <iostream>
struct Time{ int h,m,s; };
int main(){
Time a{2,40,30}, b{1,20,50}, d{};
if(b.s>a.s){ b.m++; a.s+=60; } d.s=a.s-b.s;
if(b.m>a.m){ b.h++; a.m+=60; } d.m=a.m-b.m;
d.h=a.h-b.h;
std::cout << (d.h<10?"0":"") << d.h << ":" << (d.m<10?"0":"") << d.m << ":" << (d.s<10?"0":"") << d.s << std::endl;
return 0;
}01:19:40
g++ program.cpp -std=c++17 -O2 -o program && ./program. Or use our free online C++ compiler. -std=c++17 flag when compiling. Who is this for: Multiple audiences across industries and experience levels:
Use this when: you need verified reference code, want to complete an assignment, prepare for interviews, or warm up before a systems/game project. Best for beginners through intermediate-level programmers.
The TIOBE Index ranked C++ as #2 in 2024. It powers:
C++ roles pay $120K–$200K+ in these industries.
| Feature | C++ | C |
|---|---|---|
| OOP | Classes, inheritance, polymorphism | No |
| STL | vector, map, set, algorithms | Manual arrays |
| Memory | new/delete + smart pointers | malloc/free |
| Strings | std::string | char arrays |
| Errors | try/catch exceptions | Error codes |
Related:C++ Tutorial · Online C++ Compiler · C Examples
Direct answer: C++ developers work in high-performance computing, game development, finance, embedded systems, and infrastructure. Key roles include:
Absolutely yes. C++ is one of the highest-paying programming languages because the jobs that require it are technically demanding and mission-critical. While there are fewer C++ jobs than JavaScript or Python jobs in raw count, C++ positions pay 20–40% more on average because the supply of skilled C++ developers is much smaller than demand. According to the StackOverflow 2024 Survey, C++ developers rank among the top 5 highest-paid globally.
| Experience | India (LPA) | USA (USD/year) | Typical Role |
|---|---|---|---|
| Fresher (0–1 yr) | ₹5–10 LPA | $70,000–$100,000 | Junior Developer |
| Mid (3–5 yrs) | ₹15–30 LPA | $120,000–$160,000 | Software Engineer |
| Senior (5–10 yrs) | ₹30–60 LPA | $160,000–$250,000 | Senior/Staff Engineer |
| Quant/HFT | ₹50–1.5 Cr | $200,000–$500,000+ | Quantitative Developer |
Yes, but you'll need complementary skills. C++ alone gets you interviews, but companies also expect: data structures & algorithms (essential for all C++ roles), one domain specialization (game dev, embedded, finance), familiarity with build tools (CMake, Make), and version control (Git). For embedded roles, add hardware knowledge. For game dev, add linear algebra and a game engine. For finance, add math/statistics.
Top employers by category:
Timeline:
Total: 9–12 months of consistent daily practice (2–3 hours/day) to be job-ready for entry-level C++ positions.
Yes, significantly. C++ has manual memory management, pointers, templates, multiple inheritance, undefined behavior, and a complex compilation model. Python abstracts all of this away. However, learning C++ first makes you a stronger programmer overall because you understand what's happening "under the hood." Developers who know C++ find Python trivially easy to learn later — the reverse is much harder.
Core interview topics:
No. Entry-level positions expect you to know: basic syntax, OOP, pointers, STL basics, and solve medium-level DSA problems in C++. You don't need to understand template metaprogramming, SFINAE, or write lock-free code for your first job. That comes with experience. Focus on: clean code, correct memory management, and problem-solving ability.
| Language | Avg Salary (US) | Avg Salary (India) | Job Availability |
|---|---|---|---|
| C++ | $135,000 | ₹18 LPA | Moderate (niche) |
| Python | $120,000 | ₹12 LPA | Very High |
| Java | $115,000 | ₹14 LPA | High |
| Rust | $140,000 | ₹20 LPA | Low (growing) |
| Go | $130,000 | ₹16 LPA | Moderate |
C++ pays more per role because the jobs are harder and candidates are fewer. HFT (high-frequency trading) C++ roles are the highest-paid programming jobs in the world.
Top 10 skills from job postings (2025–2026):
Yes, but it's harder. C++ is not the friendliest first language — Python or JavaScript are easier starting points. However, many CS programs teach C++ as the first language (IITs, NITs use C/C++ in first year). If you're disciplined and willing to struggle with pointers and memory for the first 2 months, you can absolutely learn C++ from scratch. Use the examples on this page to practice — they start from Hello World and build up gradually.
Growing in specialized domains. While web development moved to JavaScript/Python, C++ demand is increasing in: electric vehicles (autonomous driving), AI inference (TensorRT, ONNX Runtime are C++), gaming (Unreal Engine 5), HFT (latency-critical trading), and edge computing. The TIOBE Index shows C++ consistently in top 3. Companies are hiring more C++ developers for performance-critical workloads as software becomes more latency-sensitive.
| Industry | Use Case | Example Companies |
|---|---|---|
| Gaming | Game engines, physics, rendering | Epic, EA, Ubisoft |
| Finance/HFT | Low-latency trading, risk engines | Citadel, Tower, DE Shaw |
| Automotive | Self-driving, ADAS, ECU firmware | Tesla, Waymo, Bosch |
| Big Tech | Browsers, databases, OS | Google, Microsoft, Meta |
| Aerospace | Flight systems, simulation | Boeing, Lockheed, ISRO |
| AI/ML Infrastructure | Inference engines, GPU kernels | NVIDIA, OpenAI, Google |
Honest assessment: C++ is one of the hardest mainstream languages to learn. The difficulty comes from: manual memory management (no garbage collector), undefined behavior (code that compiles but crashes), template syntax (notoriously ugly), and the language having 40+ years of features stacked on top of each other. However, you don't need to learn ALL of C++ — modern C++ (C++17/20) is cleaner, and you can be productive knowing ~30% of the language features. Focus on STL, smart pointers, RAII, and basic templates.
new but forgetting delete — use smart pointers insteadstd::vector is faster and safer-Wall -Werror)malloc/printf instead of new/cout, raw arrays instead of vectorYes — increasingly common post-2020. Game development, finance (quant roles), and Big Tech infrastructure teams offer remote C++ positions. Embedded roles are harder to do remotely (need hardware access), though some companies ship dev kits to remote workers. Remote C++ salaries are often 80–100% of on-site pay at major companies. Platforms like LinkedIn, Indeed, and Turing.com list remote C++ positions globally.
Sources: StackOverflow Developer Survey 2024, TIOBE Index, LinkedIn Job Postings 2024–2025, Glassdoor salary data. Practice C++ with the examples above — each runs directly in your browser.