0% found this document useful (0 votes)
0 views4 pages

CPP_Syntax_Reference

The document provides a quick reference for C++ syntax covering dynamic memory allocation with 'new', using pointers for object access, templates for generic programming, operator overloading for input/output, method chaining with 'return *this', and operator overloads for arithmetic and comparison. It includes code examples and explanations for each concept. Key points include memory management, template usage, and the syntax for overloading operators.

Uploaded by

jahanzb963
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views4 pages

CPP_Syntax_Reference

The document provides a quick reference for C++ syntax covering dynamic memory allocation with 'new', using pointers for object access, templates for generic programming, operator overloading for input/output, method chaining with 'return *this', and operator overloads for arithmetic and comparison. It includes code examples and explanations for each concept. Key points include memory management, template usage, and the syntax for overloading operators.

Uploaded by

jahanzb963
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C++ Quick Syntax Reference

new | Pointer as new | Template | istream/ostream | return *this | Operator Overload

1. new (Dynamic Memory Allocation)


Heap se memory maangne ka operator, jo pointer return karta hai.
Header: <iostream> (no extra header needed, built-in operator)
// Single variable
int* p = new int;
*p = 10;

// With initialization
int* p = new int(10);

// Array allocation
int* arr = new int[5];

// Object allocation
ClassName* obj = new ClassName();

// Freeing memory
delete p;
delete[] arr;
delete obj;

2. Pointer as new (Object Pointers)


Class ka object heap par banana aur pointer se uske members access karna.
Header: <iostream>
class Student {
public:
int rollno;
void show(){ cout<<rollno; }
};

int main(){
Student* p = new Student();

// Access members using arrow operator


p->rollno = 5;
p->show();

// Same as:
(*p).rollno = 5;

delete p; // free memory


return 0;
}

Syntax Meaning Use

p->member Arrow operator Sabse common tareeka


(*p).member Dereference + dot Same kaam, kam use hota

delete p; Memory free karo new ke baad zaroori

3. Template (Function & Class)


Generic code likhna jo kisi bhi data type ke saath kaam kare.
Header: <iostream>
// Function Template
template <class T>
T add(T a, T b){
return a + b;
}

int main(){
cout<<add(3,4); // T = int
cout<<add(2.5,1.5); // T = float
}

// Class Template
template <class T>
class Box {
T value;
public:
Box(T v){ value = v; }
T getValue(){ return value; }
};

int main(){
Box<int> b1(10);
Box<string> b2("Hi");
cout<<[Link]();
}

Yaad rakho: Template parameter ka naam class ke naam se different rakho (jaise T), warna confusion hoti hai.

4. istream / ostream (Operator >> & << Overload)


cin/cout ko apni class ke objects ke sath directly use karne ke liye friend functions.
Header: <iostream>
class Student {
int rollno;
public:
friend istream& operator>>(istream& in, Student& obj);
friend ostream& operator<<(ostream& out, Student& obj);
};

istream& operator>>(istream& in, Student& obj){


in >> [Link]; // private member, friend hai isliye access ho gaya
return in;
}

ostream& operator<<(ostream& out, Student& obj){


out << [Link];
return out;
}

int main(){
Student s;
cin >> s; // calls operator>>
cout << s; // calls operator<<
}

Operator Return Type Purpose

>> istream& Input lena (cin ke sath)

<< ostream& Output dikhana (cout ke sath)

5. return *this (Method Chaining)


Current object ko khud return karna, taake ek line mein multiple function calls ho sakein.
Header: <iostream>
class Counter {
int value = 0;
public:
Counter& increment(){
value++;
return *this; // current object return
}
void show(){ cout<<value; }
};

int main(){
Counter c;
[Link]().increment().increment(); // chaining
[Link](); // output: 3
}

Yaad rakho: return type reference (&) hona chahiye, warna copy return hogi aur chaining kaam nahi karegi.

6. Operator Overload (+, -, ==)


Apni class ke objects ke liye built-in operators (+, -, ==) ka naya matlab define karna.
Header: <iostream>
class Complex {
public:
float real, imag;

Complex operator+(Complex c2){


Complex temp;
[Link] = real + [Link];
[Link] = imag + [Link];
return temp;
}

Complex operator-(Complex c2){


Complex temp;
[Link] = real - [Link];
[Link] = imag - [Link];
return temp;
}

bool operator==(Complex c2){


return (real==[Link] && imag==[Link]);
}
};

Operator Return Type Left Object Right Object

+ Complex real, imag (no name) [Link], [Link]

- Complex real, imag (no name) [Link], [Link]

== bool real, imag (no name) [Link], [Link]


Yaad rakho: Binary operator member function ke roop mein overload karo toh sirf ek parameter lagta hai — left wala
object khud 'this' ban jata hai.

You might also like