├── Avoid_diamond_using_scope_resolution_operator.cpp ├── Avoid_diamond_using_virtual_key_word.cpp ├── Diamond.cpp ├── Diamond2.cpp ├── Dynamic_memory_allocate.cpp ├── LICENSE ├── README.md ├── car&motor.cpp ├── class1.cpp ├── class_func.cpp ├── class_in_to_class.cpp ├── complex.cpp ├── constructor2.cpp ├── constructor4.cpp ├── constructor_1.cpp ├── constructor_3.cpp ├── copy_constructor.cpp ├── data_conversion_1.cpp ├── data_conversion_2.cpp ├── data_conversion_3.cpp ├── encapsulation.cpp ├── exeption_handling_01.cpp ├── exeption_handling_02.cpp ├── exeption_handling_03.cpp ├── friend_function.cpp ├── global_variables.cpp ├── inheritance1.cpp ├── inheritance2.cpp ├── inheritance3.cpp ├── inheritance4.cpp ├── opertor_overloading.cpp ├── opertor_overloading_2.cpp ├── polumoprisam 3.cpp ├── polymoprisam 1.cpp ├── polymoprisam 2.cpp ├── polymoprisam 4.cpp ├── public class.cpp ├── static_variables.cpp ├── template_3.cpp ├── templates.cpp ├── templates_2.cpp ├── templates_4.cpp └── volume&area.cpp /Avoid_diamond_using_scope_resolution_operator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class A{ 4 | int a; 5 | public: 6 | 7 | void set(int x){ 8 | a=x; 9 | } 10 | void print(){ 11 | cout< 2 | using namespace std; 3 | class A{ 4 | int a; 5 | public: 6 | 7 | void set(int x){ 8 | a=x; 9 | } 10 | void print(){ 11 | cout< 2 | using namespace std; 3 | 4 | class person 5 | { 6 | protected: 7 | int age; 8 | public: 9 | person(int x):age(x) 10 | { 11 | cout<<"I am person "< 2 | using namespace std; 3 | class A 4 | { 5 | int x; 6 | public: 7 | void set(int i) 8 | { 9 | x=i; 10 | } 11 | void print(){ 12 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a=5; 7 | 8 | //Allocate dynamic memory 9 | int *ptr1 = new int; 10 | int *ptr2 = new int[5]; 11 | 12 | ptr1=&a; 13 | 14 | for(int i=0;i<5;i++){ 15 | *(ptr2+i)=i+1; 16 | } 17 | 18 | //How to print that value 19 | cout<<*ptr1< 2 | using namespace std; 3 | class engine{ 4 | int cylinders;//number of cylinders 5 | double capacity;//capacity in liters 6 | 7 | public: 8 | engine(int c,double l); 9 | 10 | }; 11 | 12 | engine :: engine (int c, double l):cylinders(c),capacity(l) 13 | { 14 | cout<<"constructor engine intilizing finished"< 2 | using namespace std; 3 | 4 | class outside{ 5 | public : 6 | int x=0; 7 | 8 | class inside { 9 | public : 10 | int x=5; 11 | 12 | static int y; 13 | int func(){ 14 | return x; 15 | } 16 | }; 17 | }; 18 | int outside :: inside :: y=10; 19 | int main() 20 | { 21 | outside A; 22 | outside :: inside B; 23 | cout<< B.func() << endl; 24 | cout << B.y << endl; 25 | 26 | } 27 | -------------------------------------------------------------------------------- /class_func.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int a=3; 5 | 6 | class X { 7 | int a; 8 | 9 | public : 10 | X(){ 11 | a=0; 12 | } 13 | int increment_a(){ 14 | return a++; 15 | } 16 | 17 | print(){ 18 | cout<<"X-a = "< 2 | using namespace std; 3 | 4 | class outside { 5 | public : 6 | int x=0; 7 | 8 | class inside { 9 | public: 10 | int x=10; 11 | int func(){ 12 | return x; 13 | } 14 | 15 | }; 16 | 17 | 18 | }; 19 | int main() 20 | { 21 | outside A; 22 | outside :: inside B; 23 | 24 | cout< 2 | using namespace std; 3 | class complex{ 4 | public: 5 | 6 | int real1; 7 | int noreal1; 8 | 9 | int real2; 10 | int noreal2; 11 | 12 | int sumreal(int r1,int r2) 13 | { 14 | real1=r1; 15 | real2=r2; 16 | return real1+real2; 17 | } 18 | 19 | int sumnoreal(int n1,int n2) 20 | { 21 | noreal1=n1; 22 | noreal2=n2; 23 | return noreal1+noreal2; 24 | } 25 | int differencereal(int r1,int r2) 26 | { 27 | real1=r1; 28 | real2=r2; 29 | return real1-real2; 30 | } 31 | int differencenoreal(int n1,int n2) 32 | { 33 | noreal1=n1; 34 | noreal2=n2; 35 | return noreal1-noreal2; 36 | } 37 | 38 | }; 39 | int main() 40 | { 41 | int i1=0,i2=0,j1=0,j2=0; 42 | complex c1; 43 | cout<<"Enter first complex num real part:"; 44 | cin>>i1; 45 | cout<<"Enter first complex num noreal part:"; 46 | cin>>j1; 47 | cout<<"Enter second complex num real part:"; 48 | cin>>i2; 49 | cout<<"Enter second complex num noreal part:"; 50 | cin>>j2; 51 | 52 | cout<<"result = "< 2 | using namespace std; 3 | 4 | class rectangle { 5 | 6 | int width; 7 | int height; 8 | public: 9 | rectangle() 10 | { 11 | width=0; 12 | height=0; 13 | } 14 | 15 | rectangle(int w) 16 | { 17 | width=w; 18 | height=2; 19 | } 20 | 21 | rectangle(int h,int w) 22 | { 23 | width=w; 24 | height=h; 25 | 26 | } 27 | 28 | ~rectangle() 29 | { 30 | cout<<"Destructor called"< 2 | using namespace std; 3 | 4 | class Hello{ 5 | public: 6 | Hello(){ 7 | cout<<"This is consrtuctor"< 2 | #include 3 | using namespace std; 4 | 5 | class list 6 | { 7 | int length; 8 | int *array; 9 | public: 10 | list() 11 | { 12 | length = 10; 13 | assert(array !=0); 14 | array=new int[length]; 15 | for(int i=0;i 2 | using namespace std; 3 | 4 | class Hello{ 5 | 6 | string name; 7 | public: 8 | 9 | Hello(); 10 | 11 | Hello(string); 12 | 13 | ~Hello(){ 14 | int i=1; 15 | cout<<"called destructor"< 2 | using namespace std; 3 | 4 | class time 5 | { 6 | int hour; 7 | int mins; 8 | public: 9 | time() 10 | { 11 | cout<<"defult constructor"<>T; 30 | time t(T); 31 | 32 | t.display(); 33 | } 34 | -------------------------------------------------------------------------------- /data_conversion_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class time 5 | { 6 | int hour; 7 | int min; 8 | public: 9 | time(int a,int b) 10 | { 11 | hour=a; 12 | min=b; 13 | } 14 | operator int() 15 | { 16 | return (hour*60+min); 17 | } 18 | 19 | }; 20 | 21 | int main() 22 | { 23 | time t(3,45); 24 | cout<<"Time in to minitues"< 2 | using namespace std; 3 | 4 | class time 5 | { 6 | public: 7 | int hour,min; 8 | 9 | time(int h,int m):hour(h),min(m){} 10 | 11 | int mins() 12 | { 13 | return ((hour*60)+min); 14 | } 15 | }; 16 | class minitues 17 | { 18 | int mins; 19 | public: 20 | minitues(){} 21 | public: 22 | operator=(time T) 23 | { 24 | mins=T.min; 25 | } 26 | void show() 27 | { 28 | cout<<"Total min:"< 2 | using namespace std; 3 | int main() 4 | { 5 | int x=0; 6 | try 7 | { 8 | if(x<=0){ 9 | 10 | cout<<"X throw to catch..."< 2 | using namespace std; 3 | class A{}; 4 | class B:public A{}; 5 | int main() 6 | { 7 | A b; 8 | try{ 9 | throw b; 10 | } 11 | catch (B a){ 12 | cout<<"q"< 2 | using namespace std; 3 | char ABC(char Y) 4 | { 5 | if('A'<=Y&&Y<='Z'){ 6 | return Y; 7 | } 8 | throw int(Y); 9 | } 10 | int main() 11 | { 12 | try 13 | { 14 | char ch='1'; 15 | cout< 2 | 3 | class MyClass { 4 | private: 5 | int privateVar; 6 | int privatevar2; 7 | 8 | public: 9 | MyClass() : privateVar(0) {} 10 | 11 | // Declaring a friend function 12 | friend void friendFunction(MyClass); 13 | 14 | // Declaring a friend class 15 | friend class FriendClass; 16 | }; 17 | 18 | // Defining the friend function 19 | void friendFunction(MyClass obj) { 20 | // Accessing private member of MyClass 21 | obj.privateVar = 10; 22 | obj.privatevar2=100; 23 | std::cout << "Friend function accessing privateVar: " << obj.privateVar <<" "< 2 | using namespace std; 3 | 4 | int a=100; 5 | 6 | void print() 7 | { 8 | int b=10,a=1; 9 | cout<<"value of print a: "<< a << endl; 10 | cout<<"Value of global a: " << :: a << endl; 11 | cout<<"Value of b: "<< b << endl; 12 | } 13 | int main() 14 | { 15 | int a=0; 16 | cout<<"Value of main a: "<< a << endl; 17 | 18 | print(); 19 | 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /inheritance1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class vehical{ 5 | 6 | public: 7 | vehical() 8 | { 9 | cout<<"First call vehical"< 2 | using namespace std; 3 | class Base 4 | { 5 | public : 6 | int x, y; 7 | public: 8 | Base(int i, int j) 9 | { 10 | x = i; 11 | y = j; 12 | } 13 | }; 14 | class Derived : public Base 15 | { 16 | public: 17 | Derived(int i, int j):Base(i,j) {} 18 | 19 | void print() 20 | { 21 | cout << x <<" "<< y; 22 | } 23 | }; 24 | int main(void) 25 | { 26 | Derived q(10, 10); 27 | q.print(); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /inheritance3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class dad{ 5 | public: 6 | dad() 7 | { 8 | cout<<"Dad of child1 and child 2"< 2 | using namespace std; 3 | class mammals 4 | { 5 | public: 6 | 7 | void function1() 8 | { 9 | cout<<"I am mammal"< 2 | using namespace std; 3 | class number 4 | { 5 | int num; 6 | public: 7 | number(int n):num(n){} 8 | 9 | void display() 10 | { 11 | cout<<"number:"<num=-num; 16 | } 17 | }; 18 | int main() 19 | { 20 | number a(10); 21 | -a; 22 | a.display(); 23 | } 24 | -------------------------------------------------------------------------------- /opertor_overloading_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class number { 4 | unsigned int count; 5 | public: 6 | number():count(0){} 7 | void operator++() 8 | { 9 | ++count; 10 | } 11 | 12 | void display() 13 | { 14 | cout<<"count:"< 2 | using namespace std; 3 | 4 | class Transaction { 5 | protected: 6 | int transaction_ID; 7 | string date; 8 | 9 | public: 10 | virtual void recordTransaction() = 0; 11 | virtual void displayInfo() = 0; 12 | }; 13 | 14 | class IncomeTransaction : public Transaction { 15 | protected: 16 | string source; 17 | int amount; 18 | 19 | public: 20 | void recordTransaction() override { 21 | cout << "Enter transaction id: "; 22 | cin >> transaction_ID; 23 | cout << "Enter date: "; 24 | cin >> date; 25 | cout << "Enter source: "; 26 | cin >> source; 27 | cout << "Enter amount: "; 28 | cin >> amount; 29 | } 30 | 31 | void displayInfo() override { 32 | cout << "Transaction id: " << transaction_ID << endl; 33 | cout << "Date: " << date << endl; 34 | cout << "Source: " << source << endl; 35 | cout << "Amount: " << amount << endl; 36 | } 37 | }; 38 | 39 | class ExpenseTransaction : public Transaction { 40 | protected: 41 | string reason; 42 | int amount; 43 | 44 | public: 45 | void recordTransaction() override { 46 | cout << "Enter transaction id: "; 47 | cin >> transaction_ID; 48 | cout << "Enter date: "; 49 | cin >> date; 50 | cout << "Enter reason: "; 51 | cin >> reason; 52 | cout << "Enter amount: "; 53 | cin >> amount; 54 | } 55 | 56 | void displayInfo() override { 57 | cout << "Transaction id: " << transaction_ID << endl; 58 | cout << "Date: " << date << endl; 59 | cout << "Reason: " << reason << endl; 60 | cout << "Amount: " << amount << endl; 61 | } 62 | }; 63 | 64 | int main() { 65 | IncomeTransaction income; 66 | ExpenseTransaction expense; 67 | 68 | cout << "Recording Income Transaction:" << endl; 69 | income.recordTransaction(); 70 | cout << "\nDisplaying Income Transaction:" << endl; 71 | income.displayInfo(); 72 | 73 | cout << "\nRecording Expense Transaction:" << endl; 74 | expense.recordTransaction(); 75 | cout << "\nDisplaying Expense Transaction:" << endl; 76 | expense.displayInfo(); 77 | 78 | return 0; 79 | } 80 | 81 | -------------------------------------------------------------------------------- /polymoprisam 1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Employee 5 | { 6 | public: 7 | int salary=0; 8 | virtual void calculate_salary() 9 | { 10 | salary=0; 11 | cout<<"Salary="< 2 | using namespace std; 3 | 4 | class person 5 | { 6 | protected: 7 | string name; 8 | int age; 9 | static int nextID; 10 | virtual void getdata() 11 | { 12 | cout<<"give me correct data"<>name; 28 | cout<<"Enter age:"; 29 | cin>>age; 30 | cout<<"Enter specialist:"; 31 | cin>>doctor_specialist; 32 | 33 | } 34 | void putdata() 35 | { 36 | cout<<"doctor name:"<>name; 51 | cout<<"Enter age:"; 52 | cin>>age; 53 | cout<<"Enter addmission date:"; 54 | cin>>addmission_date; 55 | } 56 | void putdata() 57 | { 58 | cout<<"patient name:"<< name<<" age:"<< age<<" patient_id:"< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Book { 8 | private: 9 | string title; 10 | string author; 11 | string ISBN; 12 | bool available; 13 | 14 | public: 15 | Book(const string& title, const string& author, const string& ISBN) 16 | : title(title), author(author), ISBN(ISBN), available(true) {} 17 | 18 | 19 | string getTitle() const { 20 | return title; 21 | } 22 | 23 | string getAuthor() const { 24 | return author; 25 | } 26 | 27 | string getISBN() const { 28 | return ISBN; 29 | } 30 | 31 | bool isAvailable() const { 32 | return available; 33 | } 34 | 35 | 36 | void setTitle(const string& title) { 37 | this->title = title; 38 | } 39 | 40 | void setAuthor(const string& author) { 41 | this->author = author; 42 | } 43 | 44 | void setISBN(const string& ISBN) { 45 | this->ISBN = ISBN; 46 | } 47 | 48 | 49 | void checkOut() { 50 | if (available) 51 | available = false; 52 | else 53 | cout << "This book is already checked out." << endl; 54 | } 55 | 56 | void returnBook() { 57 | if (!available) 58 | available = true; 59 | else 60 | cout << "This book is already available." << endl; 61 | } 62 | 63 | 64 | void displayInfo() const { 65 | cout << "Title: " << title << endl; 66 | cout << "Author: " << author << endl; 67 | cout << "ISBN: " << ISBN << endl; 68 | cout << "Availability: " << (available ? "Available" : "Checked out") << endl; 69 | } 70 | }; 71 | 72 | class Patron { 73 | private: 74 | string name; 75 | string libraryCardNumber; 76 | vector borrowedBooks; 77 | 78 | public: 79 | Patron(const string& name, const string& libraryCardNumber) 80 | : name(name), libraryCardNumber(libraryCardNumber) {} 81 | 82 | 83 | string getName() const { 84 | return name; 85 | } 86 | 87 | string getLibraryCardNumber() const { 88 | return libraryCardNumber; 89 | } 90 | 91 | 92 | void setName(const string& name) { 93 | this->name = name; 94 | } 95 | 96 | void setLibraryCardNumber(const string& libraryCardNumber) { 97 | this->libraryCardNumber = libraryCardNumber; 98 | } 99 | 100 | 101 | void borrowBook(Book* book) { 102 | if (book->isAvailable()) { 103 | borrowedBooks.push_back(book); 104 | book->checkOut(); 105 | cout << "Book '" << book->getTitle() << "' has been borrowed." << endl; 106 | } else { 107 | cout << "Book '" << book->getTitle() << "' is not available for borrowing." << endl; 108 | } 109 | } 110 | 111 | void returnBook(Book* book) { 112 | auto it = find(borrowedBooks.begin(), borrowedBooks.end(), book); 113 | if (it != borrowedBooks.end()) { 114 | borrowedBooks.erase(it); 115 | book->returnBook(); 116 | cout << "Book '" << book->getTitle() << "' has been returned." << endl; 117 | } else { 118 | cout << "You haven't borrowed this book." << endl; 119 | } 120 | } 121 | 122 | 123 | void displayInfo() const { 124 | cout << "Name: " << name << endl; 125 | cout << "Library Card Number: " << libraryCardNumber << endl; 126 | cout << "Borrowed Books:" << endl; 127 | if (borrowedBooks.empty()) { 128 | cout << "No books borrowed." << endl; 129 | } else { 130 | for (const auto& book : borrowedBooks) { 131 | cout << "- " << book->getTitle() << endl; 132 | } 133 | } 134 | } 135 | }; 136 | 137 | int main() { 138 | 139 | Book book1("The Great Gatsby", "F. Scott Fitzgerald", "9780743273565"); 140 | Book book2("To Kill a Mockingbird", "Harper Lee", "9780061120084"); 141 | Book book3("1984", "George Orwell", "9780451524935"); 142 | 143 | Patron patron("John Doe", "123456"); 144 | 145 | patron.borrowBook(&book1); 146 | patron.borrowBook(&book2); 147 | patron.borrowBook(&book3); 148 | 149 | cout << "\nPatron's Information:" << endl; 150 | patron.displayInfo(); 151 | 152 | patron.returnBook(&book1); 153 | 154 | cout << "\nUpdated Patron's Information:" << endl; 155 | patron.displayInfo(); 156 | 157 | return 0; 158 | } 159 | -------------------------------------------------------------------------------- /public class.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class car 4 | { 5 | 6 | string brand; 7 | string model; 8 | int year; 9 | }; 10 | int main() 11 | { 12 | car obj; 13 | obj.brand="toyata"; 14 | obj.model="wagon"; 15 | obj.year=2020; 16 | 17 | cout< 2 | using namespace std; 3 | 4 | void demo() 5 | { 6 | static int co = 0; 7 | int c = 0; 8 | cout << co << " " < 2 | using namespace std; 3 | template 4 | T func1(T y) 5 | { 6 | return y+x; 7 | } 8 | 9 | template 10 | T func2(T y) 11 | { 12 | return y*x; 13 | } 14 | 15 | int main() 16 | { 17 | cout<(20)<(2)< 2 | using namespace std; 3 | template 4 | T mymax(T x,T y) 5 | { 6 | return (x>y)?x:y; 7 | } 8 | int main() 9 | { 10 | 11 | cout<<"MAX="<(29,11)<