├── .gitignore ├── C++ ├── PrimitiveToClassTypeConversion.cpp ├── README.md ├── abstractClass.cpp ├── callByAddress.cpp ├── callByReference.cpp ├── callByValue.cpp ├── classTypeToAnotherClassType.cpp ├── classTypeToPrimitiveType.cpp ├── classesAndObjects.cpp ├── constructorAndDestructorInInheritance.cpp ├── constructors.cpp ├── defaultParamaters.cpp ├── destructor.cpp ├── exceptionHandling.cpp ├── fileHandling.cpp ├── fileHandling.txt ├── friendClassAndFunction.cpp ├── friendFunction.cpp ├── functionOverloading.cpp ├── functions.cpp ├── inheritance.cpp ├── initializerList.cpp ├── inlineFunction.cpp ├── methodOverridingAndHiding.cpp ├── namespaces.cpp ├── nestedClass.cpp ├── newAndDelete.cpp ├── operatorOverloading.cpp ├── operatorOverloadingUsingFriendFunction.cpp ├── seekp&seekg.cpp ├── shallowAndDeepCopy.cpp ├── staticLocalVariable.cpp ├── staticMemberVariableAndMemberFunction.cpp ├── structure.cpp ├── tellg&tellp.cpp ├── template.cpp ├── thisPointer.cpp ├── typesOfVariables.cpp ├── virtualDestructor.cpp └── virtualFunctions.cpp ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | -------------------------------------------------------------------------------- /C++/PrimitiveToClassTypeConversion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Point { 4 | private: 5 | int x, y; 6 | public: 7 | Point() { 8 | x = 0, y = 0; 9 | } 10 | Point(int k) { 11 | x = k, y = k; 12 | } 13 | 14 | void setData(int x, int y) { 15 | this->x = x; 16 | this->y = y; 17 | } 18 | 19 | void getData() { 20 | cout << "The coordinates of point are: (" << x << ", " << y << ")" << endl; 21 | 22 | } 23 | }; 24 | int main() { 25 | /* 26 | Primitive type to class type can be implemented using parametrized constructor 27 | */ 28 | Point p1; 29 | int point = 5; 30 | p1 = 5; // Paramertrized constructor gets called here 31 | p1.getData(); 32 | } 33 | -------------------------------------------------------------------------------- /C++/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jineshparakh/OOP/00bb4e48952bf7416a1e25918bcb3ade8b3e0db5/C++/README.md -------------------------------------------------------------------------------- /C++/abstractClass.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Abstract Class 5 | class Person { 6 | public: 7 | /* 8 | Virtual keyword is necessary because without this keyword, early binding of 9 | print() function occurs and hence base class function can be called using object 10 | pointers 11 | */ 12 | virtual void print() = 0; 13 | }; 14 | /* 15 | For the inherited class, either we can override the pure virtual function of base class 16 | or declare the function as pure virtual in child class also. 17 | 18 | One of the two has to be done for sure 19 | */ 20 | class Student: public Person { 21 | 22 | public: 23 | void print() { 24 | cout << "In Student Class\n"; 25 | } 26 | }; 27 | 28 | int main() { 29 | /* 30 | Pure Virtual Function 31 | --> A do nothing function is called Pure Virtual Function 32 | 33 | Abstract Class 34 | --> Any class which has one or more pure virtual functions is called Abstract class 35 | --> One cannot create an object of Abstract class (can't instanciate it) 36 | */ 37 | // Person p; // This is CE 38 | Student s; 39 | s.print(); 40 | 41 | } -------------------------------------------------------------------------------- /C++/callByAddress.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int findSum(int*, int*); 5 | int main() { 6 | 7 | /* 8 | When formal arguments are pointer variables it is call by address 9 | */ 10 | 11 | int a = 2, b = 3; 12 | int sum = findSum(&a, &b); // &a and &b are actual arguments for findSum method 13 | cout << "Sum of " << a << " and " << b << " is: " << sum << endl; 14 | } 15 | // x and y are formal arguments and pointer variables in this case. (Hence, call by address) 16 | int findSum(int *x, int *y) { 17 | return *x + *y; 18 | } -------------------------------------------------------------------------------- /C++/callByReference.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int findSum(int&, int&); 5 | int main() { 6 | 7 | /* 8 | When formal arguments are reference variables it is call by reference 9 | */ 10 | 11 | int a = 2, b = 3; 12 | int sum = findSum(a, b); // References of a and b are passed 13 | cout << "Sum of " << a << " and " << b << " is: " << sum << endl; 14 | } 15 | // x and y are formal arguments and reference variales in this case. (Hence, call by reference) 16 | int findSum(int &x, int &y) { // 17 | return x + y; 18 | } -------------------------------------------------------------------------------- /C++/callByValue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int findSum(int, int); 5 | int main() { 6 | 7 | /* 8 | When formal arguments are ordinary variables it is call by value 9 | */ 10 | 11 | int a = 2, b = 3; 12 | int sum = findSum(a, b); // a and b are actual arguments for findSum method 13 | cout << "Sum of " << a << " and " << b << " is: " << sum << endl; 14 | } 15 | // x and y are formal arguments and ordinary variables in this case. (Hence, call by value) 16 | int findSum(int x, int y) { 17 | return x + y; 18 | } -------------------------------------------------------------------------------- /C++/classTypeToAnotherClassType.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Point { 4 | private: 5 | int x, y; 6 | public: 7 | Point() { 8 | x = 0, y = 0; 9 | } 10 | void setData(int x, int y) { 11 | this->x = x; 12 | this->y = y; 13 | } 14 | 15 | void getData() { 16 | cout << "The coordinates of point are: (" << x << ", " << y << ")" << endl; 17 | 18 | } 19 | int getX() { 20 | return x; 21 | } 22 | int getY() { 23 | return y; 24 | } 25 | }; 26 | class Complex { 27 | private: 28 | int x, y; 29 | public: 30 | Complex() { 31 | x = 0, y = 0; 32 | } 33 | void setData(int x, int y) { 34 | this->x = x; 35 | this->y = y; 36 | } 37 | 38 | void getData() { 39 | cout << "The complex number is: (" << x << "+" << y << "i)" << endl; 40 | 41 | } 42 | Complex(Point p) { 43 | this->x = p.getX(); 44 | this->y = p.getY(); 45 | } 46 | }; 47 | int main() { 48 | /* 49 | Class type to another class type can be implemented using: 50 | 1. conversion through constructor 51 | 2. conversion through casting operator 52 | 53 | 54 | */ 55 | Point p1; 56 | p1.setData(3, 4); 57 | Complex c1; 58 | /* 59 | Constructor should be made in Complex class 60 | Casting operator should be written in Point clas 61 | */ 62 | c1 = p1; 63 | c1.getData(); 64 | } 65 | -------------------------------------------------------------------------------- /C++/classTypeToPrimitiveType.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Point { 4 | private: 5 | int x, y; 6 | public: 7 | Point() { 8 | x = 0, y = 0; 9 | } 10 | void setData(int x, int y) { 11 | this->x = x; 12 | this->y = y; 13 | } 14 | 15 | void getData() { 16 | cout << "The coordinates of point are: (" << x << ", " << y << ")" << endl; 17 | 18 | } 19 | // casting operator 20 | operator int() { 21 | return sqrt(x * x + y * y); 22 | } 23 | }; 24 | int main() { 25 | /* 26 | Class type to primitive type can be implemented using casting operator 27 | Syntax: 28 | operator type(){ 29 | ... 30 | return (type-data); 31 | } 32 | 33 | */ 34 | Point p1; 35 | p1.setData(3, 4); 36 | int point; 37 | point = p1; 38 | cout << point << endl; 39 | } 40 | -------------------------------------------------------------------------------- /C++/classesAndObjects.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Point { 5 | private: 6 | int x, y; // Called as instance member variables 7 | public: 8 | // instance member functions 9 | void setData(int x, int y) { 10 | this->x = x; 11 | this->y = y; 12 | } 13 | 14 | void getData(); 15 | 16 | int getX() { 17 | return x; 18 | } 19 | 20 | int getY() { 21 | return y; 22 | 23 | } 24 | 25 | // Manhattan distance between 2 points 26 | int manhattanDistance(Point p) { 27 | return abs(x - p.x) + abs(y - p.y); 28 | } 29 | }; 30 | /* 31 | Point:: --> This thing is called membership label (Class name and then scope resolution operator) 32 | */ 33 | void Point::getData() { 34 | cout << "The coordinates of point are: (" << x << ", " << y << ")" << endl; 35 | } 36 | int main() { 37 | /* 38 | Class is description of object (Basically blueprint) 39 | It is used to describe properties and behaviour of an object 40 | Object is an instance of a class 41 | 42 | 43 | Differences between classes and structures: 44 | 1. Access specifiers are public for structs by default and private for classes 45 | 2. Inheritance defaults to public for structs by default and private for classes 46 | 47 | Except these, you can do everything in struct and class in similar fashion 48 | 49 | Difference between defining a function inside the class and outside the class: 50 | --> Functions defined inside the class are inline by default but when they are 51 | defined outside the class, inline keyword has to be used explicitly to reuqest for 52 | inline functionality 53 | 54 | IMP: State of an object should only be changed by instance member functions. State here means values of member variables 55 | 56 | Different names for Instance member variables: Attributes, data members, fields, properties, etc 57 | Different names for Instance member functions: Methods, procedures, actions, operations, services, etc 58 | */ 59 | Point p1; 60 | p1.setData(2, 3); 61 | p1.getData(); 62 | 63 | Point p2; 64 | p2.setData(4, 5); 65 | p2.getData(); 66 | 67 | int distance = p1.manhattanDistance(p2); 68 | 69 | cout << "Manhattan distance between (" << p1.getX() << " ," << p1.getY() << ") and (" << p2.getX() << ", " << p2.getY() << ") is " << distance << endl; 70 | 71 | } -------------------------------------------------------------------------------- /C++/constructorAndDestructorInInheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class A { 5 | 6 | public: 7 | A() { 8 | cout << "In the default constructor of parent A\n"; 9 | } 10 | ~A() { 11 | cout << "In the destructor of parent A\n"; 12 | } 13 | }; 14 | class B: public A { 15 | 16 | public: 17 | B() { 18 | cout << "In the default constructor of parent B\n"; 19 | } 20 | ~B() { 21 | cout << "In the destructor of child B\n"; 22 | } 23 | }; 24 | class C { 25 | 26 | private: 27 | int x; 28 | 29 | public: 30 | C(int x) { 31 | cout << "Assigned the value of x in Class C. x = " << x << endl; 32 | this->x = x; 33 | } 34 | ~C() { 35 | cout << "In the destructor of parent C\n"; 36 | } 37 | }; 38 | 39 | class D: public C { 40 | 41 | private: 42 | int y; 43 | 44 | public: 45 | /* 46 | Why this weird syntax? 47 | --> We have created a paratemetrized constructor in out parent class C. 48 | Thus default constructor will not be created by the compiler 49 | --> We aim to initialize values of both x and y using the constructors. 50 | --> So we will have to pass the value to the constructor of the parent class with 51 | which we wish to initialize. 52 | --> Thus this syntax 53 | 54 | What is the exact process followed? 55 | 1. You create an object of class D, say (2,3) 56 | 2. The paramterized constructor of D gets called 57 | 3. Before executing the constructor of D, it calls the parametrized constructor of C i.e C(x) 58 | 4. One the value of x has been assigned, the value of y gets assigned 59 | 60 | IMP Point: 61 | --> Constructor(s) of the derived classes call the constructor(s) of the base class 62 | */ 63 | D(int x, int y): C(x) { 64 | cout << "Assigned the value of y in Class D. y = " << y << endl; 65 | this->y = y; 66 | } 67 | ~D() { 68 | cout << "In the destructor of child D\n"; 69 | } 70 | }; 71 | 72 | int main() { 73 | 74 | /* 75 | Prerequisites: 76 | --> If user does not create any constructor for any class, compiler by default creates a default 77 | and a copy constructor 78 | --> In any class destructor is needed to release the memory allocated to the resources. In a destructor, we can do some 79 | final tasks before an object is removed from the memory. 80 | 81 | Order of calling of constructors: From child to parent 82 | Order of execution of constructors: From parent to child 83 | Order of calling of destructors: From child to parent 84 | Order of execution of destructors: From child to parent 85 | */ 86 | 87 | B b; 88 | D d(2, 3); 89 | } -------------------------------------------------------------------------------- /C++/constructors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Point { 5 | 6 | private: 7 | 8 | int x, y; 9 | 10 | public: 11 | 12 | Point() { // Default Constructor 13 | x = 0, y = 0; 14 | } 15 | 16 | Point(int k) { // Parameterized Constructor 17 | x = k, y = k; 18 | } 19 | 20 | Point(int x, int y) { // Parameterized Constructor 21 | this->x = x; 22 | this->y = y; 23 | } 24 | 25 | Point(Point &p) { // Copy Constructor 26 | this->x = p.x; 27 | this->y = p.y; 28 | } 29 | }; 30 | 31 | int main() { 32 | /* 33 | What is a constructor? 34 | --> The work of constructor is to make an object an object (Tough to understand statement:) ) 35 | --> Instance Member function of class (Hence, can never be static) 36 | --> Name of constructor is same as class name 37 | --> Cannot use return keyword 38 | --> It is implicitely invoked when an object is created 39 | --> Used to solve the problem of intialization of member variables 40 | --> If any object is created, a constructor has to be invoked (No questions asked it has to be invoked) 41 | 42 | Some important points: 43 | --> By default when a programmer does not create any constructor by ownself, the compiler creates 44 | a default constructor and a copy constructor 45 | --> When a programmer creates a copy constructor by ownself, the compiler does not create any constructors 46 | --> When a programmer creates a default constructor by ownself, the compiler creates a copy constructor 47 | 48 | Some things wrt Copy Constructor: 49 | 50 | Don't you feel the syntax for copy constructor would be more intutive if it were: 51 | ClassName (Classname Object){ 52 | // Copy stuff 53 | } 54 | But why an additional & sign is needed before the object? 55 | 56 | So, if the syntax were without the & sign, Clasname Object would call the copy constructor recursively for 57 | copying the actual arguments 58 | Thus, a reference is passed 59 | 60 | */ 61 | // Different ways of using constructors to initialize an object 62 | 63 | Point p1; 64 | Point p2(1, 2); 65 | Point p3(1); 66 | /* 67 | The p4 declaration gives CE only when user defines a copy constructor by ownself. If no explicit copy 68 | constructor is present the below line is syntactically and logically correct 69 | */ 70 | // Point p4 = 1; 71 | Point p5 = p3; 72 | Point p6(p3); 73 | } 74 | -------------------------------------------------------------------------------- /C++/defaultParamaters.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Assigning default values starts from right side 5 | int add(int, int, int = 0); 6 | int main() { 7 | int a = 2, b = 3, c = 5; 8 | cout << "Addition of " << a << " and " << b << " is: " << add(a, b) << endl; 9 | cout << "Addition of " << a << ", " << b << " and " << c << " is: " << add(a, b, c) << endl; 10 | } 11 | int add(int x, int y, int z) { 12 | return x + y + z; 13 | } -------------------------------------------------------------------------------- /C++/destructor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Point { 5 | private: 6 | int x, y; 7 | public: 8 | Point() { 9 | x = 0, y = 0; 10 | cout << "Default constructor invoked\n"; 11 | } 12 | Point(int x, int y) { 13 | this->x = x; 14 | this->y = y; 15 | cout << "Parametrized constructor invoked\n"; 16 | } 17 | ~Point() { 18 | cout << "Destructor invoked\n"; 19 | } 20 | 21 | }; 22 | int main() { 23 | /* 24 | What is a destructor? 25 | --> It is an instance member function of a class hence can never be static 26 | --> Destructor name is same as class name preceded by ~(pronounced tilde) symbol 27 | --> No return type 28 | --> No arguments, hence no overloading is possible 29 | --> It is invoked implicitly when object is going to get destroyed 30 | 31 | Use of destructor? 32 | --> It should be defined to release resources allocated to an object 33 | 34 | Some important points: 35 | --> If a programmer does not explicitly create a destructor, compiler creates one. 36 | --> But if the programmer creates one explicitly, the compiler does not create one. 37 | 38 | 39 | */ 40 | Point p1; 41 | Point p2(2, 3); 42 | } -------------------------------------------------------------------------------- /C++/exceptionHandling.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int main() { 6 | /* 7 | --> Exception is any normal behaviour, runtime error 8 | --> Using Exception Handling, a programmer can respond and manage RTEs actively 9 | 10 | 11 | try, throw and catch 12 | --> Program statements that you want to monitor for exceptions are contained in a try block 13 | --> If any exception occurs within the try block, it is thrown using throw 14 | --> The exception is caught using catch and then processed 15 | 16 | IMP Points: 17 | 1. try and catch will always be together (Both of them have to exist else no one must exist) 18 | 2. throw can be present independently (It will not cause CE but will cause RTE) 19 | */ 20 | try { 21 | cout << "This will be printed\n"; 22 | // throw 3239; 23 | throw ("An error occured"); 24 | cout << "This will not be printed\n"; 25 | } 26 | catch (char const* s) { 27 | cout << s << endl; 28 | } 29 | catch (...) { // ... indicates all types of throw statements are allowed 30 | cout << "Non-string exception occured\n"; 31 | } 32 | cout << "This will again be printed\n"; 33 | // throw 20; // Causes RTE 34 | 35 | } -------------------------------------------------------------------------------- /C++/fileHandling.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | /* 7 | File opening modes 8 | --> ios:: in input/read 9 | --> ios:: out output/write 10 | --> ios:: app append 11 | --> ios:: ate update 12 | --> ios:: binary binary 13 | 14 | 15 | */ 16 | ofstream fout; // Output Stream 17 | fout.open("fileHandling.txt"); 18 | fout << "Hello World! (This was done using File Handling in C++)" << endl; 19 | fout << "Second line" << endl; 20 | fout.close(); 21 | 22 | ifstream fin; 23 | fin.open("fileHandling.txt"); 24 | string s; 25 | getline(fin, s); 26 | while (!fin.eof()) { 27 | cout << s << endl; 28 | getline(fin, s); 29 | } 30 | } -------------------------------------------------------------------------------- /C++/fileHandling.txt: -------------------------------------------------------------------------------- 1 | Hello World! (This was done using File Handling in C++) 2 | Second line 3 | -------------------------------------------------------------------------------- /C++/friendClassAndFunction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Predecleration of class B is necessary becuase it is being passed as an argument in function1 5 | class B; 6 | 7 | class A { 8 | 9 | private: 10 | int x = 2; 11 | 12 | public: 13 | /* 14 | If it is to be used as a friend to some other class, it should be defined after that class's definition 15 | and not int class A. 16 | */ 17 | void function1(B); 18 | /* 19 | It means that C (to be specific all member functions of C)can access all the private members of A 20 | */ 21 | friend class C; 22 | }; 23 | 24 | class B { 25 | 26 | private: 27 | int x = 0; 28 | /* 29 | This statement implies that function1 from class A is a friend of class B 30 | and can access private members of class B 31 | */ 32 | friend void A::function1(B); 33 | }; 34 | 35 | /* 36 | Definition of the member function from class A which is friend to class B 37 | */ 38 | void A::function1(B b) { 39 | cout << "In Function 1 of class A\n"; 40 | cout << "Accessing value of x(Private member of B) outside class B: b.x = " << b.x << endl; 41 | 42 | } 43 | 44 | class C { 45 | 46 | public: 47 | void showA(A a) { 48 | cout << "In showA function of class C\n"; 49 | cout << "Accessing value of x(Private member of A) outside class A: a.x = " << a.x << endl; 50 | } 51 | }; 52 | 53 | int main() { 54 | A a; 55 | B b; 56 | C c; 57 | 58 | a.function1(b); 59 | c.showA(a); 60 | 61 | } -------------------------------------------------------------------------------- /C++/friendFunction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Breadth; 5 | class Length { 6 | private: 7 | int l; 8 | public: 9 | friend int area(Length, Breadth); 10 | void setL(int l) { 11 | this->l = l; 12 | } 13 | int getL() { 14 | return l; 15 | } 16 | }; 17 | class Breadth { 18 | private: 19 | int b; 20 | friend int area(Length, Breadth); 21 | public: 22 | void setB(int b) { 23 | this->b = b; 24 | } 25 | int getB() { 26 | return b; 27 | } 28 | }; 29 | int area(Length l1, Breadth b1) { 30 | return l1.l * b1.b; 31 | } 32 | int main() { 33 | /* 34 | What is friend function? 35 | --> Friend function is not a member function of a class to which it is a friend 36 | --> It should be declared inside the class 37 | --> It must be defined outside the class of which it is a friend 38 | --> Since it is a friend to a class, it can access any of the members of that class 39 | to which it is a friend, but not directly 40 | --> It has no caller object 41 | --> It should not be defined with membership label 42 | --> It can be a friend to more than one class 43 | --> You can declare a friend function anywhere in the class, access specifiers do not matter since it is a friend 44 | 45 | */ 46 | Length l; 47 | Breadth b; 48 | l.setL(2); 49 | b.setB(3); 50 | cout << area(l, b) << endl; 51 | 52 | } -------------------------------------------------------------------------------- /C++/functionOverloading.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int sum(int, int); 5 | int sum(int, int, int); 6 | int main() { 7 | 8 | /* 9 | In C, we could not have more than one function having the same name 10 | In C++ we can do so using function overloading 11 | 12 | Function Overloading: 13 | --> More than one function with same name and difference in arguments (and they need to follow some rules obviously) 14 | --> Compile time polymorphism 15 | 16 | IMP: int sum(int,int); 17 | float sum(int,int); 18 | This will give error. Type or count of arguments has to be different 19 | 20 | One of the important tasks of compiler is to bind function call with the actual function definition. 21 | This is called Early Binding. 22 | 23 | Steps: 24 | 1. When the compiler reaches a function call line, it checks the function name of the function call. 25 | 2. All the functions defined in the program with the same name are candidates for being binded. 26 | 3. Based on some rules it then checks which function to bind 27 | Rules: 28 | 1. The compiler tries to find exact match to function call (same number and type of arguments) 29 | 2. If no match is found, it tries to find match by promotion: 30 | char, unsigned char, short --> Promoted to int 31 | float --> Promoted to double 32 | 3. If, no match is found, it tries to find match via standard conversion 33 | 34 | If the function call does not fit in any of the rules, then an error occurs. 35 | */ 36 | 37 | int a = 2, b = 3, c = 5; 38 | cout << "Sum of " << a << " and " << b << " is: " << sum(a, b) << endl; // Follows rule 1 39 | cout << "Sum of " << a << ", " << b << " and " << c << " is: " << sum(a, b, c) << endl; // Follows rule 1 40 | 41 | cout << "Sum of 'a' & 'b' is:(Prints sum of ASCII) " << sum('a', 'b') << endl; // Follows rule 2 42 | cout << "Sum of 1.1 & 2.9 is:(Prints sum of floor values) " << sum(1.1, 2.9) << endl; // Follows rule 3 43 | } 44 | 45 | int sum(int x, int y) { 46 | return x + y; 47 | } 48 | int sum(int x, int y, int z) { 49 | return x + y + z; 50 | } -------------------------------------------------------------------------------- /C++/functions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Function Declaration 5 | void globalFunction(); // can be called anywhere in the program 6 | int main() { 7 | /* 8 | What is a function? 9 | --> Block of code performing a task (Helps in achieving modularization) 10 | --> Syntax: 11 | returnType name(arguements){ 12 | 13 | } 14 | --> Types: 15 | 1. Pre-defined(Declared in header files and defined in library files) 16 | 2. User-defined 17 | --> Benefits: 18 | 1. Easy to Read & Modify 19 | 2. Avoid duplication of code 20 | 3. Easy to debug codes 21 | 4. Better memory utilization (Only the functions in use are present in RAM) 22 | */ 23 | void localFunction(); // Can be only called in main method 24 | cout<<"Calling localFunction() from main: "; 25 | localFunction(); // Function call 26 | globalFunction(); 27 | } 28 | 29 | // Function Definitions 30 | void globalFunction() { 31 | cout << "In Global Function\n"; 32 | /* 33 | Un-commenting the line below gives the following error 34 | ‘localFunction’ was not declared in this scope; did you mean ‘globalFunction’? 35 | 36 | */ 37 | // localFunction(); 38 | } 39 | void localFunction() { 40 | cout << "In Local Function\n"; 41 | cout << "Calling globalFunction() from localFunction():\n"; 42 | globalFunction(); // A global function can be called from a local function but vice versa might not be possible 43 | } 44 | -------------------------------------------------------------------------------- /C++/inheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class A { 4 | private: 5 | int x; 6 | protected: 7 | void setX(int x) { 8 | this->x = x; 9 | } 10 | int getX() { 11 | return x; 12 | } 13 | }; 14 | class B: public A { 15 | public: 16 | void setData(int x) { 17 | setX(x); 18 | } 19 | int getData() { 20 | return getX(); 21 | } 22 | }; 23 | int main() { 24 | /* 25 | What is inheritance? 26 | --> It is a process of inheriting properties and behaviours of existing class(es) into new class(es) 27 | --> A more generalized class is always the parent and the specific one is always the child 28 | Example: 29 | Class Fruit will be a parent class 30 | Class Apple will be a child class 31 | 32 | Need of inheritance? 33 | --> Let us understand this via and example. Suppose you have a class Car. You also wish to have a class 34 | SportsCar which has all the properties of Car + some exclusive properties for it being a SportsCar 35 | --> What we can do for this? 36 | --> We can have 2 seperate classes, Car (With original properties) 37 | and SportsCar (With the new properties + properties of car). Issue in this is that we will have to write 38 | same code twice for Car and SportsCar for the common features 39 | --> We can have 2 seperate classes, Car (With original properties) and SportsCar (With the new properties) 40 | and then whenever we need to add a new SportsCar we make 2 objects for it. One for new features of a Sports 41 | Car and general features of a Car. Issue with this is that it does not comply with the idea of Encapsulation 42 | which states that for any entity all of it's variables and functions should be part of the same class 43 | 44 | So to solve these problems, the concept of inheritance comes into picture. 45 | We can reuse the properties of a previously declared class and use them in our new class with some 46 | additional functionalities 47 | 48 | Syntax: 49 | class BaseClassName{ 50 | 51 | }; 52 | 53 | class DerivedClassName : VisibilityMode/AccessSpecifier BaseClassName{ 54 | 55 | }; 56 | 57 | Types of Inheritance: 58 | 1. Single Inheritance 59 | --> 1 parent class and 1 child class 60 | Example: 61 | Class A{ 62 | 63 | }; 64 | class B:public A{ 65 | 66 | }; 67 | 2. Multilevel Inheritance 68 | --> More than one level of single inheritance 69 | Example: 70 | Class A{ 71 | 72 | }; 73 | Class B: public A{ 74 | 75 | }; 76 | class C: public B{ 77 | 78 | }; 79 | 3. Multiple Inheritance 80 | --> More than one parent class 81 | Example: 82 | Class A{ 83 | 84 | }; 85 | Class B{ 86 | 87 | }; 88 | Class C: public A, public B{ 89 | 90 | }; 91 | 4. Hierarchical Inheritance 92 | --> One parent having more than one children 93 | Example: 94 | Class A{ 95 | 96 | }; 97 | Class B: public A{ 98 | 99 | }; 100 | Class C: public A{ 101 | 102 | }; 103 | 5. Hybrid Inheritance 104 | --> Mixture of more than one type of inheritance from 1 to 4 105 | 106 | Visibility Modes 107 | --> Private 108 | --> Protected 109 | --> Public 110 | 111 | Types of users for a class 112 | --> Users who create an object of the class (say u1) 113 | --> Users who use derived class to access members of base class (say u2) 114 | 115 | Difference between availability & accessibility? 116 | --> During inheritance everything is inherited may it be Private or Protected or Public. 117 | They are avaiable in the derived class and availalibility does not depend on the visibility mode 118 | of inheritance. 119 | When we say they are available, it does not mean that everyone can access it. They might not 120 | be accessible depending on the visibility mode 121 | 122 | Access specifier in base class | Access specifier when inherited publicly 123 | Public | Public 124 | Protected | Protected 125 | Private | Inaccessible 126 | 127 | 128 | Access specifier in base class | Access specifier when inherited protectedly 129 | Public | Protected 130 | Protected | Protected 131 | Private | Inaccessible 132 | 133 | 134 | Access specifier in base class | Access specifier when inherited privately 135 | Public | Private 136 | Protected | Private 137 | Private | Inaccessible 138 | */ 139 | 140 | B b; 141 | b.setData(4); 142 | cout << b.getData() << endl; 143 | } -------------------------------------------------------------------------------- /C++/initializerList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Point { 5 | private: 6 | int x, y; 7 | public: 8 | Point(): x(0), y(0) { 9 | 10 | } 11 | }; 12 | /* 13 | --> non-static const or reference data members cannot be initialized in a constructor 14 | --> They need to be initialized using initializer list 15 | or initialization during declaration (case of variable y) 16 | */ 17 | class DemoClass { 18 | private: 19 | const int x; 20 | const int y = 5; 21 | int &z; 22 | public: 23 | DemoClass(int &n): x(2), z(n) { 24 | 25 | } 26 | }; 27 | 28 | int main() { 29 | /* 30 | --> Initializer list is used to initialize data members of the class 31 | --> The list of members to be initiated is indicated with a constructor as a comma separated list 32 | followed by a colon (:) 33 | */ 34 | int num = 2; 35 | DemoClass d(num); 36 | } -------------------------------------------------------------------------------- /C++/inlineFunction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | inline int square(int x); 5 | int main() { 6 | /* 7 | What is the need of inline function? 8 | --> Every time a function is called, it takes some time in context switching by saving previous states, 9 | jumping to next state and saving things into registers and pushing them onto a stack. 10 | --> So for small functions, the memory utlization is less when compared to the extra time trade off 11 | To eliminate these drawbacks, inline functions are created. 12 | 13 | Inline Function: 14 | --> It is expanded in line when the function is called 15 | --> Compiler replaces the function call with the function code present in the inline function 16 | 17 | IMP: Inline is a request not a command 18 | --> Compiler gets to decide based on memory usage whether to accept a function as inline or treat it as 19 | a normal function 20 | --> If function has loops, switch, goto, recursion, static variables, etc compiler does not accept it as inline. 21 | */ 22 | int a = 5; 23 | int sq = square(a); 24 | cout << "Square of " << a << " is: " << sq << endl; 25 | } 26 | int square(int x) { 27 | return x * x; 28 | } -------------------------------------------------------------------------------- /C++/methodOverridingAndHiding.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class A { 5 | public: 6 | void print() { 7 | cout << "In print function of class A\n"; 8 | } 9 | void showData() { 10 | cout << "In showData function of class A\n"; 11 | } 12 | }; 13 | 14 | class B: public A { 15 | public: 16 | void print() { // method overriding 17 | cout << "In print function of class B\n"; 18 | } 19 | void showData(int x) { // method hiding 20 | cout << "In showData function of class B\n"; 21 | } 22 | }; 23 | 24 | int main() { 25 | /* 26 | Method overriding: 27 | --> When you want the same functionalities in your child class but with different functionalities 28 | --> The entire function prototype should be same both in child and parent (No difference allowed) 29 | 30 | Method hiding: 31 | --> A function with same name but different arguments in parent and child class 32 | 33 | */ 34 | B b; 35 | /* 36 | What happens when you do b.print()? 37 | --> When this line is written, during compilation it is the duty of the compiler to bind the call 38 | to the declaration (Early binding) 39 | --> So the compiler searches through the member functions of the caller object and checks for 40 | functions named print(). 41 | --> If it finds a function in class B, it stops the search process and binds the call to that function 42 | --> If it does not find, the compiler continues to search it in the parent class and so on 43 | */ 44 | b.print(); 45 | b.A::print(); 46 | 47 | /* 48 | Why does the line below cause a CE? 49 | --> During compilation, the compiler starts the process of searching and binding from the 50 | caller class (B) 51 | --> So in this class it has a function with the name showData but with different arguments. 52 | --> If the name matches, the compiler stops searching and that point and the CE is caused 53 | due to mismatch of arguments. 54 | */ 55 | // b.showData(); Gives CE 56 | b.showData(1); 57 | b.A::showData(); 58 | } 59 | 60 | -------------------------------------------------------------------------------- /C++/namespaces.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | namespace Jinesh { 4 | int x; 5 | void fun(); 6 | class Jinesh { 7 | public: 8 | void jinesh(); 9 | }; 10 | } 11 | void Jinesh::fun() { 12 | cout << "Accessing fun() function from Jinesh Namespace\n"; 13 | } 14 | void Jinesh::Jinesh::jinesh() { 15 | cout << "Accessing jinesh() function from Jinesh class inside Jinesh namespace\n"; 16 | } 17 | int main() { 18 | /* 19 | namespace 20 | --> It is a container for identifiers 21 | --> It puts the names of it's members in a distinct space so that they don't conflict 22 | with the names in other global namespace 23 | 24 | How to create namespace? 25 | Syntax: 26 | namespace namespaceName{ 27 | //Declarations 28 | } 29 | IMP Points: 30 | --> The namespace definition must be done at global scope, or nested inside other namespace 31 | --> You can use an alias name for a namespace 32 | namespace AliasName=namespaceName 33 | --> It is not a class so object cannot be created 34 | --> There can be unnamed namespaces 35 | Syntax: 36 | namespace{ 37 | // Declarations 38 | } 39 | --> A namespace definiton can be extended or continued to multiple files, they are not 40 | overwritten or redefined 41 | 42 | using directive: 43 | --> using keyword allows a programmer to import an entire namespace into the program 44 | with a global scope 45 | --> It can be used to import namespaces into another namespaces or programs 46 | 47 | 48 | */ 49 | Jinesh::x = 2; 50 | Jinesh::fun(); 51 | Jinesh::Jinesh j; 52 | j.jinesh(); 53 | } -------------------------------------------------------------------------------- /C++/nestedClass.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Student { 5 | int rollNo; 6 | string name; 7 | class Address { 8 | int houseNo; 9 | string apartment; 10 | int pincode; 11 | string state; 12 | public: 13 | void setAddress(int houseNo, string apartment, int pincode, string state) { 14 | this->houseNo = houseNo; 15 | this->apartment = apartment; 16 | this->pincode = pincode; 17 | this->state = state; 18 | } 19 | void getAddress() { 20 | cout << "House No: " << houseNo << endl; 21 | cout << "Apartment: " << apartment << endl; 22 | cout << "Pincode: " << pincode << endl; 23 | cout << "State: " << state << endl; 24 | } 25 | }; 26 | Address address; 27 | public: 28 | void setData(int rollNo, string name, int houseNo, string apartment, int pincode, string state) { 29 | this->rollNo = rollNo; 30 | this->name = name; 31 | address.setAddress(houseNo, apartment, pincode, state); 32 | } 33 | void getData() { 34 | cout << "Student name: " << name << endl; 35 | cout << "RollNo: " << rollNo << endl; 36 | cout << "Address: "; 37 | address.getAddress(); 38 | } 39 | }; 40 | 41 | int main() { 42 | /* 43 | --> A class inside a class is called nested class. 44 | --> A nested class is a member and as such has the same access rights as other members 45 | --> 46 | */ 47 | Student s1; 48 | s1.setData(1, "Jinesh", 22, "HouseApartments", 113111, "Maharashtra"); 49 | s1.getData(); 50 | } -------------------------------------------------------------------------------- /C++/newAndDelete.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | /* 6 | SMA: Static Memory Allocation 7 | DMA: Dynamic Memory Allocation (Implemented using new and delete in C++) 8 | 9 | In SMA, the amount of memory allocated to the declaration statements(int x, float y, etc) is decided 10 | during Compile time only and cannot be changed. However, the memoery gets allocated when the program 11 | comes in RAM 12 | 13 | */ 14 | int *x = NULL; 15 | // Requesting memory for a new variable. nothrow signifies that if memory does 16 | // not get assigned it does not throw an error 17 | x = new(nothrow) int; 18 | if (!x) { 19 | cout << "Could not allocate memory\n"; 20 | } 21 | else { 22 | *x = 22; 23 | cout << "Value of x is: " << *x << endl; 24 | } 25 | 26 | float *y = new(nothrow) float(11.111); 27 | if (!y) { 28 | cout << "Could not allocate memory\n"; 29 | 30 | } 31 | else { 32 | cout << "Value of y is: " << *y << endl; 33 | } 34 | 35 | // DMA for Arrays 36 | int n = 10; 37 | int *a = new int[10]; 38 | for (int i = 0; i < n; i++) { 39 | a[i] = i + 1; 40 | } 41 | cout << "Array elements are: "; 42 | for (int i = 0; i < n; i++) { 43 | cout << a[i] << " "; 44 | } 45 | cout << endl; 46 | 47 | // Deallocating memory for variables 48 | delete x; 49 | delete y; 50 | 51 | // Deallocating memory for array 52 | delete []a; 53 | 54 | } -------------------------------------------------------------------------------- /C++/operatorOverloading.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Complex { 5 | private: 6 | int x, y; 7 | public: 8 | Complex() { 9 | x = 0, y = 0; 10 | } 11 | Complex(int x, int y) { 12 | this->x = x, this->y = y; 13 | } 14 | void getData() { 15 | if (y >= 0) 16 | cout << "(" << x << "+" << y << "i)\n"; 17 | else 18 | cout << "(" << x << "" << y << "i)\n"; 19 | } 20 | void setData(int x, int y) { 21 | this->x = x, this->y = y; 22 | } 23 | Complex operator +(Complex &c) { 24 | Complex temp; 25 | temp.x = x + c.x; 26 | temp.y = y + c.y; 27 | return temp; 28 | } 29 | // Overloading unary operators 30 | Complex operator -() { 31 | Complex temp; 32 | temp.x = -x; 33 | temp.y = -y; 34 | return temp; 35 | } 36 | // Overloading pre-increment operator 37 | Complex operator ++() { 38 | Complex temp; 39 | temp.x = ++this->x; 40 | temp.y = ++this->y; 41 | return temp; 42 | } 43 | /* 44 | Overloading post-increment operator 45 | Why int is present as an argument? 46 | --> This is done to help the compiler differentiate between pre and post increment operators after 47 | being overloaded 48 | --> Only 'int' should be written. Nothing else is acceptable (Else, this error occurs: no ‘operator++(int)’ declared for postfix ‘++’); 49 | */ 50 | Complex operator ++(int) { 51 | Complex temp; 52 | temp.x = this->x++; 53 | temp.y = this->y++; 54 | return temp; 55 | } 56 | /* 57 | Trying to overload << & >> operators without friend function 58 | Could not find a good way yet, it you get it do let me know!! 59 | More detailed explaination will be present in operatorOverloadingUsingFriendFunction.cpp 60 | */ 61 | istream& operator>>(istream& din) { 62 | cout << "Enter real part: "; 63 | din >> this->x; 64 | cout << "Enter imaginary part: "; 65 | din >> this->y; 66 | return din; 67 | } 68 | ostream& operator<<(ostream& dout) { 69 | if (y >= 0) 70 | dout << "(" << x << "+" << y << "i)\n"; 71 | else 72 | dout << "(" << x << "" << y << "i)\n"; 73 | 74 | return dout; 75 | } 76 | 77 | }; 78 | int main() { 79 | /* 80 | Operator Overloading: 81 | --> When an operator is overloaded with multiple jobs, it is known as operator overloading 82 | --> Way to implement compile time polymorphism 83 | You cannot overload: 84 | . (dot) 85 | :: 86 | ?: 87 | sizeof 88 | 89 | */ 90 | Complex c1(2, 3), c2(3, 4), c3, c4, c5, c6, c7, c8; 91 | 92 | cout << "First complex number is: "; c1.getData(); 93 | cout << "Second complex number is: "; c2.getData(); 94 | // c1 will be the called object. While overloading binary operator, left object will be the caller object 95 | c3 = c1 + c2; 96 | cout << "Addition of first and second complex numbers is: "; c3.getData(); 97 | c4 = c1.operator + (c2); 98 | cout << "Addition of first and second complex numbers(Different syntax) is: "; c4.getData(); 99 | c5 = -c1; 100 | cout << "Negative of first complex number is: "; c5.getData(); 101 | c6 = c1.operator - (); 102 | cout << "Negative of first complex number(Different Syntax) is: "; c6.getData(); 103 | c7 = c1++; 104 | cout << "Result after Post increment operator overloading on first complex number is: "; c7.getData(); 105 | cout << "After post incrememnt first complex number becomes: "; c1.getData(); 106 | c8 = ++c2; 107 | cout << "Result after Pre increment operator overloading on second complex number is: "; c8.getData(); 108 | cout << "After pre incrememnt second complex number becomes: "; c2.getData(); 109 | 110 | /* 111 | Weird syntaxes after overloading << & >> without friend function :) 112 | You can uncomment and check if you want! 113 | */ 114 | 115 | // Complex c9; 116 | // c9 >> cin; 117 | // c9 << cout; 118 | } -------------------------------------------------------------------------------- /C++/operatorOverloadingUsingFriendFunction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Complex { 5 | private: 6 | int x, y; 7 | public: 8 | Complex() { 9 | x = 0, y = 0; 10 | } 11 | 12 | Complex(int x, int y) { 13 | this->x = x, this->y = y; 14 | } 15 | 16 | void getData() { 17 | if (y >= 0) 18 | cout << "(" << x << "+" << y << "i)\n"; 19 | else 20 | cout << "(" << x << "" << y << "i)\n"; 21 | } 22 | 23 | void setData(int x, int y) { 24 | this->x = x, this->y = y; 25 | } 26 | 27 | friend Complex operator +(Complex&, Complex&); 28 | 29 | // Overloading unary operators 30 | friend Complex operator -(Complex&); 31 | 32 | // Overloading pre-increment operator 33 | friend Complex operator ++(Complex&); 34 | 35 | /* 36 | Overloading post-increment operator 37 | Why int is present as an argument? 38 | --> This is done to help the compiler differentiate between pre and post increment operators after 39 | being overloaded 40 | --> Only 'int' should be written. Nothing else is acceptable (Else, this error occurs: no ‘operator++(int)’ declared for postfix ‘++’); 41 | */ 42 | friend Complex operator ++(Complex&, int); 43 | 44 | /* 45 | Let us understand use and need of each keyword? 46 | --> friend is there to make the overloaded operator functions as a friend to Complex class 47 | --> istream& and ostream& are present as return types to facilitate multiple cin's and cout's together 48 | Example: cin>>c1>>c2>>c3; or cout< Why the reference(&) is mandatory? 50 | --> We cannot directly create objects of istream and ostream class because they are declared as 51 | protected in the base classes 52 | --> Thus a reference is passed do that concatenation of multiple cin's or cout's is possible 53 | --> Why Complex& in istream and Complex in ostream? 54 | --> Beacuse we need to alter or manipulate the actual values while taking the input 55 | and not during the output 56 | --> Both can be of type Complex& . There is not problem in that :) 57 | */ 58 | friend istream& operator >>(istream&, Complex&); 59 | 60 | friend ostream& operator <<(ostream&, Complex); 61 | 62 | 63 | }; 64 | 65 | Complex operator +(Complex &c1, Complex &c2) { 66 | Complex temp; 67 | temp.x = c1.x + c2.x; 68 | temp.y = c1.y + c2.y; 69 | return temp; 70 | } 71 | 72 | Complex operator -(Complex &c) { 73 | Complex temp; 74 | temp.x = -c.x; 75 | temp.y = -c.y; 76 | return temp; 77 | } 78 | 79 | Complex operator ++(Complex &c) { 80 | Complex temp; 81 | temp.x = ++c.x; 82 | temp.y = ++c.y; 83 | return temp; 84 | } 85 | 86 | Complex operator ++(Complex &c, int) { 87 | Complex temp; 88 | temp.x = c.x++; 89 | temp.y = c.y++; 90 | return temp; 91 | } 92 | 93 | istream& operator >>(istream& din, Complex &c) { 94 | cout << "Taking complex number as input....\n"; 95 | cout << "Enter real part: "; 96 | din >> c.x; 97 | cout << "Enter imaginary part: "; 98 | din >> c.y; 99 | return din; 100 | } 101 | 102 | ostream& operator <<(ostream& dout, Complex c) { 103 | if (c.y >= 0) 104 | dout << "(" << c.x << "+" << c.y << "i)\n"; 105 | else 106 | dout << "(" << c.x << "" << c.y << "i)\n"; 107 | return dout; 108 | } 109 | 110 | int main() { 111 | 112 | Complex c1(2, 3), c2(3, 4), c3, c4, c5, c6, c7, c8; 113 | 114 | cout << "First complex number is: "; c1.getData(); 115 | cout << "Second complex number is: "; c2.getData(); 116 | // c1 will be the called object. While overloading binary operator, left object will be the caller object 117 | c3 = c1 + c2; 118 | cout << "Addition of first and second complex numbers is: "; c3.getData(); 119 | c4 = operator + (c1, c2); 120 | cout << "Addition of first and second complex numbers(Different syntax) is: "; c4.getData(); 121 | c5 = -c1; 122 | cout << "Negative of first complex number is: "; c5.getData(); 123 | c6 = operator - (c1); 124 | cout << "Negative of first complex number(Different Syntax) is: "; c6.getData(); 125 | c7 = c1++; 126 | cout << "Result after Post increment operator overloading on first complex number is: "; c7.getData(); 127 | cout << "After post incrememnt first complex number becomes: "; c1.getData(); 128 | c8 = ++c2; 129 | cout << "Result after Pre increment operator overloading on second complex number is: "; c8.getData(); 130 | cout << "After pre incrememnt second complex number becomes: "; c2.getData(); 131 | 132 | 133 | Complex c9; 134 | cin >> c9; 135 | cout << "The complex number taken as input is: " << c9; 136 | } -------------------------------------------------------------------------------- /C++/seekp&seekg.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | /* 7 | seekg() (get pointer) 8 | --> Defined in istream class 9 | --> Prototype: 10 | - istream& seekg(streampos pos); 11 | - isteeam& seekg(streamoff off, ios_base::seekdir way); 12 | 13 | --> pos is the new absolute position within the stream (relative to the beginning) 14 | --> off is the offset value, relative to the way parameter 15 | --> way values (one of the 3) --> ios_base::beg, ios_base::cur and ios_base::end 16 | 17 | seekp() (put pointer) 18 | --> Defined in ostream class 19 | --> Prototype: 20 | - ostream& seekg(streampos pos); 21 | - ostream& seekg(streamoff off, ios_base::seekdir way); 22 | 23 | --> pos is the new absolute position within the stream (relative to the beginning) 24 | --> off is the offset value, relative to the way parameter 25 | --> way values (one of the 3) --> ios_base::beg, ios_base::cur and ios_base::end 26 | */ 27 | 28 | cout << "For seekg()..\n\n"; 29 | ifstream fin; 30 | fin.open("fileHandling.txt"); 31 | if (fin.tellg() == -1) { 32 | cout << "Could not open the file\n"; 33 | exit(0); 34 | } 35 | cout << "File Opened Successfully\n"; 36 | cout << "Tellg position: " << fin.tellg() << endl; 37 | cout << "First char from the file: " << (char)fin.get() << endl; 38 | cout << "Second char from the file: " << (char)fin.get() << endl; 39 | cout << "Tellg position: " << fin.tellg() << endl; 40 | cout << "Making tellg again point to start of the file..\n"; 41 | fin.seekg(2); 42 | cout << "Tellg position: " << fin.tellg() << endl; 43 | cout << "Third char from the file: " << (char)fin.get() << endl; 44 | cout << "Making tellg move 7 positions ahead from current..\n"; 45 | fin.seekg(7, ios_base::cur); 46 | cout << "Tellg position: " << fin.tellg() << endl; 47 | cout << "Char from the file: " << (char)fin.get() << endl; 48 | fin.close(); 49 | 50 | cout << "For seekp()\n\n"; 51 | ofstream fout; 52 | fout.open("fileHandling.txt", ios::ate | ios::app); 53 | cout << "Tellp position: " << fout.tellp() << endl; 54 | cout << "Making tellp point to 4 position to the right of beginning..\n"; 55 | fout.seekp(4, ios_base::beg); 56 | cout << "Tellp position: " << fout.tellp() << endl; 57 | 58 | } -------------------------------------------------------------------------------- /C++/shallowAndDeepCopy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class ShallowCopy { 5 | private: 6 | int x, y; 7 | public: 8 | ShallowCopy() { 9 | x = 0, y = 0; 10 | } 11 | void setData(int x, int y) { 12 | this->x = x, this->y = y; 13 | } 14 | void getData() { 15 | cout << "X: " << x << ", Y: " << y << endl; 16 | } 17 | ShallowCopy(ShallowCopy &c) { 18 | x = c.x, y = c.y; 19 | } 20 | }; 21 | class DeepCopy { 22 | private: 23 | int x, y; 24 | int* p; 25 | public: 26 | DeepCopy() { 27 | p = new int; 28 | } 29 | void setData(int x, int y, int z) { 30 | this->x = x; 31 | this->y = y; 32 | *p = z; 33 | } 34 | void getData() { 35 | cout << "X: " << x << ", Y: " << y << ", *P: " << *p << endl; 36 | 37 | } 38 | /* 39 | Why is deep copy required? 40 | --> In this class p is a pointer variable which will hold the address of an integer 41 | memory block 42 | --> So if we do normal copy as we do above, contents of p will be copied to new object. 43 | --> This implies that the address of that integer variable (stored in p) will be copied. 44 | --> So p from 2 different objects will have same *p 45 | --> Hence, we need to declare a new int at runtime and then do a deep copy 46 | */ 47 | DeepCopy(DeepCopy &c) { 48 | x = c.x, y = c.y; 49 | p = new int; 50 | *(p) = *(c.p); 51 | } 52 | /* 53 | Dangling pointer stuff: 54 | --> So if the lifetime of an object is over and we do not explicitely delete the existance 55 | of *p, it will be there in the memory with no access point. 56 | --> It can cause unnecessary crashes and RTE. 57 | --> So, deleting via a destructor is a good practice. 58 | */ 59 | ~DeepCopy() { 60 | delete p; 61 | } 62 | }; 63 | 64 | 65 | int main() { 66 | /* 67 | How to create copy of an object? 68 | --> Copy Constructor 69 | --> Implicit copy assignment operator 70 | 71 | Shallow Copy: 72 | --> Creating copy of an object by copying data of all member variables as it is is called 73 | shallow copy 74 | 75 | Deep Copy: 76 | --> Creating an object by copying data of another object along with the values of memory resources 77 | which resides outside the object but are handled by the object. 78 | */ 79 | ShallowCopy shallow1; 80 | shallow1.setData(2, 3); 81 | ShallowCopy shallow2; 82 | shallow2 = shallow1; 83 | shallow2.getData(); 84 | DeepCopy deep1; 85 | deep1.setData(4, 5, 6); 86 | DeepCopy deep2; 87 | deep2 = deep1; 88 | deep2.getData(); 89 | 90 | 91 | } -------------------------------------------------------------------------------- /C++/staticLocalVariable.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void foo() { 5 | static int x; 6 | x++; 7 | cout << x << endl; 8 | } 9 | int main() { 10 | /* 11 | Static local variables: 12 | --> Concept is taken from C 13 | --> By default initialized to 0 and not some garbage value 14 | --> Only one copy is present throughout the lifecycle of the program 15 | --> Can be used to implement co-routines in C/C++ 16 | */ 17 | for (int i = 0; i < 5; i++) { 18 | foo(); 19 | } 20 | 21 | } -------------------------------------------------------------------------------- /C++/staticMemberVariableAndMemberFunction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Account { 5 | int balance; 6 | static float rateOfInterest; // Static member variable or Class variable 7 | public: 8 | void setBalance(int balance) { 9 | this->balance = balance; 10 | } 11 | int getBalance() { 12 | return balance; 13 | } 14 | static float getRateOfInterest() { 15 | return rateOfInterest; 16 | } 17 | /* 18 | We cannot use this pointer (->) with static static member functions 19 | Example: 20 | this->rateOfInterest=rateOfInterest is not correct and causes CE 21 | */ 22 | static void setRateOfInterest(float r) { // Static member function or Class member function 23 | rateOfInterest = r; 24 | } 25 | }; 26 | /* 27 | The line below is extremely important for static member variables. 28 | Without this line an error will occur and we cannot access the value present in rateOfInterest 29 | Error if the line below is not present: undefined reference to `Account::rateOfInterest' 30 | 31 | We can also set some default value for the static variable. It is 0 by default 32 | */ 33 | float Account::rateOfInterest = 6.5f; 34 | int main() { 35 | /* 36 | Static Member Variables: 37 | --> Declared inside the class body 38 | --> Also known as class member variables 39 | --> Should be defined outside the class (MANDATORY) 40 | --> They do not belong to any object but belong to the entire class 41 | --> Only one copy of static member variable for the whole class throughout the runtime of the program 42 | --> They can also be used with classname 43 | --> Memory for static member variable only gets alloted after member definition and not after declaration 44 | 45 | Since static member variable is class variable, it can be accessed without an object 46 | If it is a public member it can directly be accessed using className::staticaVariableName 47 | But If it is private/ protected we need to have some functions to get and set the values 48 | 49 | But if the function is a member function, then it can only be accessed using objects. 50 | But we should be able to access static member variables without objects also. 51 | 52 | Thus, static member functions come into picture 53 | 54 | Static Member Functions: 55 | --> Can only access static member variables 56 | --> Can also be invoked without an object 57 | 58 | */ 59 | Account a1; 60 | a1.setBalance(200); 61 | cout << a1.getBalance() << endl; 62 | 63 | cout << Account::getRateOfInterest() << endl; 64 | } -------------------------------------------------------------------------------- /C++/structure.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Point { 5 | int x = 1; // Can initialize with some value in C++ but not in C 6 | int y = 1; 7 | } p1; 8 | 9 | struct Employee { 10 | 11 | private: 12 | int eID; 13 | string name; 14 | int age; 15 | 16 | public: 17 | // Structs can also have constructors 18 | Employee() { 19 | eID = 0; 20 | name = ""; 21 | age = 0; 22 | } 23 | void getInputData() { 24 | cout << "Enter Employee ID, Employee name & Employee age: "; 25 | /* 26 | No Need of '.' operator to access member variables. (It will actually give CE) 27 | This (->) pointer can be used but no need as such 28 | */ 29 | cin >> eID >> this->name >> age; 30 | } 31 | void printData(); // You can also define functions outside the struct 32 | }; 33 | 34 | void Employee::printData() { 35 | cout << "Hello " << name << ". Your Employee ID is " << eID << " and your age is " << age << endl; 36 | } 37 | int main() { 38 | /* 39 | What is structure? 40 | --> Collection of dissimilar elements 41 | --> Way to group variables 42 | --> Used to create user-defined Data Type 43 | --> Memory is only assigned when structure variable is declared and not when struct is defined 44 | 45 | IMP: Writing the keyword struct before declaring a new structure variable is optional in C++ 46 | but mandatory in C 47 | 48 | 49 | Some ways to create and initialize structure variable --> 50 | 51 | Example struct: 52 | struct myStruct(){ 53 | // member variables 54 | } type1; ** First Type ** 55 | 56 | myStrcut type2={initialize here}; ** Second Type ** 57 | myStrcut type3; ** Third Type ** 58 | type3={initialize here} 59 | myStrcut type4; 60 | type4.someMemberVariable=someValue (Use of '.' operator) ** Fourth Type ** 61 | 62 | myStruct type5; 63 | 64 | type5=type3 (Initialize with any other struct variable directly) ** Fifth Type ** 65 | 66 | */ 67 | 68 | Point p2 = {2, 3}; 69 | Point p3; 70 | p3 = {4, 5}; 71 | Point p4; 72 | p4.x = 6, p4.y = 7; 73 | 74 | /* 75 | In C, structs could not have member functions but they can have in C++. 76 | This is the beginning step to achieve encapsulation 77 | (See Struct Employee) 78 | 79 | IMP: 80 | 1. Struct can also have access specifiers public, private and protected 81 | (See Employee struct) 82 | For Difference between classes and structs see clasesAndObjects.cpp file 83 | */ 84 | 85 | Employee e1; 86 | e1.getInputData(); 87 | e1.printData(); 88 | 89 | Employee e2; 90 | e2.printData(); // Values set in constructor will get printed 91 | 92 | } -------------------------------------------------------------------------------- /C++/tellg&tellp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main() { 7 | /* 8 | 9 | tellg() 10 | --> Defined in istream class 11 | --> It's prototype is 12 | streampos tellg() 13 | --> Returns the position of the current character in the input stream 14 | */ 15 | 16 | ifstream fin; 17 | fin.open("fileHandling.txt"); 18 | int pos = fin.tellg(); 19 | cout << pos << endl; //0 20 | char ch; 21 | fin >> ch; 22 | pos = fin.tellg(); 23 | cout << pos << endl; //1 24 | fin >> ch; 25 | pos = fin.tellg(); 26 | cout << pos << endl; //2 27 | fin.close(); 28 | 29 | ofstream fout; 30 | fout.open("fileHandling.txt", ios::app); 31 | pos = fout.tellp(); 32 | cout << pos << endl; 33 | 34 | } -------------------------------------------------------------------------------- /C++/template.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Function template or Generic template 5 | template 6 | T maxx(T a, T b) { 7 | if (a > b) 8 | return a; 9 | return b; 10 | } 11 | 12 | template 13 | class ClassName { 14 | // Class elements 15 | }; 16 | int main() { 17 | /* 18 | Template: 19 | --> The keyword template is used to define function and class template 20 | --> It is a way to make any class or function generalized wrt data types 21 | */ 22 | cout << maxx(2, 3) << endl; 23 | cout << maxx(2.342, 31.121) << endl; 24 | } -------------------------------------------------------------------------------- /C++/thisPointer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Point { 4 | private: 5 | int x, y; 6 | public: 7 | void setData(int x, int y) { 8 | this->x = x; 9 | this->y = y; 10 | } 11 | void getData() { 12 | cout << "The coordinates of the point are: (" << x << "," << y << ")\n"; 13 | } 14 | }; 15 | int main() { 16 | /* 17 | Object Pointer 18 | --> Pointer which contains address of an object is called object pointer 19 | 20 | this Pointer 21 | --> 'this' is a keyword in C++ 22 | --> this is a local object pointer in every instance member function and it contains the 23 | address of the caller object 24 | --> this pointer cannot be modified 25 | --> It is used to refer caller object in member function 26 | */ 27 | // p is an object pointer 28 | Point *p, p1; 29 | 30 | p = &p1; 31 | p->setData(2, 3); 32 | p->getData(); 33 | } -------------------------------------------------------------------------------- /C++/typesOfVariables.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int main() { 6 | /* 7 | 1. Ordinary Variable: 8 | --> A block gets created in the memory with name x, value 5 and some address(starting at base address) 9 | 2. Pointer Variable: 10 | --> Used to hold address of some other variable 11 | 3. Reference Variable: (New in C++, was not in C) 12 | --> It is a new name to an already existing variable 13 | --> To be initialized at declaration only(with some previously created variable and not constant) 14 | --> Contains reference of another variable 15 | --> Reference variables cannot be updated 16 | --> Modern style of pointer. It is an internal pointer 17 | 18 | 19 | IMP: Address will be used in context of pointers and reference will be used in case of reference variables 20 | */ 21 | cout << "Ordinary Variable:\n"; 22 | int x = 3; // Ordinary Variable 23 | cout << "x: " << x << endl; 24 | 25 | cout << "\nPointer Variable:\n"; 26 | int *p; // Pointer Variable 27 | p = &x; // To be read as: p holds address(&) of x 28 | cout << "p (Address of x): " << p << endl; 29 | cout << "*p (Value at address of x): " << *p << endl; 30 | 31 | cout << "\nReference Variable:\n"; 32 | int &y = x; // Reference Variable --> Reference of x is stored in y 33 | cout << "y (Value of x): " << y << endl; 34 | cout << "&y (Reference of x): " << &y << endl; 35 | 36 | assert(p == &y); // Value of address and reference for the same variable should be same; 37 | 38 | 39 | y++; // This increments the value of x 40 | 41 | cout << "\nAfter doing y++:\n"; 42 | 43 | cout << "x: " << x << endl; // Value of x changes to 4 44 | 45 | cout << "y (Value of x): " << y << endl; // Value of y changes to 4 46 | cout << "&y (Reference of x): " << &y << endl; 47 | } -------------------------------------------------------------------------------- /C++/virtualDestructor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class A { 4 | private: 5 | int a, b; 6 | public: 7 | void print() { 8 | cout << "In class A\n"; 9 | } 10 | virtual ~A() { 11 | cout << "In destructor of A\n"; 12 | } 13 | }; 14 | class B: public A { 15 | private: 16 | int x, y; 17 | public: 18 | void print() { 19 | cout << "In class B\n"; 20 | } 21 | ~B() { 22 | cout << "In destructor of B\n"; 23 | } 24 | }; 25 | int main() { 26 | A* a = new B; 27 | a->print(); 28 | delete a; 29 | } -------------------------------------------------------------------------------- /C++/virtualFunctions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Classes A and B are parent & child classes respectively without using virtual functions 5 | class A { 6 | public: 7 | void print() { 8 | cout << "In print function of class A\n"; 9 | } 10 | }; 11 | 12 | class B: public A { 13 | public: 14 | void print() { 15 | cout << "In print function of class B\n"; 16 | } 17 | }; 18 | 19 | // Classes C and D are parent & child classes respectively using virtual functions 20 | class C { 21 | public: 22 | /* 23 | This virtual keyword tells the compiler that late binding(binding at runtime) should 24 | take place for this function 25 | 26 | Working concept behind virtual function link: 27 | https://www.youtube.com/watch?v=Z_FiER8aAqM&list=PLLYz8uHU480j37APNXBdPz7YzAi4XlQUF&index=45&ab_channel=C%2B%2BbySaurabhShuklaSir 28 | */ 29 | virtual void print() { 30 | cout << "In print function of class C\n"; 31 | } 32 | }; 33 | 34 | class D: public C { 35 | public: 36 | void print() { 37 | cout << "In print function of class D\n"; 38 | } 39 | }; 40 | 41 | int main() { 42 | 43 | /* 44 | Base Class Pointer: 45 | --> Base class pointer can point to the objects of any of it's descendant class(es) 46 | --> The converse is not true (Child pointer can't point to parent's objects) 47 | 48 | IMP: Binding of function call with function definition happens at Compile Time only 49 | */ 50 | 51 | cout << "Before using virtual function..\n"; 52 | A *p1, a; 53 | B b; 54 | p1 = &b; 55 | a.print(); 56 | b.print(); 57 | 58 | /* 59 | The main issue with overriding occurs over here. 60 | --> You feel that since p1 contains address of an object of class B, it should bind with the print() 61 | method of class B 62 | --> However, this does not happen in reality. Binding happens at compile time. 63 | --> The function call at this moment does not know the address it will contain. Cause addresses are 64 | assigned at runtime 65 | --> So it checks for the datatype of p1 which is of type A and binds it there 66 | --> Thus, print() function of class A is called 67 | 68 | This problem in hand paves way for the need of virtual function 69 | */ 70 | 71 | p1->print(); 72 | 73 | 74 | 75 | // Using virtual functions 76 | cout << "\nAfter using virtual function..\n"; 77 | C *p2, c; 78 | D d; 79 | p2 = &d; 80 | c.print(); 81 | d.print(); 82 | p2->print(); 83 | 84 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OOP in C++ 2 | ![Lines of code](https://img.shields.io/tokei/lines/github/jineshparakh/OOP?style=for-the-badge) 3 | Repo Views 4 | 5 | ## Table of Contents 6 | 7 | 8 | Sr.No. | Topic | Code | 9 | | --- | --- | --- | 10 | 1| Types of Variables | [C++ Code Link](/C++/typesOfVariables.cpp) 11 | 2| General idea about functions | [C++ Code Link](/C++/functions.cpp) 12 | 3| Default Paramaters | [C++ Code Link](/C++/defaultParamaters.cpp) 13 | 4| Inline function | [C++ Code Link](/C++/inlineFunction.cpp) 14 | 5| Call by Value | [C++ Code Link](/C++/callByValue.cpp) 15 | 6| Call by Reference | [C++ Code Link](/C++/callByReference.cpp) 16 | 7| Call by Address | [C++ Code Link](/C++/callByAddress.cpp) 17 | 8| Function Overloading | [C++ Code Link](/C++/functionOverloading.cpp) 18 | 9| Structure (struct) | [C++ Code Link](/C++/structure.cpp) 19 | 10| Classes & Objects | [C++ Code Link](/C++/classesAndObjects.cpp) 20 | 11| Static local Variable | [C++ Code Link](/C++/staticLocalVariable.cpp) 21 | 12| Static Member Variable and Function | [C++ Code Link](/C++/staticMemberVariableAndMemberFunction.cpp) 22 | 13| Constructors | [C++ Code Link](/C++/constructors.cpp) 23 | 14| Destructor | [C++ Code Link](/C++/destructor.cpp) 24 | 15| Operator Overloading | [C++ Code Link](/C++/operatorOverloading.cpp) 25 | 16| Friend Function | [C++ Code Link](/C++/friendFunction.cpp) 26 | 17| Friend Class And Functions | [C++ Code Link](/C++/friendClassAndFunction.cpp) 27 | 18| Operator Overloading using Friend Function | [C++ Code Link](/C++/operatorOverloadingUsingFriendFunction.cpp) 28 | 19| Inheritance | [C++ Code Link](/C++/inheritance.cpp) 29 | 20| Constructor and Destructor in Inheritance | [C++ Code Link](/C++/constructorAndDestructorInInheritance.cpp) 30 | 21| this Pointer | [C++ Code Link](/C++/thisPointer.cpp) 31 | 22| Method Overriding and Hiding | [C++ Code Link](/C++/methodOverridingAndHiding.cpp) 32 | 23| new and delete | [C++ Code Link](/C++/newAndDelete.cpp) 33 | 24| Virtual Functions | [C++ Code Link](/C++/virtualFunctions.cpp) 34 | 25| Abstract Class | [C++ Code Link](/C++/abstractClass.cpp) 35 | 26| Template | [C++ Code Link](/C++/template.cpp) 36 | 27| File Handling | [C++ Code Link](/C++/fileHandling.cpp) 37 | 28| tellg & tellp | [C++ Code Link](/C++/tellg&tellp.cpp) 38 | 29| seekg & seekp | [C++ Code Link](/C++/seekp&seekg.cpp) 39 | 30| Initializer List | [C++ Code Link](/C++/initializerList.cpp) 40 | 31| Shallow & Deep Copy | [C++ Code Link](/C++/shallowAndDeepCopy.cpp) 41 | 32| Type Conversion - Primitive to Class Type | [C++ Code Link](/C++/PrimitiveToClassTypeConversion.cpp) 42 | 33| Type Conversion - Class to another Class Type | [C++ Code Link](/C++/classTypeToAnotherClassType.cpp) 43 | 34| Type Conversion - Class to Primitive Type| [C++ Code Link](/C++/classTypeToPrimitiveType.cpp) 44 | 35| Exception Handling | [C++ Code Link](/C++/exceptionHandling.cpp) 45 | 36| Namespaces | [C++ Code Link](/C++/namespaces.cpp) 46 | 37| Virtual Destructor | [C++ Code Link](/C++/virtualDestructor.cpp) 47 | 38| Nested Class| [C++ Code Link](/C++/nestedClass.cpp) 48 | 49 | 50 | --------------------------------------------------------------------------------