From 9c6212ba9a1d140124bfa47946e2ab23cd2ebfa5 Mon Sep 17 00:00:00 2001 From: Avinash Kumar Date: Mon, 14 Dec 2020 14:16:21 +0530 Subject: [PATCH] Modification when shift from windows to linux --- 1. CppBasics/Addition.cpp | 26 +- 1. CppBasics/AreaofCircle.cpp | 28 +- 1. CppBasics/AreaofTriangle.cpp | 32 +- 1. CppBasics/CompoundAssignment.cpp | 28 +- 1. CppBasics/HelloWorld.cpp | 18 +- 1. CppBasics/IncDecOperator.cpp | 44 +- 1. CppBasics/InputName.cpp | 26 +- 1. CppBasics/Overflow.cpp | 48 +- 1. CppBasics/RootsQuadraticEq.cpp | 36 +- 1. CppBasics/Speed.cpp | 30 +- 1. CppBasics/StudentExercise.cpp | 62 +- 1. CppBasics/SumofNaturalNo.cpp | 28 +- 10. Inheritance/AccessSpecifiers.cpp | 78 +- 10. Inheritance/Inheritance.cpp | 182 ++-- 10. Inheritance/InheritanceConstructor.cpp | 90 +- 10. Inheritance/InheritanceExample.cpp | 80 +- 10. Inheritance/StudentExercise.cpp | 118 +-- 10. Inheritance/WaysOfInheritance.cpp | 104 +- 11. Polymorphism/Polymorphism.cpp | 100 +- 12. FriendAndStaticMember/FriendClass.cpp | 70 +- 12. FriendAndStaticMember/FriendFunction.cpp | 58 +- 12. FriendAndStaticMember/NestedClass.cpp | 68 +- 12. FriendAndStaticMember/StaticMember.cpp | 56 +- 2. ConditionalStatements/BillAmount.cpp | 48 +- .../CompoundConditional.cpp | 52 +- 2. ConditionalStatements/DayofWeek.cpp | 68 +- .../DynamicDeclaration.cpp | 46 +- 2. ConditionalStatements/LeapYear.cpp | 66 +- 2. ConditionalStatements/MaxAmong2No.cpp | 42 +- 2. ConditionalStatements/MaxAmong3No.cpp | 50 +- .../NatureofQuadraticRoot.cpp | 58 +- 2. ConditionalStatements/ShortCircuit.cpp | 32 +- 2. ConditionalStatements/SwitchCase.cpp | 74 +- 2. ConditionalStatements/Validation.cpp | 38 +- 3. Loops/AllLoops.cpp | 66 +- 3. Loops/AmstrongNumber.cpp | 60 +- 3. Loops/Factorial.cpp | 38 +- 3. Loops/Factors.cpp | 36 +- 3. Loops/GCDofNumber.cpp | 50 +- 3. Loops/MultiplicationTable.cpp | 36 +- 3. Loops/PalindromeNumber.cpp | 60 +- 3. Loops/PerfectNumber.cpp | 60 +- 3. Loops/PrimeNumber.cpp | 70 +- 3. Loops/ReverseANo.cpp | 40 +- 3. Loops/ReverseDigitofNo.cpp | 36 +- 3. Loops/SumofNaturalNo.cpp | 38 +- 4. Arrays/2D-Arrays.cpp | 36 +- 4. Arrays/AverageofNumbers.cpp | 42 +- 4. Arrays/ForEachLoop.cpp | 42 +- 4. Arrays/LinearSearch.cpp | 44 +- 4. Arrays/MatrixAddition.cpp | 58 +- 4. Arrays/MatrixMultiplication.cpp | 112 +-- 4. Arrays/MaxElement.cpp | 40 +- 4. Arrays/NesteForLoops.cpp | 34 +- 4. Arrays/Pattern1.cpp | 52 +- 4. Arrays/Pattern2.cpp | 54 +- 4. Arrays/Pattern3.cpp | 62 +- 4. Arrays/Pattern4.cpp | 70 +- 4. Arrays/SumofArrayElement.cpp | 34 +- 5. Pointers/PointerSyntax.cpp | 30 +- 6. Strings/ChangingCase.cpp | 32 +- 6. Strings/CountVowelConsonant.cpp | 38 +- 6. Strings/LengthofString.cpp | 44 +- 6. Strings/Palindrome.cpp | 42 +- 6. Strings/ReadWriteString.cpp | 70 +- 6. Strings/StringToInteger.cpp | 42 +- 6. Strings/Tokenizing.cpp | 34 +- 6. Strings/UpperCase.cpp | 32 +- 6. Strings/UserNameEmail.cpp | 20 +- 7. Functions/DefaultArgument.cpp | 26 +- 7. Functions/FunctionOverloading.cpp | 50 +- 7. Functions/FunctionTemplate.cpp | 32 +- 7. Functions/LinearSearch.cpp | 52 +- 7. Functions/ReturnAddress.cpp | 44 +- 7. Functions/ReturnReference.cpp | 32 +- 7. Functions/StaticVariables.cpp | 32 +- 7. Functions/Sum.cpp | 30 +- 7. Functions/Swap.cpp | 38 +- 8. IntroductionToOPPs/Constructor.cpp | 162 ++-- 8. IntroductionToOPPs/DataHiding.cpp | 138 +-- 8. IntroductionToOPPs/PointerToObject.cpp | 82 +- 8. IntroductionToOPPs/Rectangle.cpp | 72 +- 8. IntroductionToOPPs/StudentExercise.cpp | 128 +-- 8. IntroductionToOPPs/ThisPointer.cpp | 68 +- 9. OperatorOverloading/ComplexNumber.cpp | 84 +- .../FriendOperatorOverloading.cpp | 88 +- .../InsertionOperatorOverloading.cpp | 104 +- 9. OperatorOverloading/RationalNumber.cpp | 132 +-- README.md | 888 +++++++++--------- 89 files changed, 2960 insertions(+), 2960 deletions(-) diff --git a/1. CppBasics/Addition.cpp b/1. CppBasics/Addition.cpp index 8a52247..932191c 100644 --- a/1. CppBasics/Addition.cpp +++ b/1. CppBasics/Addition.cpp @@ -1,14 +1,14 @@ -#include -using namespace std; - -int main(void) -{ - int a, b, c; - cout << "Enter 2 Numbers: "; - cin >> a >> b; //Input from Console - - c = a + b; - - cout << "Addition = " << c << endl; - return 0; +#include +using namespace std; + +int main(void) +{ + int a, b, c; + cout << "Enter 2 Numbers: "; + cin >> a >> b; //Input from Console + + c = a + b; + + cout << "Addition = " << c << endl; + return 0; } \ No newline at end of file diff --git a/1. CppBasics/AreaofCircle.cpp b/1. CppBasics/AreaofCircle.cpp index 114f4dc..9766109 100644 --- a/1. CppBasics/AreaofCircle.cpp +++ b/1. CppBasics/AreaofCircle.cpp @@ -1,15 +1,15 @@ -#include -using namespace std; - -int main(void) -{ - float radius, area; - - cout << "Enter Radius: "; - cin >> radius; - area = 3.1425f * radius * radius; - - cout << "Area is " << area; - - return 0; +#include +using namespace std; + +int main(void) +{ + float radius, area; + + cout << "Enter Radius: "; + cin >> radius; + area = 3.1425f * radius * radius; + + cout << "Area is " << area; + + return 0; } \ No newline at end of file diff --git a/1. CppBasics/AreaofTriangle.cpp b/1. CppBasics/AreaofTriangle.cpp index 63405ba..7dc8e59 100644 --- a/1. CppBasics/AreaofTriangle.cpp +++ b/1. CppBasics/AreaofTriangle.cpp @@ -1,17 +1,17 @@ -#include -using namespace std; - -int main(void) -{ - //Variables - float area, height, base; - //Taking User Input - cout << "Enter Base & Height Respectively: "; - cin >> base >> height; - //Calculating Area of Triangle - area = (base * height) / 2; - //Printing Area to the console - cout << area << endl; - - return 0; +#include +using namespace std; + +int main(void) +{ + //Variables + float area, height, base; + //Taking User Input + cout << "Enter Base & Height Respectively: "; + cin >> base >> height; + //Calculating Area of Triangle + area = (base * height) / 2; + //Printing Area to the console + cout << area << endl; + + return 0; } \ No newline at end of file diff --git a/1. CppBasics/CompoundAssignment.cpp b/1. CppBasics/CompoundAssignment.cpp index 4b6239d..766189a 100644 --- a/1. CppBasics/CompoundAssignment.cpp +++ b/1. CppBasics/CompoundAssignment.cpp @@ -1,15 +1,15 @@ -#include -using namespace std; - -int main() -{ - int sum = 10, x = 5; - sum += x; - cout << sum << endl; - - int fact = 10, y = 5; - fact *= y; - cout << fact << endl; - - return 0; +#include +using namespace std; + +int main() +{ + int sum = 10, x = 5; + sum += x; + cout << sum << endl; + + int fact = 10, y = 5; + fact *= y; + cout << fact << endl; + + return 0; } \ No newline at end of file diff --git a/1. CppBasics/HelloWorld.cpp b/1. CppBasics/HelloWorld.cpp index a9c4915..2bd71bd 100644 --- a/1. CppBasics/HelloWorld.cpp +++ b/1. CppBasics/HelloWorld.cpp @@ -1,10 +1,10 @@ -#include //Preprocessor Directive -using namespace std; //Global Namespace - -int main(void) -{ - cout << "Hello World" << endl; //Output to Console - // cin << Insertion Operator - // cout >> Extraction Operator - return 0; +#include //Preprocessor Directive +using namespace std; //Global Namespace + +int main(void) +{ + cout << "Hello World" << endl; //Output to Console + // cin << Insertion Operator + // cout >> Extraction Operator + return 0; } \ No newline at end of file diff --git a/1. CppBasics/IncDecOperator.cpp b/1. CppBasics/IncDecOperator.cpp index 66155b1..330fdc4 100644 --- a/1. CppBasics/IncDecOperator.cpp +++ b/1. CppBasics/IncDecOperator.cpp @@ -1,23 +1,23 @@ -#include -using namespace std; - -int main() -{ - int i = 5, j; - j = i++; - cout << j << " " << i << endl; - - int k = 5, l; - l = ++k; - cout << l << " " << k << endl; - - int a = 5, b; - b = 2 * ++a + 2 * a++; - cout << b << " " << a << endl; - - int c = 5, d; - d = 2 * c++ + 2 * c++; - cout << d << " " << c << endl; - - return 0; +#include +using namespace std; + +int main() +{ + int i = 5, j; + j = i++; + cout << j << " " << i << endl; + + int k = 5, l; + l = ++k; + cout << l << " " << k << endl; + + int a = 5, b; + b = 2 * ++a + 2 * a++; + cout << b << " " << a << endl; + + int c = 5, d; + d = 2 * c++ + 2 * c++; + cout << d << " " << c << endl; + + return 0; } \ No newline at end of file diff --git a/1. CppBasics/InputName.cpp b/1. CppBasics/InputName.cpp index 09768cd..1e58115 100644 --- a/1. CppBasics/InputName.cpp +++ b/1. CppBasics/InputName.cpp @@ -1,14 +1,14 @@ -#include -#include -using namespace std; - -int main(void) -{ - string name; - cout << "May I know Your Name ?" << endl; - getline(cin, name); //cin>>name; - - cout << "Welcome! " << name << endl; - - return 0; +#include +#include +using namespace std; + +int main(void) +{ + string name; + cout << "May I know Your Name ?" << endl; + getline(cin, name); //cin>>name; + + cout << "Welcome! " << name << endl; + + return 0; } \ No newline at end of file diff --git a/1. CppBasics/Overflow.cpp b/1. CppBasics/Overflow.cpp index 6d6540b..c66828a 100644 --- a/1. CppBasics/Overflow.cpp +++ b/1. CppBasics/Overflow.cpp @@ -1,25 +1,25 @@ -#include -using namespace std; - -int main() -{ - char a = 128; - cout << (int)a << endl; - - char b = 127; - b++; - cout << (int)b << endl; - - char c = -129; - cout << (int)c << endl; - - char d = -128; - d--; - cout << (int)d << endl; - - int e = INT_MAX; - e++; - cout << (int)e << endl; - - return 0; +#include +using namespace std; + +int main() +{ + char a = 128; + cout << (int)a << endl; + + char b = 127; + b++; + cout << (int)b << endl; + + char c = -129; + cout << (int)c << endl; + + char d = -128; + d--; + cout << (int)d << endl; + + int e = INT_MAX; + e++; + cout << (int)e << endl; + + return 0; } \ No newline at end of file diff --git a/1. CppBasics/RootsQuadraticEq.cpp b/1. CppBasics/RootsQuadraticEq.cpp index 0e2c984..815497f 100644 --- a/1. CppBasics/RootsQuadraticEq.cpp +++ b/1. CppBasics/RootsQuadraticEq.cpp @@ -1,19 +1,19 @@ -#include -#include -using namespace std; -//Finding the root of a Quadratic Equation -int main(void) -{ - float a, b, c, x1, x2; - - cout << "Enter the coefficients of x^2, x and constant terms respectively: "; - cin >> a >> b >> c; - - x1 = (-b + sqrt((b * b) - (4 * a * c))) / (2 * a); - x1 = (-b - sqrt((b * b) - (4 * a * c))) / (2 * a); - - cout << "\nRoots are:\n" - << "x1 = " << x1 << " x2 = " << x2 << endl; - - return 0; +#include +#include +using namespace std; +//Finding the root of a Quadratic Equation +int main(void) +{ + float a, b, c, x1, x2; + + cout << "Enter the coefficients of x^2, x and constant terms respectively: "; + cin >> a >> b >> c; + + x1 = (-b + sqrt((b * b) - (4 * a * c))) / (2 * a); + x1 = (-b - sqrt((b * b) - (4 * a * c))) / (2 * a); + + cout << "\nRoots are:\n" + << "x1 = " << x1 << " x2 = " << x2 << endl; + + return 0; } \ No newline at end of file diff --git a/1. CppBasics/Speed.cpp b/1. CppBasics/Speed.cpp index eae07b9..5d0b137 100644 --- a/1. CppBasics/Speed.cpp +++ b/1. CppBasics/Speed.cpp @@ -1,16 +1,16 @@ -#include -using namespace std; - -int main(void) -{ - float u, v, a; //initial_velocity, final_velocity, acceleration - cout << "Enter Values: "; - cin >> u >> v >> a; - - //Calculating speed using formula - int speed = ((v * v) - (u * u)) / (2 * a); - - cout << "Speed = " << speed << endl; - - return 0; +#include +using namespace std; + +int main(void) +{ + float u, v, a; //initial_velocity, final_velocity, acceleration + cout << "Enter Values: "; + cin >> u >> v >> a; + + //Calculating speed using formula + int speed = ((v * v) - (u * u)) / (2 * a); + + cout << "Speed = " << speed << endl; + + return 0; } \ No newline at end of file diff --git a/1. CppBasics/StudentExercise.cpp b/1. CppBasics/StudentExercise.cpp index 787cb34..4887bab 100644 --- a/1. CppBasics/StudentExercise.cpp +++ b/1. CppBasics/StudentExercise.cpp @@ -1,32 +1,32 @@ -/* -Write a program to calculate Net Salary Program should take following as input: -1. Basic Salary -2. Percentage of Allowances -3. Percentage of Deductions -*/ - -#include -using namespace std; - -int main(void) -{ - float basic; - float percentAllow; - float percentDeduct; - float netSalary; - - cout << "Enter Basic Salary: "; - cin >> basic; - cout << "Enter percent of Allowances: "; - cin >> percentAllow; - cout << "Enter percent of Deductions: "; - cin >> percentDeduct; - - netSalary = basic + basic * percentAllow / 100 - basic * percentDeduct / 100; - - cout << "Net Salary is: " << netSalary << endl; - - return 0; -} -// Date: 09 Nov 2020 +/* +Write a program to calculate Net Salary Program should take following as input: +1. Basic Salary +2. Percentage of Allowances +3. Percentage of Deductions +*/ + +#include +using namespace std; + +int main(void) +{ + float basic; + float percentAllow; + float percentDeduct; + float netSalary; + + cout << "Enter Basic Salary: "; + cin >> basic; + cout << "Enter percent of Allowances: "; + cin >> percentAllow; + cout << "Enter percent of Deductions: "; + cin >> percentDeduct; + + netSalary = basic + basic * percentAllow / 100 - basic * percentDeduct / 100; + + cout << "Net Salary is: " << netSalary << endl; + + return 0; +} +// Date: 09 Nov 2020 // Code By: Avinash Kumar. \ No newline at end of file diff --git a/1. CppBasics/SumofNaturalNo.cpp b/1. CppBasics/SumofNaturalNo.cpp index 3340726..c2acf82 100644 --- a/1. CppBasics/SumofNaturalNo.cpp +++ b/1. CppBasics/SumofNaturalNo.cpp @@ -1,15 +1,15 @@ -#include -using namespace std; -//Calculating the sum of N natural Number -int main(void) -{ - int lastIndex, sum; - - cout << "Enter the last index: "; - cin >> lastIndex; - //Calculating the sum using mathematical formula - sum = (lastIndex * (lastIndex + 1)) / 2; - - cout << "Sum of " << lastIndex << " Natural Numbers is " << sum << endl; - return 0; +#include +using namespace std; +//Calculating the sum of N natural Number +int main(void) +{ + int lastIndex, sum; + + cout << "Enter the last index: "; + cin >> lastIndex; + //Calculating the sum using mathematical formula + sum = (lastIndex * (lastIndex + 1)) / 2; + + cout << "Sum of " << lastIndex << " Natural Numbers is " << sum << endl; + return 0; } \ No newline at end of file diff --git a/10. Inheritance/AccessSpecifiers.cpp b/10. Inheritance/AccessSpecifiers.cpp index c44c7fe..c99e3ba 100644 --- a/10. Inheritance/AccessSpecifiers.cpp +++ b/10. Inheritance/AccessSpecifiers.cpp @@ -1,40 +1,40 @@ -#include -using namespace std; - -class Base -{ -private: - int a; - -protected: - int b; - -public: - int c; - void funBase() - { - a = 10; //Accessable - b = 5; //Accessable - c = 15; //Accessable - } -}; - -class Derived : public Base -{ -public: - void funDerived() - { - a = 10; //Cannot Accessable - b = 5; //Accessable - c = 15; //Accessable - } -}; - -int main() -{ - Base b; - b.a = 10; - b.b = 5; - b.c = 20; -} +#include +using namespace std; + +class Base +{ +private: + int a; + +protected: + int b; + +public: + int c; + void funBase() + { + a = 10; //Accessable + b = 5; //Accessable + c = 15; //Accessable + } +}; + +class Derived : public Base +{ +public: + void funDerived() + { + a = 10; //Cannot Accessable + b = 5; //Accessable + c = 15; //Accessable + } +}; + +int main() +{ + Base b; + b.a = 10; + b.b = 5; + b.c = 20; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/10. Inheritance/Inheritance.cpp b/10. Inheritance/Inheritance.cpp index f2a8cc4..6135096 100644 --- a/10. Inheritance/Inheritance.cpp +++ b/10. Inheritance/Inheritance.cpp @@ -1,92 +1,92 @@ -#include -using namespace std; -//Program to demonstrate Inheritance of cuboid from Rectangle class - -//Base Class -class Rectangle -{ -private: - int length; - int breadth; - -public: - //Mutators or Setters - void setLength(int l) - { - if (l > 0) - { - length = l; - } - else - { - length = 0; - } - } - void setBreadth(int b) - { - if (b > 0) - { - breadth = b; - } - else - { - breadth = 0; - } - } - //Accessors or Getters - int getLength() - { - return length; - } - int getBreadth() - { - return breadth; - } - //Function to calculate area of rectangle - int area() - { - return (length * breadth); - } - //Function to calculate perimeter of rectangle - int perimeter() - { - return (2 * (length + breadth)); - } -}; -//Derived Class -class Cuboid : public Rectangle -{ -private: - int height; - -public: - Cuboid(int l = 0, int b = 0, int h = 0) - { - setLength(l); - setBreadth(b); - height = h; - } - int getHeight() - { - return height; - } - void setHeight(int h) - { - height = h; - } - int volume() - { - return (getLength() * getBreadth() * height); - } -}; - -int main(void) -{ - Cuboid c(10, 5, 3); - cout << c.getLength() << " " << c.getBreadth() << " " << c.getHeight(); - cout << "Volume = " << c.volume() << endl; - cout << "Area = " << c.area() << endl; - - return 0; -} +#include +using namespace std; +//Program to demonstrate Inheritance of cuboid from Rectangle class + +//Base Class +class Rectangle +{ +private: + int length; + int breadth; + +public: + //Mutators or Setters + void setLength(int l) + { + if (l > 0) + { + length = l; + } + else + { + length = 0; + } + } + void setBreadth(int b) + { + if (b > 0) + { + breadth = b; + } + else + { + breadth = 0; + } + } + //Accessors or Getters + int getLength() + { + return length; + } + int getBreadth() + { + return breadth; + } + //Function to calculate area of rectangle + int area() + { + return (length * breadth); + } + //Function to calculate perimeter of rectangle + int perimeter() + { + return (2 * (length + breadth)); + } +}; +//Derived Class +class Cuboid : public Rectangle +{ +private: + int height; + +public: + Cuboid(int l = 0, int b = 0, int h = 0) + { + setLength(l); + setBreadth(b); + height = h; + } + int getHeight() + { + return height; + } + void setHeight(int h) + { + height = h; + } + int volume() + { + return (getLength() * getBreadth() * height); + } +}; + +int main(void) +{ + Cuboid c(10, 5, 3); + cout << c.getLength() << " " << c.getBreadth() << " " << c.getHeight(); + cout << "Volume = " << c.volume() << endl; + cout << "Area = " << c.area() << endl; + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/10. Inheritance/InheritanceConstructor.cpp b/10. Inheritance/InheritanceConstructor.cpp index bec5cef..521227e 100644 --- a/10. Inheritance/InheritanceConstructor.cpp +++ b/10. Inheritance/InheritanceConstructor.cpp @@ -1,46 +1,46 @@ -#include -using namespace std; -//Program to demonstrate Constructor in Inheritance - -//Parent class -class Base -{ -public: - Base() - { - cout << "Default of Base" << endl; - } - Base(int x) - { - cout << "Parameterized of Base " << x << endl; - } -}; -//Child class -class Derived : public Base -{ -public: - Derived() - { - cout << "Default of Derived" << endl; - } - Derived(int a) - { - cout << "Parameterized of Derived " << a << endl; - } - Derived(int x, int a) : Base(x) - { - cout << "Parameterized of Derived " << a << endl; - } -}; - -int main(void) -{ - Derived d1; - cout << endl; - Derived d2(10); - cout << endl; - Derived d3(20, 10); - - return 0; -} +#include +using namespace std; +//Program to demonstrate Constructor in Inheritance + +//Parent class +class Base +{ +public: + Base() + { + cout << "Default of Base" << endl; + } + Base(int x) + { + cout << "Parameterized of Base " << x << endl; + } +}; +//Child class +class Derived : public Base +{ +public: + Derived() + { + cout << "Default of Derived" << endl; + } + Derived(int a) + { + cout << "Parameterized of Derived " << a << endl; + } + Derived(int x, int a) : Base(x) + { + cout << "Parameterized of Derived " << a << endl; + } +}; + +int main(void) +{ + Derived d1; + cout << endl; + Derived d2(10); + cout << endl; + Derived d3(20, 10); + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/10. Inheritance/InheritanceExample.cpp b/10. Inheritance/InheritanceExample.cpp index 7635c4e..6d2a154 100644 --- a/10. Inheritance/InheritanceExample.cpp +++ b/10. Inheritance/InheritanceExample.cpp @@ -1,41 +1,41 @@ -#include -using namespace std; - -class Base -{ - -public: - int x; - void show() - { - cout << "Base Class:" << endl; - cout << x << endl; - } -}; - -class Derived : public Base -{ -public: - int y; - void display() - { - cout << "Derived Class:" << endl; - cout << x << " " << y << endl; - } -}; - -int main(void) -{ - Base b; - b.x = 25; - b.show(); - - Derived d; - d.x = 10; - d.y = 15; - d.show(); - d.display(); - - return 0; -} +#include +using namespace std; + +class Base +{ + +public: + int x; + void show() + { + cout << "Base Class:" << endl; + cout << x << endl; + } +}; + +class Derived : public Base +{ +public: + int y; + void display() + { + cout << "Derived Class:" << endl; + cout << x << " " << y << endl; + } +}; + +int main(void) +{ + Base b; + b.x = 25; + b.show(); + + Derived d; + d.x = 10; + d.y = 15; + d.show(); + d.display(); + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/10. Inheritance/StudentExercise.cpp b/10. Inheritance/StudentExercise.cpp index fceb6ea..84b8e82 100644 --- a/10. Inheritance/StudentExercise.cpp +++ b/10. Inheritance/StudentExercise.cpp @@ -1,60 +1,60 @@ -#include -using namespace std; -/*Write a class for Employee -Derived classes -1. Full time Employee with Salary -2. Part Time Employee with Daily wages -Write required Methods*/ -class Employee -{ -private: - int eid; - string name; - -public: - Employee(int e, string n) - { - eid = e; - name = n; - } - int getEmployeeID() { return eid; } - string getName() { return name; } -}; - -class FullTimeEmployee : public Employee -{ -private: - int salary; - -public: - FullTimeEmployee(int e, string n, int sal) : Employee(e, n) - { - salary = sal; - } - int getSalary() { return salary; } -}; - -class PartTimeEmployee : public Employee -{ -private: - int wage; - -public: - PartTimeEmployee(int e, string n, int w) : Employee(e, n) - { - wage = w; - } - int getWage() { return wage; } -}; - -int main() -{ - PartTimeEmployee p1(1, "John", 300); - FullTimeEmployee p2(2, "Raj", 5000); - - cout << "Salary of " << p2.getName() << " is " << p2.getSalary() << endl; - cout << "Daily wage of " << p1.getName() << " is " << p1.getWage() << endl; - - return 0; -} +#include +using namespace std; +/*Write a class for Employee +Derived classes +1. Full time Employee with Salary +2. Part Time Employee with Daily wages +Write required Methods*/ +class Employee +{ +private: + int eid; + string name; + +public: + Employee(int e, string n) + { + eid = e; + name = n; + } + int getEmployeeID() { return eid; } + string getName() { return name; } +}; + +class FullTimeEmployee : public Employee +{ +private: + int salary; + +public: + FullTimeEmployee(int e, string n, int sal) : Employee(e, n) + { + salary = sal; + } + int getSalary() { return salary; } +}; + +class PartTimeEmployee : public Employee +{ +private: + int wage; + +public: + PartTimeEmployee(int e, string n, int w) : Employee(e, n) + { + wage = w; + } + int getWage() { return wage; } +}; + +int main() +{ + PartTimeEmployee p1(1, "John", 300); + FullTimeEmployee p2(2, "Raj", 5000); + + cout << "Salary of " << p2.getName() << " is " << p2.getSalary() << endl; + cout << "Daily wage of " << p1.getName() << " is " << p1.getWage() << endl; + + return 0; +} //Code By: Avinash Kumar \ No newline at end of file diff --git a/10. Inheritance/WaysOfInheritance.cpp b/10. Inheritance/WaysOfInheritance.cpp index 2e99837..e18a590 100644 --- a/10. Inheritance/WaysOfInheritance.cpp +++ b/10. Inheritance/WaysOfInheritance.cpp @@ -1,53 +1,53 @@ -#include -using namespace std; - -class Parent -{ -private: - int a; - -protected: - int b; - -public: - int c; - void funParent() - { - a = 10; - b = 5; - c = 15; - } -}; - -class Child : private Parent -{ -private: -protected: -public: - void funChild() - { - //a=10; - b = 5; - c = 15; - } -}; - -class GrandChild : public Child -{ -public: - void funGrandChild() - { - //a=10; - //b=5; - //c=20; - } -}; - -int main() -{ - // Child c; - //c.a=10; - //c.b=5; - //c.c=20; -} +#include +using namespace std; + +class Parent +{ +private: + int a; + +protected: + int b; + +public: + int c; + void funParent() + { + a = 10; + b = 5; + c = 15; + } +}; + +class Child : private Parent +{ +private: +protected: +public: + void funChild() + { + //a=10; + b = 5; + c = 15; + } +}; + +class GrandChild : public Child +{ +public: + void funGrandChild() + { + //a=10; + //b=5; + //c=20; + } +}; + +int main() +{ + // Child c; + //c.a=10; + //c.b=5; + //c.c=20; +} //Code By: Avinash Kumar \ No newline at end of file diff --git a/11. Polymorphism/Polymorphism.cpp b/11. Polymorphism/Polymorphism.cpp index c01e449..c89cf13 100644 --- a/11. Polymorphism/Polymorphism.cpp +++ b/11. Polymorphism/Polymorphism.cpp @@ -1,51 +1,51 @@ -/* Write classes to Demonstrate Polymorphism -Base class shape -Derived classes -1. Rectangle -2. Circle */ -#include -using namespace std; - -class Shape -{ -public: - virtual float area() = 0; - virtual float perimeter() = 0; -}; - -class Rectangle : public Shape -{ -private: - float length; - float breadth; - -public: - Rectangle(int l = 1, int b = 1) - { - length = 1; - breadth = b; - } - float area() { return length * breadth; } - float perimeter() { return 2 * (length + breadth); } -}; - -class Circle : public Shape -{ -private: - float radius; - -public: - Circle(float r) { radius = r; } - float area() { return 3.1425 * radius * radius; } - float perimeter() { return 2 * 3.1425 * radius; } -}; - -int main() -{ - Shape *s = new Rectangle(10, 5); - cout << "Area of Rectangle " << s->area() << endl; - cout << "Perimeter of Rectangle " << s->perimeter() << endl; - s = new Circle(10); - cout << "Area of Circle " << s->area() << endl; - cout << "Perimeter of Circle " << s->perimeter() << endl; +/* Write classes to Demonstrate Polymorphism +Base class shape +Derived classes +1. Rectangle +2. Circle */ +#include +using namespace std; + +class Shape +{ +public: + virtual float area() = 0; + virtual float perimeter() = 0; +}; + +class Rectangle : public Shape +{ +private: + float length; + float breadth; + +public: + Rectangle(int l = 1, int b = 1) + { + length = 1; + breadth = b; + } + float area() { return length * breadth; } + float perimeter() { return 2 * (length + breadth); } +}; + +class Circle : public Shape +{ +private: + float radius; + +public: + Circle(float r) { radius = r; } + float area() { return 3.1425 * radius * radius; } + float perimeter() { return 2 * 3.1425 * radius; } +}; + +int main() +{ + Shape *s = new Rectangle(10, 5); + cout << "Area of Rectangle " << s->area() << endl; + cout << "Perimeter of Rectangle " << s->perimeter() << endl; + s = new Circle(10); + cout << "Area of Circle " << s->area() << endl; + cout << "Perimeter of Circle " << s->perimeter() << endl; } \ No newline at end of file diff --git a/12. FriendAndStaticMember/FriendClass.cpp b/12. FriendAndStaticMember/FriendClass.cpp index fe074c5..b714a20 100644 --- a/12. FriendAndStaticMember/FriendClass.cpp +++ b/12. FriendAndStaticMember/FriendClass.cpp @@ -1,36 +1,36 @@ -#include -using namespace std; - -class Your; //Class Your Declaration - -class My -{ -private: - int a; - -protected: - int b; - -public: - int c; - friend Your; -}; - -class Your -{ -public: - My m; - void fun() - { - m.a = 10; - m.b = 10; - m.c = 10; - } -}; - -int main(void) -{ - - return 0; -} +#include +using namespace std; + +class Your; //Class Your Declaration + +class My +{ +private: + int a; + +protected: + int b; + +public: + int c; + friend Your; +}; + +class Your +{ +public: + My m; + void fun() + { + m.a = 10; + m.b = 10; + m.c = 10; + } +}; + +int main(void) +{ + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/12. FriendAndStaticMember/FriendFunction.cpp b/12. FriendAndStaticMember/FriendFunction.cpp index f0cd7ec..7e30904 100644 --- a/12. FriendAndStaticMember/FriendFunction.cpp +++ b/12. FriendAndStaticMember/FriendFunction.cpp @@ -1,30 +1,30 @@ -#include -using namespace std; - -class Test -{ -private: - int a; - -protected: - int b; - -public: - int c; - friend void fun(); -}; - -void fun() -{ - Test t; - t.a = 10; - t.b = 15; - t.c = 9; -} - -int main(void) -{ - - return 0; -} +#include +using namespace std; + +class Test +{ +private: + int a; + +protected: + int b; + +public: + int c; + friend void fun(); +}; + +void fun() +{ + Test t; + t.a = 10; + t.b = 15; + t.c = 9; +} + +int main(void) +{ + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/12. FriendAndStaticMember/NestedClass.cpp b/12. FriendAndStaticMember/NestedClass.cpp index 72e3b6b..54f6891 100644 --- a/12. FriendAndStaticMember/NestedClass.cpp +++ b/12. FriendAndStaticMember/NestedClass.cpp @@ -1,35 +1,35 @@ -#include -using namespace std; -//Used in Linked List -class Outer -{ -public: - int a = 10; - static int b; - void fun() - { - i.show(); - cout << i.x << endl; - } - class Inner - { - public: - int x = 25; - void show() - { - /* - cout << a << endl; //Not Allowed - cout << b << endl; //Allowed - */ - cout << "Show of Inner" << endl; - } - }; - Inner i; -}; - -int main(void) -{ - - return 0; -} +#include +using namespace std; +//Used in Linked List +class Outer +{ +public: + int a = 10; + static int b; + void fun() + { + i.show(); + cout << i.x << endl; + } + class Inner + { + public: + int x = 25; + void show() + { + /* + cout << a << endl; //Not Allowed + cout << b << endl; //Allowed + */ + cout << "Show of Inner" << endl; + } + }; + Inner i; +}; + +int main(void) +{ + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/12. FriendAndStaticMember/StaticMember.cpp b/12. FriendAndStaticMember/StaticMember.cpp index 2b532e1..f42a0c0 100644 --- a/12. FriendAndStaticMember/StaticMember.cpp +++ b/12. FriendAndStaticMember/StaticMember.cpp @@ -1,29 +1,29 @@ -#include -using namespace std; - -class Test -{ -public: - int a; - static int count; //Static member variable Declaration - Test() - { - a = 10; - count++; - } - static int getCount() //Static Member Function - { - /*a++; //Not Accessable*/ - return count; - } -}; - -int Test::count = 0; //Initialization - -int main() -{ - Test t1, t2; - cout << Test::getCount() << endl; //Access upon class name - cout << t1.getCount() << endl; //Access upon object -} +#include +using namespace std; + +class Test +{ +public: + int a; + static int count; //Static member variable Declaration + Test() + { + a = 10; + count++; + } + static int getCount() //Static Member Function + { + /*a++; //Not Accessable*/ + return count; + } +}; + +int Test::count = 0; //Initialization + +int main() +{ + Test t1, t2; + cout << Test::getCount() << endl; //Access upon class name + cout << t1.getCount() << endl; //Access upon object +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/2. ConditionalStatements/BillAmount.cpp b/2. ConditionalStatements/BillAmount.cpp index 204f91a..64c8a1c 100644 --- a/2. ConditionalStatements/BillAmount.cpp +++ b/2. ConditionalStatements/BillAmount.cpp @@ -1,25 +1,25 @@ -// Program to Calculate Discounted Bill Amount -#include -using namespace std; -int main() -{ - float billAmount; - float discount = 0.0; - cout << "Enter Bill Amount:"; - cin >> billAmount; - - if (billAmount >= 500) - { - discount = billAmount * 20 / 100; - } - else if (billAmount >= 100 && billAmount < 500) - { - discount = billAmount * 10 / 100; - } - - cout << "Bill Amount is:" << billAmount << endl; - cout << "Discount is :" << discount << endl; - cout << "Discounted Amount is:" << billAmountdiscount << endl; - - return 0; +// Program to Calculate Discounted Bill Amount +#include +using namespace std; +int main() +{ + float billAmount; + float discount = 0.0; + cout << "Enter Bill Amount:"; + cin >> billAmount; + + if (billAmount >= 500) + { + discount = billAmount * 20 / 100; + } + else if (billAmount >= 100 && billAmount < 500) + { + discount = billAmount * 10 / 100; + } + + cout << "Bill Amount is:" << billAmount << endl; + cout << "Discount is :" << discount << endl; + cout << "Discounted Amount is:" << billAmountdiscount << endl; + + return 0; } \ No newline at end of file diff --git a/2. ConditionalStatements/CompoundConditional.cpp b/2. ConditionalStatements/CompoundConditional.cpp index a5eb592..7e56ede 100644 --- a/2. ConditionalStatements/CompoundConditional.cpp +++ b/2. ConditionalStatements/CompoundConditional.cpp @@ -1,27 +1,27 @@ -// Program to Demonstrate Compound Conditional Statement -#include -using namespace std; -int main() -{ - int age; - cout << "Enter your age: " << endl; - cin >> age; - if (age >= 12 && age <= 50) - { - cout << "Young" << endl; - } - else - { - cout << "Not Young" << endl; - } - if (age < 12 || age > 50) - { - cout << "Eligible for the offer" << endl; - } - else - { - cout << "Not eligible for the offer" << endl; - } - - return 0; +// Program to Demonstrate Compound Conditional Statement +#include +using namespace std; +int main() +{ + int age; + cout << "Enter your age: " << endl; + cin >> age; + if (age >= 12 && age <= 50) + { + cout << "Young" << endl; + } + else + { + cout << "Not Young" << endl; + } + if (age < 12 || age > 50) + { + cout << "Eligible for the offer" << endl; + } + else + { + cout << "Not eligible for the offer" << endl; + } + + return 0; } \ No newline at end of file diff --git a/2. ConditionalStatements/DayofWeek.cpp b/2. ConditionalStatements/DayofWeek.cpp index 333ece0..261863f 100644 --- a/2. ConditionalStatements/DayofWeek.cpp +++ b/2. ConditionalStatements/DayofWeek.cpp @@ -1,35 +1,35 @@ -// Menu Driven Program using Switch Case -#include -using namespace std; -int main() -{ - cout << "Menu" << endl; - cout << "1. Add\n" - << "2. Sub\n" - << "3. Mul\n" - << "4. Div\n"; - int option; - cout << "Enter your choice no." << endl; - cin >> option; - int a, b, c; - cout << "Enter two numbers" << endl; - cin >> a >> b; - switch (option) - { - case 1: - c = a + b; - break; - case 2: - c = a - b; - break; - case 3: - c = a * b; - break; - case 4: - c = a / b; - break; - } - cout << c << endl; - - return 0; +// Menu Driven Program using Switch Case +#include +using namespace std; +int main() +{ + cout << "Menu" << endl; + cout << "1. Add\n" + << "2. Sub\n" + << "3. Mul\n" + << "4. Div\n"; + int option; + cout << "Enter your choice no." << endl; + cin >> option; + int a, b, c; + cout << "Enter two numbers" << endl; + cin >> a >> b; + switch (option) + { + case 1: + c = a + b; + break; + case 2: + c = a - b; + break; + case 3: + c = a * b; + break; + case 4: + c = a / b; + break; + } + cout << c << endl; + + return 0; } \ No newline at end of file diff --git a/2. ConditionalStatements/DynamicDeclaration.cpp b/2. ConditionalStatements/DynamicDeclaration.cpp index 8cfd804..511070b 100644 --- a/2. ConditionalStatements/DynamicDeclaration.cpp +++ b/2. ConditionalStatements/DynamicDeclaration.cpp @@ -1,24 +1,24 @@ -// Dynamic Declaration -#include -using namespace std; -int main() -{ - int a = 10, b = 5; - if (true) - { - int c = a + b; - cout << c << endl; - } - { - //Variable go out of scope. - int d = a - b; - if (true) - { - cout << d << endl; - } - } - if (int e = a * b) - { - cout << e << endl; - } +// Dynamic Declaration +#include +using namespace std; +int main() +{ + int a = 10, b = 5; + if (true) + { + int c = a + b; + cout << c << endl; + } + { + //Variable go out of scope. + int d = a - b; + if (true) + { + cout << d << endl; + } + } + if (int e = a * b) + { + cout << e << endl; + } } \ No newline at end of file diff --git a/2. ConditionalStatements/LeapYear.cpp b/2. ConditionalStatements/LeapYear.cpp index d6a04bc..6c8dfbb 100644 --- a/2. ConditionalStatements/LeapYear.cpp +++ b/2. ConditionalStatements/LeapYear.cpp @@ -1,34 +1,34 @@ -// Program to to check if its a Leap Year -#include -using namespace std; -int main() -{ - int year; - cout << "Enter a year: "; - cin >> year; - - if (year % 4 == 0) - { - if (year % 100 == 0) - { - if (year % 400 == 0) - { - cout << year << " is a leap year."; - } - else - { - cout << year << " is not a leap year."; - } - } - else - { - cout << year << " is a leap year."; - } - } - else - { - cout << year << " is not a leap year."; - } - - return 0; +// Program to to check if its a Leap Year +#include +using namespace std; +int main() +{ + int year; + cout << "Enter a year: "; + cin >> year; + + if (year % 4 == 0) + { + if (year % 100 == 0) + { + if (year % 400 == 0) + { + cout << year << " is a leap year."; + } + else + { + cout << year << " is not a leap year."; + } + } + else + { + cout << year << " is a leap year."; + } + } + else + { + cout << year << " is not a leap year."; + } + + return 0; } \ No newline at end of file diff --git a/2. ConditionalStatements/MaxAmong2No.cpp b/2. ConditionalStatements/MaxAmong2No.cpp index a8a9880..66ecf32 100644 --- a/2. ConditionalStatements/MaxAmong2No.cpp +++ b/2. ConditionalStatements/MaxAmong2No.cpp @@ -1,22 +1,22 @@ -#include -using namespace std; -// Program to find maximum among 2 numbers -int main(void) -{ - int x, y; - - cout << "Enter 2 Numbers: "; - cin >> x >> y; - - if (x > y) - { - cout << "Max is " << x << endl; - } - else - { - cout << "Max is " << y << endl; - } - - return 0; -} +#include +using namespace std; +// Program to find maximum among 2 numbers +int main(void) +{ + int x, y; + + cout << "Enter 2 Numbers: "; + cin >> x >> y; + + if (x > y) + { + cout << "Max is " << x << endl; + } + else + { + cout << "Max is " << y << endl; + } + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/2. ConditionalStatements/MaxAmong3No.cpp b/2. ConditionalStatements/MaxAmong3No.cpp index eba5f3a..8aabbf4 100644 --- a/2. ConditionalStatements/MaxAmong3No.cpp +++ b/2. ConditionalStatements/MaxAmong3No.cpp @@ -1,26 +1,26 @@ -#include -using namespace std; -// Program to find maximum among 3 numbers -int main(void) -{ - int a, b, c; - - cout << "Enter 3 number: "; - cin >> a >> b >> c; - - if (a > b && a > c) - { - cout << a << " is greater" << endl; - } - else if (b > c) - { - cout << b << " is greater" << endl; - } - else - { - cout << c << " is greater" << endl; - } - - return 0; -} +#include +using namespace std; +// Program to find maximum among 3 numbers +int main(void) +{ + int a, b, c; + + cout << "Enter 3 number: "; + cin >> a >> b >> c; + + if (a > b && a > c) + { + cout << a << " is greater" << endl; + } + else if (b > c) + { + cout << b << " is greater" << endl; + } + else + { + cout << c << " is greater" << endl; + } + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/2. ConditionalStatements/NatureofQuadraticRoot.cpp b/2. ConditionalStatements/NatureofQuadraticRoot.cpp index 5190360..f75012b 100644 --- a/2. ConditionalStatements/NatureofQuadraticRoot.cpp +++ b/2. ConditionalStatements/NatureofQuadraticRoot.cpp @@ -1,30 +1,30 @@ -#include -#include -using namespace std; -//Program to check the nature of the roots of a quadratic equation. -int main(void) -{ - float a, b, c, d; - cout << "Enter a, b and c: "; - cin >> a >> b >> c; - - d = b * b - 4 * a * c; - if (d == 0) - { - cout << "Roots are Real & Equal." << endl; - cout << (-b / (2 * a)) << endl; - } - else if (d > 0) - { - cout << "Roots are Real & Unequal."; - cout << (-b + sqrt(d)) / (2 * a) << endl - << (-b - sqrt(d)) / (2 * a) << endl; - } - else - { - cout << "Imaginary" << endl; - } - - return 0; -} +#include +#include +using namespace std; +//Program to check the nature of the roots of a quadratic equation. +int main(void) +{ + float a, b, c, d; + cout << "Enter a, b and c: "; + cin >> a >> b >> c; + + d = b * b - 4 * a * c; + if (d == 0) + { + cout << "Roots are Real & Equal." << endl; + cout << (-b / (2 * a)) << endl; + } + else if (d > 0) + { + cout << "Roots are Real & Unequal."; + cout << (-b + sqrt(d)) / (2 * a) << endl + << (-b - sqrt(d)) / (2 * a) << endl; + } + else + { + cout << "Imaginary" << endl; + } + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/2. ConditionalStatements/ShortCircuit.cpp b/2. ConditionalStatements/ShortCircuit.cpp index c2d51a6..7f90216 100644 --- a/2. ConditionalStatements/ShortCircuit.cpp +++ b/2. ConditionalStatements/ShortCircuit.cpp @@ -1,17 +1,17 @@ -// Program to Demonstrate Short Circuit -#include -using namespace std; -int main() -{ - int a = 10, b = 5, i = 5; - if (a > b && ++i <= b) - { - } - cout << i << endl; - if (a < b || ++i <= b) - { - } - cout << i << endl; - - return 0; +// Program to Demonstrate Short Circuit +#include +using namespace std; +int main() +{ + int a = 10, b = 5, i = 5; + if (a > b && ++i <= b) + { + } + cout << i << endl; + if (a < b || ++i <= b) + { + } + cout << i << endl; + + return 0; } \ No newline at end of file diff --git a/2. ConditionalStatements/SwitchCase.cpp b/2. ConditionalStatements/SwitchCase.cpp index fc56b7d..d904e66 100644 --- a/2. ConditionalStatements/SwitchCase.cpp +++ b/2. ConditionalStatements/SwitchCase.cpp @@ -1,38 +1,38 @@ -// Program to Demonstrate switch case -#include -using namespace std; - -int main() -{ - int day; - cout << "Enter a day no. :" << endl; - cin >> day; - switch (day) - { - case 1: - cout << "Monday"; - break; - case 2: - cout << "Tuesday"; - break; - case 3: - cout << "Wednesday"; - break; - case 4: - cout << "Thursday"; - break; - case 5: - cout << "Friday"; - break; - case 6: - cout << "Saturday"; - break; - case 7: - cout << "Sunday"; - break; - default: - cout << "Invalid day no." << endl; - } - - return 0; +// Program to Demonstrate switch case +#include +using namespace std; + +int main() +{ + int day; + cout << "Enter a day no. :" << endl; + cin >> day; + switch (day) + { + case 1: + cout << "Monday"; + break; + case 2: + cout << "Tuesday"; + break; + case 3: + cout << "Wednesday"; + break; + case 4: + cout << "Thursday"; + break; + case 5: + cout << "Friday"; + break; + case 6: + cout << "Saturday"; + break; + case 7: + cout << "Sunday"; + break; + default: + cout << "Invalid day no." << endl; + } + + return 0; } \ No newline at end of file diff --git a/2. ConditionalStatements/Validation.cpp b/2. ConditionalStatements/Validation.cpp index 32ad815..3c22db6 100644 --- a/2. ConditionalStatements/Validation.cpp +++ b/2. ConditionalStatements/Validation.cpp @@ -1,20 +1,20 @@ -// Program to perform Validation -#include -using namespace std; -int main() -{ - int a, b, c; - cout << "Enter two numbers: " << endl; - cin >> a >> b; - if (b == 0) - { - cout << "Invalid denominator" << endl; - } - else - { - c = a / b; - cout << c << endl; - } - - return 0; +// Program to perform Validation +#include +using namespace std; +int main() +{ + int a, b, c; + cout << "Enter two numbers: " << endl; + cin >> a >> b; + if (b == 0) + { + cout << "Invalid denominator" << endl; + } + else + { + c = a / b; + cout << c << endl; + } + + return 0; } \ No newline at end of file diff --git a/3. Loops/AllLoops.cpp b/3. Loops/AllLoops.cpp index aead940..6f9bb83 100644 --- a/3. Loops/AllLoops.cpp +++ b/3. Loops/AllLoops.cpp @@ -1,34 +1,34 @@ -// Program to Test all Loops -#include -using namespace std; -//Printing Whole numbers less than 10 -int main() -{ - int a = 0; - //while loop: Entry Control Loop - while (a < 10) - { - cout << a << " "; - a++; - } - - cout << endl; - - int b = 0; - //do-while loop: Exit Control Loop - do - { - cout << b << " "; - b++; - } while (b < 10); - - cout << endl; - - //for loop: Counter Control Loop - for (int i = 0; i < 10; i++) - { - cout << i << " "; - } - - return 0; +// Program to Test all Loops +#include +using namespace std; +//Printing Whole numbers less than 10 +int main() +{ + int a = 0; + //while loop: Entry Control Loop + while (a < 10) + { + cout << a << " "; + a++; + } + + cout << endl; + + int b = 0; + //do-while loop: Exit Control Loop + do + { + cout << b << " "; + b++; + } while (b < 10); + + cout << endl; + + //for loop: Counter Control Loop + for (int i = 0; i < 10; i++) + { + cout << i << " "; + } + + return 0; } \ No newline at end of file diff --git a/3. Loops/AmstrongNumber.cpp b/3. Loops/AmstrongNumber.cpp index 9bad7c7..c1301f2 100644 --- a/3. Loops/AmstrongNumber.cpp +++ b/3. Loops/AmstrongNumber.cpp @@ -1,31 +1,31 @@ -//Amstrong Number: The sum of the cube of the digits of the number is Amstrong number. -#include -using namespace std; - -int main(void) -{ - int n, r, sum = 0; - cout << "Enter Number: "; - cin >> n; - - int original_n = n; //Preserving original input number for future refernce in program - - while (n != 0) - { - r = n % 10; - n = n / 10; - sum += (r * r * r); // sum = sum + r^3; - } - //Comparing the sum with input number - if (sum == original_n) - { - cout << "Amstrong Number." << endl; - } - else - { - cout << "Not Amstrong." << endl; - } - - return 0; -} +//Amstrong Number: The sum of the cube of the digits of the number is Amstrong number. +#include +using namespace std; + +int main(void) +{ + int n, r, sum = 0; + cout << "Enter Number: "; + cin >> n; + + int original_n = n; //Preserving original input number for future refernce in program + + while (n != 0) + { + r = n % 10; + n = n / 10; + sum += (r * r * r); // sum = sum + r^3; + } + //Comparing the sum with input number + if (sum == original_n) + { + cout << "Amstrong Number." << endl; + } + else + { + cout << "Not Amstrong." << endl; + } + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/3. Loops/Factorial.cpp b/3. Loops/Factorial.cpp index c38dc24..0f14f7f 100644 --- a/3. Loops/Factorial.cpp +++ b/3. Loops/Factorial.cpp @@ -1,20 +1,20 @@ -#include -using namespace std; -//Program to print the factorial of a number -int main(void) -{ - int n, factorial = 1; - - cout << "Enter number to find factorial: "; - cin >> n; - - for (int i = 1; i <= n; i++) - { - factorial *= i; - } - - cout << "Factorial of " << n << " is " << factorial << endl; - - return 0; -} +#include +using namespace std; +//Program to print the factorial of a number +int main(void) +{ + int n, factorial = 1; + + cout << "Enter number to find factorial: "; + cin >> n; + + for (int i = 1; i <= n; i++) + { + factorial *= i; + } + + cout << "Factorial of " << n << " is " << factorial << endl; + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/3. Loops/Factors.cpp b/3. Loops/Factors.cpp index fa28a84..b12a04a 100644 --- a/3. Loops/Factors.cpp +++ b/3. Loops/Factors.cpp @@ -1,19 +1,19 @@ -// Program to find Factors of a Number -#include -using namespace std; -int main() -{ - int n; - cout << "Enter a no. "; - cin >> n; - - for (int i = 1; i <= n; i++) - { - if (n % i == 0) - { - cout << i << " "; - } - } - - return 0; +// Program to find Factors of a Number +#include +using namespace std; +int main() +{ + int n; + cout << "Enter a no. "; + cin >> n; + + for (int i = 1; i <= n; i++) + { + if (n % i == 0) + { + cout << i << " "; + } + } + + return 0; } \ No newline at end of file diff --git a/3. Loops/GCDofNumber.cpp b/3. Loops/GCDofNumber.cpp index df118d6..3447836 100644 --- a/3. Loops/GCDofNumber.cpp +++ b/3. Loops/GCDofNumber.cpp @@ -1,26 +1,26 @@ -// Program to find GCD -#include -using namespace std; - -int main() -{ - int m, n; - cout << "Enter two no.s " << endl; - cin >> m >> n; - - while (m != n) - { - if (m > n) - { - m = m - n; - } - else - { - n = n - m; - } - } - - cout << "GCD is " << m; - - return 0; +// Program to find GCD +#include +using namespace std; + +int main() +{ + int m, n; + cout << "Enter two no.s " << endl; + cin >> m >> n; + + while (m != n) + { + if (m > n) + { + m = m - n; + } + else + { + n = n - m; + } + } + + cout << "GCD is " << m; + + return 0; } \ No newline at end of file diff --git a/3. Loops/MultiplicationTable.cpp b/3. Loops/MultiplicationTable.cpp index f09bb37..24ccd10 100644 --- a/3. Loops/MultiplicationTable.cpp +++ b/3. Loops/MultiplicationTable.cpp @@ -1,19 +1,19 @@ -#include -using namespace std; -//Program to print the multiplication table of a given number -int main(void) -{ - int n; - cout << "Enter the number which Multiplication Table you want? "; - cin >> n; - - for (int i = 1; i <= 10; i++) - { - cout << n << " x " << i << " = " << (n * i) << endl; - } - - cout << endl; - - return 0; -} +#include +using namespace std; +//Program to print the multiplication table of a given number +int main(void) +{ + int n; + cout << "Enter the number which Multiplication Table you want? "; + cin >> n; + + for (int i = 1; i <= 10; i++) + { + cout << n << " x " << i << " = " << (n * i) << endl; + } + + cout << endl; + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/3. Loops/PalindromeNumber.cpp b/3. Loops/PalindromeNumber.cpp index ab062bc..745cae1 100644 --- a/3. Loops/PalindromeNumber.cpp +++ b/3. Loops/PalindromeNumber.cpp @@ -1,31 +1,31 @@ -//Program to check given number is palindrom or not. -#include -using namespace std; -//Palindrome Number: If the reverse of a number is same as original number is called palindrome. -int main(void) -{ - int n, r, reverse; - cout << "Enter Number: "; - cin >> n; - - int original_n = n; - - while (n != 0) - { - r = n % 10; - n = n / 10; //updating the n - reverse = reverse * 10 + r; - } - //Checking Palindrome or not - if (original_n == reverse) - { - cout << "Palindrome Number" << endl; - } - else - { - cout << "Not Palindrome" << endl; - } - - return 0; -} +//Program to check given number is palindrom or not. +#include +using namespace std; +//Palindrome Number: If the reverse of a number is same as original number is called palindrome. +int main(void) +{ + int n, r, reverse; + cout << "Enter Number: "; + cin >> n; + + int original_n = n; + + while (n != 0) + { + r = n % 10; + n = n / 10; //updating the n + reverse = reverse * 10 + r; + } + //Checking Palindrome or not + if (original_n == reverse) + { + cout << "Palindrome Number" << endl; + } + else + { + cout << "Not Palindrome" << endl; + } + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/3. Loops/PerfectNumber.cpp b/3. Loops/PerfectNumber.cpp index 586c346..46b6801 100644 --- a/3. Loops/PerfectNumber.cpp +++ b/3. Loops/PerfectNumber.cpp @@ -1,31 +1,31 @@ -// Program to find perfect number -#include -using namespace std; -//Perfect No: If the sum of the factors of a number is equal to the double of the number. -int main() -{ - int n, sum = 0; - cout << "Enter a no. "; - cin >> n; - - for (int i = 1; i <= n; i++) - { - if (n % i == 0) - { - sum += i; - } - } - // cout << "Sum of factors = " << sum << endl; - - //Checking whether the number is Perfect is not. - if (2 * n == sum) - { - cout << "\nPerfect Number." << endl; - } - else - { - cout << "\nNot Perfect Number." << endl; - } - - return 0; +// Program to find perfect number +#include +using namespace std; +//Perfect No: If the sum of the factors of a number is equal to the double of the number. +int main() +{ + int n, sum = 0; + cout << "Enter a no. "; + cin >> n; + + for (int i = 1; i <= n; i++) + { + if (n % i == 0) + { + sum += i; + } + } + // cout << "Sum of factors = " << sum << endl; + + //Checking whether the number is Perfect is not. + if (2 * n == sum) + { + cout << "\nPerfect Number." << endl; + } + else + { + cout << "\nNot Perfect Number." << endl; + } + + return 0; } \ No newline at end of file diff --git a/3. Loops/PrimeNumber.cpp b/3. Loops/PrimeNumber.cpp index 7236f60..567b273 100644 --- a/3. Loops/PrimeNumber.cpp +++ b/3. Loops/PrimeNumber.cpp @@ -1,36 +1,36 @@ -//Program to check Prime Number. -#include -using namespace std; - -int main(void) -{ - int n; - bool flag; - // int count = 0; - - cout << "Enter Number: "; - cin >> n; - - for (int i = 1; i <= n; i++) - { - if (n % i == 0) - { - flag = true; - // count++; - } - } - - // (count == 2) ? (cout << "Prime" << endl) : (cout << "Not Prime" << endl); - - if (flag == true) - { - cout << "Prime Number" << endl; - } - else - { - cout << "Not Prime" << endl; - } - - return 0; -} +//Program to check Prime Number. +#include +using namespace std; + +int main(void) +{ + int n; + bool flag; + // int count = 0; + + cout << "Enter Number: "; + cin >> n; + + for (int i = 1; i <= n; i++) + { + if (n % i == 0) + { + flag = true; + // count++; + } + } + + // (count == 2) ? (cout << "Prime" << endl) : (cout << "Not Prime" << endl); + + if (flag == true) + { + cout << "Prime Number" << endl; + } + else + { + cout << "Not Prime" << endl; + } + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/3. Loops/ReverseANo.cpp b/3. Loops/ReverseANo.cpp index 9f147bd..0dff10e 100644 --- a/3. Loops/ReverseANo.cpp +++ b/3. Loops/ReverseANo.cpp @@ -1,21 +1,21 @@ -//Program to reverse the input number -#include -using namespace std; - -int main(void) -{ - int n, r, reverse; - cout << "Enter Number: "; - cin >> n; - - while (n != 0) - { - r = n % 10; - n = n / 10; //updating the n - reverse = reverse * 10 + r; - } - - cout << "Reverse Number is = " << reverse << endl; - return 0; -} +//Program to reverse the input number +#include +using namespace std; + +int main(void) +{ + int n, r, reverse; + cout << "Enter Number: "; + cin >> n; + + while (n != 0) + { + r = n % 10; + n = n / 10; //updating the n + reverse = reverse * 10 + r; + } + + cout << "Reverse Number is = " << reverse << endl; + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/3. Loops/ReverseDigitofNo.cpp b/3. Loops/ReverseDigitofNo.cpp index c9596e2..2e97f04 100644 --- a/3. Loops/ReverseDigitofNo.cpp +++ b/3. Loops/ReverseDigitofNo.cpp @@ -1,19 +1,19 @@ -#include -using namespace std; -//Printing the digits of the input number in reverse order -int main(void) -{ - int n, r; - cout << "Enter Number: "; - cin >> n; - - while (n != 0) - { - r = n % 10; - cout << r << endl; //Printing every time the remainder - n = n / 10; //updating the n - } - - return 0; -} +#include +using namespace std; +//Printing the digits of the input number in reverse order +int main(void) +{ + int n, r; + cout << "Enter Number: "; + cin >> n; + + while (n != 0) + { + r = n % 10; + cout << r << endl; //Printing every time the remainder + n = n / 10; //updating the n + } + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/3. Loops/SumofNaturalNo.cpp b/3. Loops/SumofNaturalNo.cpp index 07b9fbe..ddcac79 100644 --- a/3. Loops/SumofNaturalNo.cpp +++ b/3. Loops/SumofNaturalNo.cpp @@ -1,20 +1,20 @@ -#include -using namespace std; -//Program to print the sum of n natural number using loop -int main(void) -{ - int n, sum = 0; - - cout << "Enter number: "; - cin >> n; - - for (int i = 0; i < n; i++) - { - sum += i; - } - - cout << "Sum of " << n << " natural no is " << sum << endl; - - return 0; -} +#include +using namespace std; +//Program to print the sum of n natural number using loop +int main(void) +{ + int n, sum = 0; + + cout << "Enter number: "; + cin >> n; + + for (int i = 0; i < n; i++) + { + sum += i; + } + + cout << "Sum of " << n << " natural no is " << sum << endl; + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/4. Arrays/2D-Arrays.cpp b/4. Arrays/2D-Arrays.cpp index 4070a84..86d5a10 100644 --- a/4. Arrays/2D-Arrays.cpp +++ b/4. Arrays/2D-Arrays.cpp @@ -1,19 +1,19 @@ -#include -using namespace std; -//Cpp Program to demonstrate 2D-Arrays -int main(void) -{ - int arr[2][3] = {2, 5, 9, 6, 9, 15}; - //Traversal - for (int i = 0; i < 2; i++) //No of rows - { - for (int j = 0; j < 3; j++) //No of column - { - cout << arr[i][j] << " "; - } - cout << endl; - } - - return 0; -} +#include +using namespace std; +//Cpp Program to demonstrate 2D-Arrays +int main(void) +{ + int arr[2][3] = {2, 5, 9, 6, 9, 15}; + //Traversal + for (int i = 0; i < 2; i++) //No of rows + { + for (int j = 0; j < 3; j++) //No of column + { + cout << arr[i][j] << " "; + } + cout << endl; + } + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/4. Arrays/AverageofNumbers.cpp b/4. Arrays/AverageofNumbers.cpp index 435acbe..2d38395 100644 --- a/4. Arrays/AverageofNumbers.cpp +++ b/4. Arrays/AverageofNumbers.cpp @@ -1,22 +1,22 @@ -// Program to Find Average of Numbers -#include -using namespace std; -int main() -{ - int n, i; - float num[100], sum = 0.0, average; - - cout << "Enter the numbers of elements: "; - cin >> n; - - for (i = 0; i < n; ++i) - { - cout << i + 1 << ". Enter number: "; - cin >> num[i]; - sum += num[i]; - } - average = sum / n; - cout << "Average = " << average; - - return 0; +// Program to Find Average of Numbers +#include +using namespace std; +int main() +{ + int n, i; + float num[100], sum = 0.0, average; + + cout << "Enter the numbers of elements: "; + cin >> n; + + for (i = 0; i < n; ++i) + { + cout << i + 1 << ". Enter number: "; + cin >> num[i]; + sum += num[i]; + } + average = sum / n; + cout << "Average = " << average; + + return 0; } \ No newline at end of file diff --git a/4. Arrays/ForEachLoop.cpp b/4. Arrays/ForEachLoop.cpp index cc48227..117b89a 100644 --- a/4. Arrays/ForEachLoop.cpp +++ b/4. Arrays/ForEachLoop.cpp @@ -1,22 +1,22 @@ -#include -using namespace std; - -int main(void) -{ - int arr[] = {48, 84, 35, 84, 15, 35, 84}; - //Printing elements of the array using for loop - for (int i = 0; i < 7; i++) - { - cout << arr[i] << " "; - } - cout << endl; - - //Printing elements of the array using for-each loop, introduced in C++11 - for (int i : arr) - { - cout << i << " "; - } - - return 0; -} +#include +using namespace std; + +int main(void) +{ + int arr[] = {48, 84, 35, 84, 15, 35, 84}; + //Printing elements of the array using for loop + for (int i = 0; i < 7; i++) + { + cout << arr[i] << " "; + } + cout << endl; + + //Printing elements of the array using for-each loop, introduced in C++11 + for (int i : arr) + { + cout << i << " "; + } + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/4. Arrays/LinearSearch.cpp b/4. Arrays/LinearSearch.cpp index c9c6339..82c60b6 100644 --- a/4. Arrays/LinearSearch.cpp +++ b/4. Arrays/LinearSearch.cpp @@ -1,23 +1,23 @@ -// Program for Linear Search -#include -using namespace std; - -int main() -{ - int A[10] = {2, 4, 6, 8, 12, 3, 5, 7, 9}; - int key; - cout << "Enter a Key element "; - cin >> key; - - for (int i = 0; i < 10; i++) - { - if (key == A[i]) - { - cout << "The Key element is found at " << i << endl; - exit(0); - } - } - cout << "Key element not found" << endl; - - return 0; +// Program for Linear Search +#include +using namespace std; + +int main() +{ + int A[10] = {2, 4, 6, 8, 12, 3, 5, 7, 9}; + int key; + cout << "Enter a Key element "; + cin >> key; + + for (int i = 0; i < 10; i++) + { + if (key == A[i]) + { + cout << "The Key element is found at " << i << endl; + exit(0); + } + } + cout << "Key element not found" << endl; + + return 0; } \ No newline at end of file diff --git a/4. Arrays/MatrixAddition.cpp b/4. Arrays/MatrixAddition.cpp index 5d8b2d8..eb0a476 100644 --- a/4. Arrays/MatrixAddition.cpp +++ b/4. Arrays/MatrixAddition.cpp @@ -1,30 +1,30 @@ -#include -using namespace std; - -int main(void) -{ - int A[2][3] = {2, 5, 9, 6, 9, 15}; - int B[2][3] = {6, 9, 15, , 12, 17}; - - int C[2][3]; - //Adding Matrix A and Matrix B - for (int i = 0; i < 2; i++) - { - for (int j = 0; j < 3; j++) - { - C[i][j] = A[i][j] + B[i][j]; - } - } - //Printing Matrix C - for (int i = 0; i < 2; i++) - { - for (int j = 0; j < 3; j++) - { - cout << C[i][j] << " "; - } - cout << endl; - } - - return 0; -} +#include +using namespace std; + +int main(void) +{ + int A[2][3] = {2, 5, 9, 6, 9, 15}; + int B[2][3] = {6, 9, 15, , 12, 17}; + + int C[2][3]; + //Adding Matrix A and Matrix B + for (int i = 0; i < 2; i++) + { + for (int j = 0; j < 3; j++) + { + C[i][j] = A[i][j] + B[i][j]; + } + } + //Printing Matrix C + for (int i = 0; i < 2; i++) + { + for (int j = 0; j < 3; j++) + { + cout << C[i][j] << " "; + } + cout << endl; + } + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/4. Arrays/MatrixMultiplication.cpp b/4. Arrays/MatrixMultiplication.cpp index a615176..7590c02 100644 --- a/4. Arrays/MatrixMultiplication.cpp +++ b/4. Arrays/MatrixMultiplication.cpp @@ -1,57 +1,57 @@ -// Program to Multiply Matrices -#include -using namespace std; -int main() -{ - int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k; - cout << "Enter rows and columns for first matrix: "; - cin >> r1 >> c1; - cout << "Enter rows and columns for second matrix: "; - cin >> r2 >> c2; - - if (c1 != r2) - { - cout <<“Cant be Multiplied”; - return 0; - } - // Storing elements of first matrix. - cout << endl - << "Enter elements of matrix 1:" << endl; - for (i = 0; i < r1; ++i) - for (j = 0; j < c1; ++j) - { - cout << "Enter element a" << i + 1 << j + 1 << " : "; - cin >> a[i][j]; - } - // Storing elements of second matrix. - cout << endl - << "Enter elements of matrix 2:" << endl; - for (i = 0; i < r2; ++i) - for (j = 0; j < c2; ++j) - { - cout << "Enter element b" << i + 1 << j + 1 << " : "; - cin >> b[i][j]; - } - // Multiplying matrix a and b and storing in array mult. - for (i = 0; i < r1; ++i) - for (j = 0; j < c2; ++j) - { - mult[i][j] = 0; - for (k = 0; k < c1; ++k) - { - mult[i][j] += a[i][k] * b[k][j]; - } - } - // Displaying the multiplication of two matrix. - cout << endl - << "Output Matrix: " << endl; - for (i = 0; i < r1; ++i) - for (j = 0; j < c2; ++j) - { - cout << " " << mult[i][j]; - if (j == c2 - 1) - cout << endl; - } - - return 0; +// Program to Multiply Matrices +#include +using namespace std; +int main() +{ + int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k; + cout << "Enter rows and columns for first matrix: "; + cin >> r1 >> c1; + cout << "Enter rows and columns for second matrix: "; + cin >> r2 >> c2; + + if (c1 != r2) + { + cout <<“Cant be Multiplied”; + return 0; + } + // Storing elements of first matrix. + cout << endl + << "Enter elements of matrix 1:" << endl; + for (i = 0; i < r1; ++i) + for (j = 0; j < c1; ++j) + { + cout << "Enter element a" << i + 1 << j + 1 << " : "; + cin >> a[i][j]; + } + // Storing elements of second matrix. + cout << endl + << "Enter elements of matrix 2:" << endl; + for (i = 0; i < r2; ++i) + for (j = 0; j < c2; ++j) + { + cout << "Enter element b" << i + 1 << j + 1 << " : "; + cin >> b[i][j]; + } + // Multiplying matrix a and b and storing in array mult. + for (i = 0; i < r1; ++i) + for (j = 0; j < c2; ++j) + { + mult[i][j] = 0; + for (k = 0; k < c1; ++k) + { + mult[i][j] += a[i][k] * b[k][j]; + } + } + // Displaying the multiplication of two matrix. + cout << endl + << "Output Matrix: " << endl; + for (i = 0; i < r1; ++i) + for (j = 0; j < c2; ++j) + { + cout << " " << mult[i][j]; + if (j == c2 - 1) + cout << endl; + } + + return 0; } \ No newline at end of file diff --git a/4. Arrays/MaxElement.cpp b/4. Arrays/MaxElement.cpp index f107c84..f8bbf5a 100644 --- a/4. Arrays/MaxElement.cpp +++ b/4. Arrays/MaxElement.cpp @@ -1,21 +1,21 @@ -// Program to find maximum element from an Array -#include -using namespace std; - -int main() -{ - int A[10] = {2, 4, 6, 8, 12, 3, 5, 7, 9, 11}; - int max = INT_MIN; - - for (int i = 0; i <= 10; i++) - { - if (A[i] > max) - { - max = A[i]; - } - } - - cout << max << endl; - - return 0; +// Program to find maximum element from an Array +#include +using namespace std; + +int main() +{ + int A[10] = {2, 4, 6, 8, 12, 3, 5, 7, 9, 11}; + int max = INT_MIN; + + for (int i = 0; i <= 10; i++) + { + if (A[i] > max) + { + max = A[i]; + } + } + + cout << max << endl; + + return 0; } \ No newline at end of file diff --git a/4. Arrays/NesteForLoops.cpp b/4. Arrays/NesteForLoops.cpp index 708e069..88fd5f6 100644 --- a/4. Arrays/NesteForLoops.cpp +++ b/4. Arrays/NesteForLoops.cpp @@ -1,18 +1,18 @@ -#include -using namespace std; - -int main(void) -{ - for (int i = 1; i <= 5; i++) //Number of rows in a matrix - { - for (int j = 1; j <= 5; j++) //Number of column in the matrix - { - cout << "(" << i << "," << j << ")" - << " "; - } - cout << endl; - } - - return 0; -} +#include +using namespace std; + +int main(void) +{ + for (int i = 1; i <= 5; i++) //Number of rows in a matrix + { + for (int j = 1; j <= 5; j++) //Number of column in the matrix + { + cout << "(" << i << "," << j << ")" + << " "; + } + cout << endl; + } + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/4. Arrays/Pattern1.cpp b/4. Arrays/Pattern1.cpp index a113bc3..b8de132 100644 --- a/4. Arrays/Pattern1.cpp +++ b/4. Arrays/Pattern1.cpp @@ -1,27 +1,27 @@ -#include -using namespace std; -/* - -1 2 3 4 -5 6 7 8 -9 10 11 12 -13 14 15 16 - -*/ -int main(void) -{ - int count = 1; - - for (int i = 0; i < 4; i++) - { - for (int j = 0; j < 4; j++) - { - cout << count << "\t"; - count++; - } - cout << endl; - } - - return 0; -} +#include +using namespace std; +/* + +1 2 3 4 +5 6 7 8 +9 10 11 12 +13 14 15 16 + +*/ +int main(void) +{ + int count = 1; + + for (int i = 0; i < 4; i++) + { + for (int j = 0; j < 4; j++) + { + cout << count << "\t"; + count++; + } + cout << endl; + } + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/4. Arrays/Pattern2.cpp b/4. Arrays/Pattern2.cpp index 9124117..1a470f7 100644 --- a/4. Arrays/Pattern2.cpp +++ b/4. Arrays/Pattern2.cpp @@ -1,28 +1,28 @@ -#include -using namespace std; -/* - -* -* * -* * * -* * * * - -*/ -int main(void) -{ - for (int i = 0; i < 4; i++) - { - for (int j = 0; j < 4; j++) - { - if (i >= j) - { - cout << "*" - << "\t"; - } - } - cout << endl; - } - - return 0; -} +#include +using namespace std; +/* + +* +* * +* * * +* * * * + +*/ +int main(void) +{ + for (int i = 0; i < 4; i++) + { + for (int j = 0; j < 4; j++) + { + if (i >= j) + { + cout << "*" + << "\t"; + } + } + cout << endl; + } + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/4. Arrays/Pattern3.cpp b/4. Arrays/Pattern3.cpp index 97b9839..65f57bc 100644 --- a/4. Arrays/Pattern3.cpp +++ b/4. Arrays/Pattern3.cpp @@ -1,32 +1,32 @@ -#include -using namespace std; -/* - -* * * * - * * * - * * - * - -*/ -int main(void) -{ - for (int i = 0; i < 4; i++) - { - for (int j = 0; j < 4; j++) - { - if (i > j) - { - cout << " " - << "\t"; - } - else - { - cout << "*" - << "\t"; - } - } - cout << endl; - } - return 0; -} +#include +using namespace std; +/* + +* * * * + * * * + * * + * + +*/ +int main(void) +{ + for (int i = 0; i < 4; i++) + { + for (int j = 0; j < 4; j++) + { + if (i > j) + { + cout << " " + << "\t"; + } + else + { + cout << "*" + << "\t"; + } + } + cout << endl; + } + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/4. Arrays/Pattern4.cpp b/4. Arrays/Pattern4.cpp index 150765f..b716494 100644 --- a/4. Arrays/Pattern4.cpp +++ b/4. Arrays/Pattern4.cpp @@ -1,36 +1,36 @@ -#include -using namespace std; -/* - - * - * * - * * * -* * * * - -*/ -int main(void) -{ - int n; - cin >> n; - - for (int i = 0; i < n; i++) - { - for (int j = 0; j < n; j++) - { - if (i + j >= (n - 1)) - { - cout << "*" - << "\t"; - } - else - { - cout << " " - << "\t"; - } - } - cout << endl; - } - - return 0; -} +#include +using namespace std; +/* + + * + * * + * * * +* * * * + +*/ +int main(void) +{ + int n; + cin >> n; + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (i + j >= (n - 1)) + { + cout << "*" + << "\t"; + } + else + { + cout << " " + << "\t"; + } + } + cout << endl; + } + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/4. Arrays/SumofArrayElement.cpp b/4. Arrays/SumofArrayElement.cpp index 0050a47..c44d542 100644 --- a/4. Arrays/SumofArrayElement.cpp +++ b/4. Arrays/SumofArrayElement.cpp @@ -1,18 +1,18 @@ -// Program to find Sum of all elements in an Array -#include -using namespace std; - -int main() -{ - int A[10] = {2, 4, 6, 8, 12, 3, 5, 7, 9, 11}; - int sum = 0; - - for (int i = 0; i <= 10; i++) - { - sum = sum + A[i]; - } - - cout << "The sum is " << sum << endl; - - return 0; +// Program to find Sum of all elements in an Array +#include +using namespace std; + +int main() +{ + int A[10] = {2, 4, 6, 8, 12, 3, 5, 7, 9, 11}; + int sum = 0; + + for (int i = 0; i <= 10; i++) + { + sum = sum + A[i]; + } + + cout << "The sum is " << sum << endl; + + return 0; } \ No newline at end of file diff --git a/5. Pointers/PointerSyntax.cpp b/5. Pointers/PointerSyntax.cpp index ca4820c..78d6101 100644 --- a/5. Pointers/PointerSyntax.cpp +++ b/5. Pointers/PointerSyntax.cpp @@ -1,16 +1,16 @@ -// Program to Demonstrate Pointer Syntax -#include -using namespace std; - -int main() -{ - int a = 10; - int *p = &a; - - cout << a << endl; - cout << &a << endl; - cout << p << endl; - cout << *p << endl; - - return 0; +// Program to Demonstrate Pointer Syntax +#include +using namespace std; + +int main() +{ + int a = 10; + int *p = &a; + + cout << a << endl; + cout << &a << endl; + cout << p << endl; + cout << *p << endl; + + return 0; } \ No newline at end of file diff --git a/6. Strings/ChangingCase.cpp b/6. Strings/ChangingCase.cpp index 05a5389..c969dba 100644 --- a/6. Strings/ChangingCase.cpp +++ b/6. Strings/ChangingCase.cpp @@ -1,17 +1,17 @@ -#include -using namespace std; -/*program for changing cases of letters*/ -int main() -{ - - string str = "welcome"; - for (int i = 0; str[i] != '\0'; i++) - { - if (str[i] >= 65 && str[i] <= 90) - { - str[i] = str[i] + 32; - } - } - cout << str << endl; - return 0; +#include +using namespace std; +/*program for changing cases of letters*/ +int main() +{ + + string str = "welcome"; + for (int i = 0; str[i] != '\0'; i++) + { + if (str[i] >= 65 && str[i] <= 90) + { + str[i] = str[i] + 32; + } + } + cout << str << endl; + return 0; } \ No newline at end of file diff --git a/6. Strings/CountVowelConsonant.cpp b/6. Strings/CountVowelConsonant.cpp index 37648b4..7893847 100644 --- a/6. Strings/CountVowelConsonant.cpp +++ b/6. Strings/CountVowelConsonant.cpp @@ -1,20 +1,20 @@ -#include -using namespace std; -/*program for count vowels consonant and words*/ -int main() -{ - string str = "how many words" int vowels = 0, consonant = 0, space = 0; - for (int i = 0; str[i] = !'\0'; i++) - { - if (str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U' || str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u') - vowels++; - else if (str[i] == ' ') - space++; - else - consonant++; - } - cout << "vowels " << vowels << endl; - cout << "consonant " << consonant << endl; - cout << "words " << space++ << endl; - return 0; +#include +using namespace std; +/*program for count vowels consonant and words*/ +int main() +{ + string str = "how many words" int vowels = 0, consonant = 0, space = 0; + for (int i = 0; str[i] = !'\0'; i++) + { + if (str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U' || str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u') + vowels++; + else if (str[i] == ' ') + space++; + else + consonant++; + } + cout << "vowels " << vowels << endl; + cout << "consonant " << consonant << endl; + cout << "words " << space++ << endl; + return 0; } \ No newline at end of file diff --git a/6. Strings/LengthofString.cpp b/6. Strings/LengthofString.cpp index c9a5646..c359305 100644 --- a/6. Strings/LengthofString.cpp +++ b/6. Strings/LengthofString.cpp @@ -1,23 +1,23 @@ -#include -using namespace std; -int main() -{ - /*program for finding length of a string using for loop*/ - string s = "welcome"; - int count = 0; - for (int i = 0; s[i] != '\0'; i++) - { - cout << "length is" << count << endl; - } - - /*program for finding length of a string using string iterator*/ - string str = "welcome"; - string::iterator it; - int count = 0; - for (it = str.begin(); it != str.end(); it++) - { - cout << "length is" << count << endl; - } - - return 0; +#include +using namespace std; +int main() +{ + /*program for finding length of a string using for loop*/ + string s = "welcome"; + int count = 0; + for (int i = 0; s[i] != '\0'; i++) + { + cout << "length is" << count << endl; + } + + /*program for finding length of a string using string iterator*/ + string str = "welcome"; + string::iterator it; + int count = 0; + for (it = str.begin(); it != str.end(); it++) + { + cout << "length is" << count << endl; + } + + return 0; } \ No newline at end of file diff --git a/6. Strings/Palindrome.cpp b/6. Strings/Palindrome.cpp index ccb30ad..bfe5722 100644 --- a/6. Strings/Palindrome.cpp +++ b/6. Strings/Palindrome.cpp @@ -1,22 +1,22 @@ -#include -using namespace std; - -// program for reverse a string and check a string is a palidrome - -int main() -{ - string str = "MADAM"; - string rev = ""; - int len = (int)str.length(); - rev.resize(len); - for (int i = 0, j = len - 1; i < len; i++, j--) - { - rev[i] = str[j]; - } - rev[len] = '\0'; - if (str.compare(rev) == 0) - cout << "palindrome" << endl; - else - cout << "not a pallindrome" << endl; - return 0; +#include +using namespace std; + +// program for reverse a string and check a string is a palidrome + +int main() +{ + string str = "MADAM"; + string rev = ""; + int len = (int)str.length(); + rev.resize(len); + for (int i = 0, j = len - 1; i < len; i++, j--) + { + rev[i] = str[j]; + } + rev[len] = '\0'; + if (str.compare(rev) == 0) + cout << "palindrome" << endl; + else + cout << "not a pallindrome" << endl; + return 0; } \ No newline at end of file diff --git a/6. Strings/ReadWriteString.cpp b/6. Strings/ReadWriteString.cpp index fdbc331..b93e3a2 100644 --- a/6. Strings/ReadWriteString.cpp +++ b/6. Strings/ReadWriteString.cpp @@ -1,36 +1,36 @@ -#include -using namespace std; - -int main(void) -{ - /* - char str[20]; - - cout << "Enter your name: "; - cin >> s; - cout << "Hello! " << str << endl; - - char s[50]; - cout << "Enter your name: "; - // cin.get(s, 50); - cin.getline(s, 50); - cout << "Hello! " << s << endl; - */ - - //Problem with cin.get() - char a[50]; - cout << "Enter name: "; - cin.get(a, 50); - - cout << "Welcome " << a << endl; - - cin.ignore(); //Clears the input buffer - - cout << "Enter name again: "; - cin.getline(a, 50); - - cout << "Welcome " << a << endl; - - return 0; -} +#include +using namespace std; + +int main(void) +{ + /* + char str[20]; + + cout << "Enter your name: "; + cin >> s; + cout << "Hello! " << str << endl; + + char s[50]; + cout << "Enter your name: "; + // cin.get(s, 50); + cin.getline(s, 50); + cout << "Hello! " << s << endl; + */ + + //Problem with cin.get() + char a[50]; + cout << "Enter name: "; + cin.get(a, 50); + + cout << "Welcome " << a << endl; + + cin.ignore(); //Clears the input buffer + + cout << "Enter name again: "; + cin.getline(a, 50); + + cout << "Welcome " << a << endl; + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/6. Strings/StringToInteger.cpp b/6. Strings/StringToInteger.cpp index 2474aa9..c2423c6 100644 --- a/6. Strings/StringToInteger.cpp +++ b/6. Strings/StringToInteger.cpp @@ -1,22 +1,22 @@ -#include -using namespace std; - -int main(void) -{ - char s1[] = "235"; - char s2[] = "54.778"; - //String to long integer(string, NUll Character, Base 10) - long int x = strtol(s1, NULL, 10); - //String to float(string, NUll Character) - float y = strtof(s2, NULL); - - cout << x << " " << y << endl; - - x = x * 10; - y = y * 10; - - cout << x << " " << y << endl; - - return 0; -} +#include +using namespace std; + +int main(void) +{ + char s1[] = "235"; + char s2[] = "54.778"; + //String to long integer(string, NUll Character, Base 10) + long int x = strtol(s1, NULL, 10); + //String to float(string, NUll Character) + float y = strtof(s2, NULL); + + cout << x << " " << y << endl; + + x = x * 10; + y = y * 10; + + cout << x << " " << y << endl; + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/6. Strings/Tokenizing.cpp b/6. Strings/Tokenizing.cpp index 5a4964d..1ff8825 100644 --- a/6. Strings/Tokenizing.cpp +++ b/6. Strings/Tokenizing.cpp @@ -1,18 +1,18 @@ -#include -using namespace std; - -int main(void) -{ - char s1[20] = "x=10;y=20;z=30;"; - - char *token = strtok(s1, ";"); //Tokenizing - - while (token != NULL) - { - cout << token << " "; - token = strtok(NULL, ";"); - } - - return 0; -} +#include +using namespace std; + +int main(void) +{ + char s1[20] = "x=10;y=20;z=30;"; + + char *token = strtok(s1, ";"); //Tokenizing + + while (token != NULL) + { + cout << token << " "; + token = strtok(NULL, ";"); + } + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/6. Strings/UpperCase.cpp b/6. Strings/UpperCase.cpp index c42f4c5..46a1cfe 100644 --- a/6. Strings/UpperCase.cpp +++ b/6. Strings/UpperCase.cpp @@ -1,17 +1,17 @@ -#include -using namespace std; -/*program for change cases to upper case only*/ -int main() -{ - - string str = "wELcoMe7"; - for (int i = 0; str[i] != '\0'; i++) - { - if (str[i] >= 97 && str[i] <= 122) - { - str[i] = str[i] - 32; - } - } - cout << str << endl; - return 0; +#include +using namespace std; +/*program for change cases to upper case only*/ +int main() +{ + + string str = "wELcoMe7"; + for (int i = 0; str[i] != '\0'; i++) + { + if (str[i] >= 97 && str[i] <= 122) + { + str[i] = str[i] - 32; + } + } + cout << str << endl; + return 0; } \ No newline at end of file diff --git a/6. Strings/UserNameEmail.cpp b/6. Strings/UserNameEmail.cpp index cb03713..3d27c25 100644 --- a/6. Strings/UserNameEmail.cpp +++ b/6. Strings/UserNameEmail.cpp @@ -1,11 +1,11 @@ -#include -#include -using namespace std; -// program for finding user name from email address -int main() -{ - string email = "john123@gmail.com" int i = (int)email.find('@'); - string uname = email.substr(0, i); - cout << "user name is " << uname << endl; - return 0; +#include +#include +using namespace std; +// program for finding user name from email address +int main() +{ + string email = "john123@gmail.com" int i = (int)email.find('@'); + string uname = email.substr(0, i); + cout << "user name is " << uname << endl; + return 0; } \ No newline at end of file diff --git a/7. Functions/DefaultArgument.cpp b/7. Functions/DefaultArgument.cpp index 07c39fa..5889ed6 100644 --- a/7. Functions/DefaultArgument.cpp +++ b/7. Functions/DefaultArgument.cpp @@ -1,14 +1,14 @@ -// Example of Default Arguments -#include -using namespace std; - -int sum(int a, int b, int c = 0) -{ - return a + b + c; -} - -int main() -{ - cout << sum(10, 20, 3) << endl; - return 0; +// Example of Default Arguments +#include +using namespace std; + +int sum(int a, int b, int c = 0) +{ + return a + b + c; +} + +int main() +{ + cout << sum(10, 20, 3) << endl; + return 0; } \ No newline at end of file diff --git a/7. Functions/FunctionOverloading.cpp b/7. Functions/FunctionOverloading.cpp index 4b5b42b..ba67f91 100644 --- a/7. Functions/FunctionOverloading.cpp +++ b/7. Functions/FunctionOverloading.cpp @@ -1,26 +1,26 @@ -// Program to Demonstrate Function Overloading using Sum function -#include -using namespace std; - -int sum(int a, int b) -{ - return a + b; -} - -float sum(float a, float b) -{ - return a + b; -} - -int sum(int a, int b, int c) -{ - return a, b, c; -} - -int main() -{ - cout << sum(10, 5) << endl; - cout << sum(12.5f, 3.4f) << endl; - cout << sum(10, 20, 3) << endl; - return 0; +// Program to Demonstrate Function Overloading using Sum function +#include +using namespace std; + +int sum(int a, int b) +{ + return a + b; +} + +float sum(float a, float b) +{ + return a + b; +} + +int sum(int a, int b, int c) +{ + return a, b, c; +} + +int main() +{ + cout << sum(10, 5) << endl; + cout << sum(12.5f, 3.4f) << endl; + cout << sum(10, 20, 3) << endl; + return 0; } \ No newline at end of file diff --git a/7. Functions/FunctionTemplate.cpp b/7. Functions/FunctionTemplate.cpp index 1adecb3..84534bb 100644 --- a/7. Functions/FunctionTemplate.cpp +++ b/7. Functions/FunctionTemplate.cpp @@ -1,17 +1,17 @@ -// Example of Function Template -#include -using namespace std; -//Generic datatype -template -T maxim(T a, T b) -{ - return a > b ? a : b; -} - -int main() -{ - cout << maxim(12, 14) << endl; - cout << maxim(2.3, 1.4) << endl; - cout << maxim(2.3f, 5.6f) << endl; - return 0; +// Example of Function Template +#include +using namespace std; +//Generic datatype +template +T maxim(T a, T b) +{ + return a > b ? a : b; +} + +int main() +{ + cout << maxim(12, 14) << endl; + cout << maxim(2.3, 1.4) << endl; + cout << maxim(2.3f, 5.6f) << endl; + return 0; } \ No newline at end of file diff --git a/7. Functions/LinearSearch.cpp b/7. Functions/LinearSearch.cpp index 7b5aed2..0049b2a 100644 --- a/7. Functions/LinearSearch.cpp +++ b/7. Functions/LinearSearch.cpp @@ -1,27 +1,27 @@ -// Program for Linear Search using Functions -#include -using namespace std; - -int Search(int A[], int n, int key) -{ - for (int i = 0; i < n; i++) - { - if (key == A[i]) - { - return i; - } - } - return 0; -} - -int main() -{ - int A[] = {2, 4, 5, 7, 10, 9, 13}; - int k; - cout << "Enter an Element to be Searched:"; - cin >> k; - int index = Search(A, 7, k); - cout << "Element found at index :" << index << endl; - - return 0; +// Program for Linear Search using Functions +#include +using namespace std; + +int Search(int A[], int n, int key) +{ + for (int i = 0; i < n; i++) + { + if (key == A[i]) + { + return i; + } + } + return 0; +} + +int main() +{ + int A[] = {2, 4, 5, 7, 10, 9, 13}; + int k; + cout << "Enter an Element to be Searched:"; + cin >> k; + int index = Search(A, 7, k); + cout << "Element found at index :" << index << endl; + + return 0; } \ No newline at end of file diff --git a/7. Functions/ReturnAddress.cpp b/7. Functions/ReturnAddress.cpp index 8254c83..c4a1ad0 100644 --- a/7. Functions/ReturnAddress.cpp +++ b/7. Functions/ReturnAddress.cpp @@ -1,23 +1,23 @@ -#include -using namespace std; - -int *fun(int n) -{ - int *p = new int[n]; - for (int i = 0; i < n; i++) - { - p[i] = i + 1; - } - - return p; -} - -int main(void) -{ - int *ptr = fun(5); - for (int i = 0; i < 5; i++) - cout << i << endl; - - return 0; -} +#include +using namespace std; + +int *fun(int n) +{ + int *p = new int[n]; + for (int i = 0; i < n; i++) + { + p[i] = i + 1; + } + + return p; +} + +int main(void) +{ + int *ptr = fun(5); + for (int i = 0; i < 5; i++) + cout << i << endl; + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/7. Functions/ReturnReference.cpp b/7. Functions/ReturnReference.cpp index b8993eb..5207e66 100644 --- a/7. Functions/ReturnReference.cpp +++ b/7. Functions/ReturnReference.cpp @@ -1,17 +1,17 @@ -#include -using namespace std; - -int &fun(int &a) -{ - cout << a; - return a; -} - -int main(void) -{ - int x = 10; - fun(x) = 25; - cout << x; - return 0; -} +#include +using namespace std; + +int &fun(int &a) +{ + cout << a; + return a; +} + +int main(void) +{ + int x = 10; + fun(x) = 25; + cout << x; + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/7. Functions/StaticVariables.cpp b/7. Functions/StaticVariables.cpp index f780377..97de601 100644 --- a/7. Functions/StaticVariables.cpp +++ b/7. Functions/StaticVariables.cpp @@ -1,17 +1,17 @@ -#include -using namespace std; - -void fun() -{ - static int v = 0; - int a = 10; - v++ : cout << a <<“ “<< v; -} - -int main() -{ - fun(); - fun(); - fun(); - return 0; +#include +using namespace std; + +void fun() +{ + static int v = 0; + int a = 10; + v++ : cout << a <<“ “<< v; +} + +int main() +{ + fun(); + fun(); + fun(); + return 0; } \ No newline at end of file diff --git a/7. Functions/Sum.cpp b/7. Functions/Sum.cpp index f107e95..071ac0f 100644 --- a/7. Functions/Sum.cpp +++ b/7. Functions/Sum.cpp @@ -1,16 +1,16 @@ -#include -using namespace std; -//Function take two integers and returns the sum of them -int add(int a, int b) -{ - return (a + b); -} -//Driver Code -int main(void) -{ - int a = 5, b = 15, c; - c = add(a, b); //Function Call for add() - cout << "Sum is " << c << endl; - return 0; -} +#include +using namespace std; +//Function take two integers and returns the sum of them +int add(int a, int b) +{ + return (a + b); +} +//Driver Code +int main(void) +{ + int a = 5, b = 15, c; + c = add(a, b); //Function Call for add() + cout << "Sum is " << c << endl; + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/7. Functions/Swap.cpp b/7. Functions/Swap.cpp index 0b828a3..7250a5e 100644 --- a/7. Functions/Swap.cpp +++ b/7. Functions/Swap.cpp @@ -1,19 +1,19 @@ -#include -using namespace std; - -void swap(int *x, int *y) -{ - int temp; - temp = *x; - *x = *y; - *y = temp; -} - -int main(void) -{ - int a = 10, b = 20; - swap(&a, &b); - cout << a << b; - return 0; -} -// Code By: Avinash Kumar. +#include +using namespace std; + +void swap(int *x, int *y) +{ + int temp; + temp = *x; + *x = *y; + *y = temp; +} + +int main(void) +{ + int a = 10, b = 20; + swap(&a, &b); + cout << a << b; + return 0; +} +// Code By: Avinash Kumar. diff --git a/8. IntroductionToOPPs/Constructor.cpp b/8. IntroductionToOPPs/Constructor.cpp index 190a131..41ef647 100644 --- a/8. IntroductionToOPPs/Constructor.cpp +++ b/8. IntroductionToOPPs/Constructor.cpp @@ -1,82 +1,82 @@ -#include -using namespace std; -//Program to demonstrate Constructor -class Rectangle -{ -private: - int length; - int breadth; - -public: - /* - //Default Constructor - Rectangle() - { - } - */ - - //Non-Parameterized Constructor - Rectangle() - { - length = 0; - breadth = 0; - } - //Parametrized Constructor - Rectangle(int l, int b) - { - setLength(l); - setBreadth(b); - } - - //Mutators or Setters - void setLength(int l) - { - if (l > 0) - { - length = l; - } - else - { - length = 0; - } - } - void setBreadth(int b) - { - if (b > 0) - { - breadth = b; - } - else - { - breadth = 0; - } - } - //Accessors or Getters - int getLength() - { - return length; - } - int getBreadth() - { - return breadth; - } - //Facilatators - int area() - { - return (length * breadth); - } - int perimeter() - { - return (2 * (length + breadth)); - } -}; - -int main(void) -{ - Rectangle r1; //Default Constructor - Rectangle r2(); //Non-Parameterized Constructor - Rectangle r3(10, 5); //Parametrized Constructor - - return 0; -} +#include +using namespace std; +//Program to demonstrate Constructor +class Rectangle +{ +private: + int length; + int breadth; + +public: + /* + //Default Constructor + Rectangle() + { + } + */ + + //Non-Parameterized Constructor + Rectangle() + { + length = 0; + breadth = 0; + } + //Parametrized Constructor + Rectangle(int l, int b) + { + setLength(l); + setBreadth(b); + } + + //Mutators or Setters + void setLength(int l) + { + if (l > 0) + { + length = l; + } + else + { + length = 0; + } + } + void setBreadth(int b) + { + if (b > 0) + { + breadth = b; + } + else + { + breadth = 0; + } + } + //Accessors or Getters + int getLength() + { + return length; + } + int getBreadth() + { + return breadth; + } + //Facilatators + int area() + { + return (length * breadth); + } + int perimeter() + { + return (2 * (length + breadth)); + } +}; + +int main(void) +{ + Rectangle r1; //Default Constructor + Rectangle r2(); //Non-Parameterized Constructor + Rectangle r3(10, 5); //Parametrized Constructor + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/8. IntroductionToOPPs/DataHiding.cpp b/8. IntroductionToOPPs/DataHiding.cpp index b5996cc..c51c3f6 100644 --- a/8. IntroductionToOPPs/DataHiding.cpp +++ b/8. IntroductionToOPPs/DataHiding.cpp @@ -1,70 +1,70 @@ -#include -using namespace std; -//Program to demonstrate Data Hiding (Accessors & Mutators) -class Rectangle -{ -private: - int length; - int breadth; - -public: - //Mutators or Setters - void setLength(int l) - { - if (l > 0) - { - length = l; - } - else - { - length = 0; - } - } - void setBreadth(int b) - { - if (b > 0) - { - breadth = b; - } - else - { - breadth = 0; - } - } - //Accessors or Getters - int getLength() - { - return length; - } - int getBreadth() - { - return breadth; - } - //Function to calculate area of rectangle - int area() - { - return (length * breadth); - } - //Function to calculate perimeter of rectangle - int perimeter() - { - return (2 * (length + breadth)); - } -}; -//Accessors & Mutators are called Property Functions -int main(void) -{ - Rectangle r; - //Mutators - r.setLength(10); - r.setBreadth(5); - //Public Member Functions - cout << r.area() << endl; - cout << r.perimeter() << endl; - //Accessors - cout << "Length is " << r.getLength() << endl; - cout << "Breadth is " << r.getBreadth() << endl; - - return 0; -} +#include +using namespace std; +//Program to demonstrate Data Hiding (Accessors & Mutators) +class Rectangle +{ +private: + int length; + int breadth; + +public: + //Mutators or Setters + void setLength(int l) + { + if (l > 0) + { + length = l; + } + else + { + length = 0; + } + } + void setBreadth(int b) + { + if (b > 0) + { + breadth = b; + } + else + { + breadth = 0; + } + } + //Accessors or Getters + int getLength() + { + return length; + } + int getBreadth() + { + return breadth; + } + //Function to calculate area of rectangle + int area() + { + return (length * breadth); + } + //Function to calculate perimeter of rectangle + int perimeter() + { + return (2 * (length + breadth)); + } +}; +//Accessors & Mutators are called Property Functions +int main(void) +{ + Rectangle r; + //Mutators + r.setLength(10); + r.setBreadth(5); + //Public Member Functions + cout << r.area() << endl; + cout << r.perimeter() << endl; + //Accessors + cout << "Length is " << r.getLength() << endl; + cout << "Breadth is " << r.getBreadth() << endl; + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/8. IntroductionToOPPs/PointerToObject.cpp b/8. IntroductionToOPPs/PointerToObject.cpp index 7cb55f3..a5aa1c5 100644 --- a/8. IntroductionToOPPs/PointerToObject.cpp +++ b/8. IntroductionToOPPs/PointerToObject.cpp @@ -1,42 +1,42 @@ -#include -using namespace std; - -class Rectangle -{ -public: - int length; - int breadth; - //Function to calculate area of rectangle - int area() - { - return (length * breadth); - } - //Function to calculate perimeter of rectangle - int perimeter() - { - return (2 * (length + breadth)); - } -}; - -int main(void) -{ - Rectangle r; - Rectangle *ptrToStack; - // ptr created in STACK - ptrToStack = &r; //Ptr pointing to Rectangle r object - r.length = 10; - ptrToStack->length = 20; - ptrToStack->breadth = 50; - cout << (ptrToStack->area()) << endl; - - Rectangle *ptrToHeap; - //ptr created in heap - ptrToHeap = new Rectangle; - Rectangle *q = new Rectangle(); - ptrToHeap->length = 50; - ptrToHeap->breadth = 100; - cout << (ptrToHeap->area()) << endl; - - return 0; -} +#include +using namespace std; + +class Rectangle +{ +public: + int length; + int breadth; + //Function to calculate area of rectangle + int area() + { + return (length * breadth); + } + //Function to calculate perimeter of rectangle + int perimeter() + { + return (2 * (length + breadth)); + } +}; + +int main(void) +{ + Rectangle r; + Rectangle *ptrToStack; + // ptr created in STACK + ptrToStack = &r; //Ptr pointing to Rectangle r object + r.length = 10; + ptrToStack->length = 20; + ptrToStack->breadth = 50; + cout << (ptrToStack->area()) << endl; + + Rectangle *ptrToHeap; + //ptr created in heap + ptrToHeap = new Rectangle; + Rectangle *q = new Rectangle(); + ptrToHeap->length = 50; + ptrToHeap->breadth = 100; + cout << (ptrToHeap->area()) << endl; + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/8. IntroductionToOPPs/Rectangle.cpp b/8. IntroductionToOPPs/Rectangle.cpp index 7b37ebc..cd62882 100644 --- a/8. IntroductionToOPPs/Rectangle.cpp +++ b/8. IntroductionToOPPs/Rectangle.cpp @@ -1,37 +1,37 @@ -#include -using namespace std; -//Simple Program to demonstrate Object orientation -class Rectangle -{ -public: - int length; - int breadth; - //Function to calculate area of rectangle - int area() - { - return (length * breadth); - } - //Function to calculate perimeter of rectangle - int perimeter() - { - return (2 * (length + breadth)); - } -}; - -int main(void) -{ - //Objects of class Rectangle - Rectangle r1, r2; - //Accessing Data members - r1.length = 10; - r1.breadth = 5; - //Accessing Member Functions - cout << r1.area() << " " << r1.perimeter() << endl; - //Accessing Data members - r2.length = 15; - r2.breadth = 10; - //Accessing Member Functions - cout << r2.area() << " " << r2.perimeter() << endl; - return 0; -} +#include +using namespace std; +//Simple Program to demonstrate Object orientation +class Rectangle +{ +public: + int length; + int breadth; + //Function to calculate area of rectangle + int area() + { + return (length * breadth); + } + //Function to calculate perimeter of rectangle + int perimeter() + { + return (2 * (length + breadth)); + } +}; + +int main(void) +{ + //Objects of class Rectangle + Rectangle r1, r2; + //Accessing Data members + r1.length = 10; + r1.breadth = 5; + //Accessing Member Functions + cout << r1.area() << " " << r1.perimeter() << endl; + //Accessing Data members + r2.length = 15; + r2.breadth = 10; + //Accessing Member Functions + cout << r2.area() << " " << r2.perimeter() << endl; + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/8. IntroductionToOPPs/StudentExercise.cpp b/8. IntroductionToOPPs/StudentExercise.cpp index a912c46..bb36818 100644 --- a/8. IntroductionToOPPs/StudentExercise.cpp +++ b/8. IntroductionToOPPs/StudentExercise.cpp @@ -1,65 +1,65 @@ -#include -#include -using namespace std; -/*Write a class for student with -1. Roll -2. Name -3. Marks in 3 subject -Functions for Total marks Grade & required methods*/ -class Student -{ -private: - int roll; - string name; - int mathMarks; - int phyMarks; - int chemMarks; - -public: - Student(int r, string n, int m, int p, int c) //Constructor - { - roll = r; - name = n; - mathMarks = m; - phyMarks = p; - chemMarks = c; - } - int total() - { - return (mathMarks + phyMarks + chemMarks); - } - char grade() - { - float avg = total() / 3; - if (avg > 60) - { - return 'A'; - } - else if (avg >= 40 && avg < 60) - { - return 'B'; - } - else - { - return 'C'; - } - } -}; - -int main(void) -{ - int roll; - string name; - int m, p, c; - cout << "Enter Roll number of a Student: "; - cin >> roll; - cout << "Enter name of the Student: "; - cin >> name; - cout << "Enter marks for maths, phy & chem respectively: "; - cin >> m >> p >> c; - Student s(roll, name, m, p, c); - cout << "Total Marks: " << s.total() << endl; - cout << "Grade of Student: " << s.grade() << endl; - return 0; -} +#include +#include +using namespace std; +/*Write a class for student with +1. Roll +2. Name +3. Marks in 3 subject +Functions for Total marks Grade & required methods*/ +class Student +{ +private: + int roll; + string name; + int mathMarks; + int phyMarks; + int chemMarks; + +public: + Student(int r, string n, int m, int p, int c) //Constructor + { + roll = r; + name = n; + mathMarks = m; + phyMarks = p; + chemMarks = c; + } + int total() + { + return (mathMarks + phyMarks + chemMarks); + } + char grade() + { + float avg = total() / 3; + if (avg > 60) + { + return 'A'; + } + else if (avg >= 40 && avg < 60) + { + return 'B'; + } + else + { + return 'C'; + } + } +}; + +int main(void) +{ + int roll; + string name; + int m, p, c; + cout << "Enter Roll number of a Student: "; + cin >> roll; + cout << "Enter name of the Student: "; + cin >> name; + cout << "Enter marks for maths, phy & chem respectively: "; + cin >> m >> p >> c; + Student s(roll, name, m, p, c); + cout << "Total Marks: " << s.total() << endl; + cout << "Grade of Student: " << s.grade() << endl; + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/8. IntroductionToOPPs/ThisPointer.cpp b/8. IntroductionToOPPs/ThisPointer.cpp index 07e06e3..252ee3b 100644 --- a/8. IntroductionToOPPs/ThisPointer.cpp +++ b/8. IntroductionToOPPs/ThisPointer.cpp @@ -1,35 +1,35 @@ -#include -using namespace std; - -class Rectangle -{ -private: - int length; - int breadth; - -public: - //Parametrized Constructor - Rectangle(int length, int breadth) - { - this->length = length; - this->breadth = breadth; - } - //Function to calculate area of rectangle - int area() - { - return (length * breadth); - } - //Function to calculate perimeter of rectangle - int perimeter() - { - return (2 * (length + breadth)); - } -}; - -int main(void) -{ - Rectangle r3(10, 5); //Parametrized Constructor - - return 0; -} +#include +using namespace std; + +class Rectangle +{ +private: + int length; + int breadth; + +public: + //Parametrized Constructor + Rectangle(int length, int breadth) + { + this->length = length; + this->breadth = breadth; + } + //Function to calculate area of rectangle + int area() + { + return (length * breadth); + } + //Function to calculate perimeter of rectangle + int perimeter() + { + return (2 * (length + breadth)); + } +}; + +int main(void) +{ + Rectangle r3(10, 5); //Parametrized Constructor + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/9. OperatorOverloading/ComplexNumber.cpp b/9. OperatorOverloading/ComplexNumber.cpp index 684e17c..e2b9857 100644 --- a/9. OperatorOverloading/ComplexNumber.cpp +++ b/9. OperatorOverloading/ComplexNumber.cpp @@ -1,43 +1,43 @@ -#include -using namespace std; -//Program to demonstrate Operator '+' Overloading -class Complex -{ -private: - int real; - int img; - -public: - Complex(int r = 0, int i = 0) - { - real = r; - img = i; - } - // Complex add(Complex c) - Complex operator+(Complex c) - { - Complex temp; - temp.real = real + c.real; - temp.img = img + c.img; - return temp; - } - void print() - { - cout << real << " + " << img << "i" << endl; - } -}; - -int main(void) -{ - Complex c1(3, 6); - Complex c2(5, 4); - - Complex c3; - - // c3 = c1.add(c2); //c3 = c2.add(c1); - c3 = c1 + c2; - - c3.print(); - return 0; -} +#include +using namespace std; +//Program to demonstrate Operator '+' Overloading +class Complex +{ +private: + int real; + int img; + +public: + Complex(int r = 0, int i = 0) + { + real = r; + img = i; + } + // Complex add(Complex c) + Complex operator+(Complex c) + { + Complex temp; + temp.real = real + c.real; + temp.img = img + c.img; + return temp; + } + void print() + { + cout << real << " + " << img << "i" << endl; + } +}; + +int main(void) +{ + Complex c1(3, 6); + Complex c2(5, 4); + + Complex c3; + + // c3 = c1.add(c2); //c3 = c2.add(c1); + c3 = c1 + c2; + + c3.print(); + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/9. OperatorOverloading/FriendOperatorOverloading.cpp b/9. OperatorOverloading/FriendOperatorOverloading.cpp index 5c1d656..356bc0e 100644 --- a/9. OperatorOverloading/FriendOperatorOverloading.cpp +++ b/9. OperatorOverloading/FriendOperatorOverloading.cpp @@ -1,45 +1,45 @@ -#include -using namespace std; -//Program to demonstrate Operator '+' Overloading with friend function -class Complex -{ -private: - int real; - int img; - -public: - Complex(int r = 0, int i = 0) - { - real = r; - img = i; - } - - friend Complex operator+(Complex c1, Complex c2); - - void print() - { - cout << real << " + " << img << "i" << endl; - } -}; - -Complex operator+(Complex c1, Complex c2) -{ - Complex temp; - temp.real = c1.real + c2.real; - temp.img = c1.img + c2.img; - return temp; -} - -int main(void) -{ - Complex c1(3, 6); - Complex c2(5, 4); - - Complex c3; - - c3 = c1 + c2; //operator+(c1,c2); - - c3.print(); - return 0; -} +#include +using namespace std; +//Program to demonstrate Operator '+' Overloading with friend function +class Complex +{ +private: + int real; + int img; + +public: + Complex(int r = 0, int i = 0) + { + real = r; + img = i; + } + + friend Complex operator+(Complex c1, Complex c2); + + void print() + { + cout << real << " + " << img << "i" << endl; + } +}; + +Complex operator+(Complex c1, Complex c2) +{ + Complex temp; + temp.real = c1.real + c2.real; + temp.img = c1.img + c2.img; + return temp; +} + +int main(void) +{ + Complex c1(3, 6); + Complex c2(5, 4); + + Complex c3; + + c3 = c1 + c2; //operator+(c1,c2); + + c3.print(); + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/9. OperatorOverloading/InsertionOperatorOverloading.cpp b/9. OperatorOverloading/InsertionOperatorOverloading.cpp index 52dab3e..671532b 100644 --- a/9. OperatorOverloading/InsertionOperatorOverloading.cpp +++ b/9. OperatorOverloading/InsertionOperatorOverloading.cpp @@ -1,53 +1,53 @@ -// Stream operator overloading -// Input stream cin >> (extraction) operator can be overloaded upon a class -// output stream cout << (insertion) operator can be overloaded upon a class -#include -using namespace std; -//Program to demonstrate Operator '+' Overloading with friend function -class Complex -{ -private: - int real; - int img; - -public: - Complex(int r = 0, int i = 0) - { - real = r; - img = i; - } - //Overloading Binary '+' Operator - friend Complex operator+(Complex c1, Complex c2); //Function prototype - //Overloading Insertion Operator '<<' - friend ostream &operator<<(ostream &print, Complex &c); //function Prototype -}; -//Function Declaration -Complex operator+(Complex c1, Complex c2) -{ - Complex temp; - temp.real = c1.real + c2.real; - temp.img = c1.img + c2.img; - return temp; -} -//Function Declaration -ostream &operator<<(ostream &print, Complex &c) -{ - print << c.real << " + " << c.img << "i" << endl; - return print; -} - -int main(void) -{ - Complex c1(3, 6); - Complex c2(5, 4); - - Complex c3; - - c3 = c1 + c2; //operator+(c1,c2); - - // Using overloaded Insertion operator to display output to console - cout << c3 << endl; //operator<<(cout, c3); - - return 0; -} +// Stream operator overloading +// Input stream cin >> (extraction) operator can be overloaded upon a class +// output stream cout << (insertion) operator can be overloaded upon a class +#include +using namespace std; +//Program to demonstrate Operator '+' Overloading with friend function +class Complex +{ +private: + int real; + int img; + +public: + Complex(int r = 0, int i = 0) + { + real = r; + img = i; + } + //Overloading Binary '+' Operator + friend Complex operator+(Complex c1, Complex c2); //Function prototype + //Overloading Insertion Operator '<<' + friend ostream &operator<<(ostream &print, Complex &c); //function Prototype +}; +//Function Declaration +Complex operator+(Complex c1, Complex c2) +{ + Complex temp; + temp.real = c1.real + c2.real; + temp.img = c1.img + c2.img; + return temp; +} +//Function Declaration +ostream &operator<<(ostream &print, Complex &c) +{ + print << c.real << " + " << c.img << "i" << endl; + return print; +} + +int main(void) +{ + Complex c1(3, 6); + Complex c2(5, 4); + + Complex c3; + + c3 = c1 + c2; //operator+(c1,c2); + + // Using overloaded Insertion operator to display output to console + cout << c3 << endl; //operator<<(cout, c3); + + return 0; +} // Code By: Avinash Kumar. \ No newline at end of file diff --git a/9. OperatorOverloading/RationalNumber.cpp b/9. OperatorOverloading/RationalNumber.cpp index 60acc7a..96edc99 100644 --- a/9. OperatorOverloading/RationalNumber.cpp +++ b/9. OperatorOverloading/RationalNumber.cpp @@ -1,67 +1,67 @@ -// Program with a Class Rational Number -#include -using namespace std; - -class Rational -{ -private: - int p; - int q; - -public: - //Constructors - Rational() - { - p = 1; - q = 1; - } - Rational(int p, int q) - { - this->p = p; - this->q = q; - } - Rational(Rational &r) - { - this->p = r.p; - this->q = r.q; - } - //Accessors or Getters - int getP() { return p; } - int getQ() { return q; } - - //Mutators or Setters - void setP(int p) - { - this->p = p; - } - void setQ(int q) - { - this->q = q; - } - //Overloading '+' Operator - Rational operator+(Rational r) - { - Rational t; - t.p = this->p * r.q + this->q * r.p; - t.q = this->q * r.q; - return t; - } - //Overloading Insertion Operator '<<' - friend ostream &operator<<(ostream &os, Rational &r); -}; - -ostream &operator<<(ostream &os, Rational &r) -{ - os << r.p << "/" << r.q; - return os; -} - -int main() -{ - Rational r1(3, 4), r2(2, 5), r3; - r3 = r1 + r2; - cout << "Sum of " << r1 << " and " << r2 << " is " << r3 << endl; - - return 0; -} +// Program with a Class Rational Number +#include +using namespace std; + +class Rational +{ +private: + int p; + int q; + +public: + //Constructors + Rational() + { + p = 1; + q = 1; + } + Rational(int p, int q) + { + this->p = p; + this->q = q; + } + Rational(Rational &r) + { + this->p = r.p; + this->q = r.q; + } + //Accessors or Getters + int getP() { return p; } + int getQ() { return q; } + + //Mutators or Setters + void setP(int p) + { + this->p = p; + } + void setQ(int q) + { + this->q = q; + } + //Overloading '+' Operator + Rational operator+(Rational r) + { + Rational t; + t.p = this->p * r.q + this->q * r.p; + t.q = this->q * r.q; + return t; + } + //Overloading Insertion Operator '<<' + friend ostream &operator<<(ostream &os, Rational &r); +}; + +ostream &operator<<(ostream &os, Rational &r) +{ + os << r.p << "/" << r.q; + return os; +} + +int main() +{ + Rational r1(3, 4), r2(2, 5), r3; + r3 = r1 + r2; + cout << "Sum of " << r1 << " and " << r2 << " is " << r3 << endl; + + return 0; +} //Code By: Avinash Kumar \ No newline at end of file diff --git a/README.md b/README.md index bcc9ebb..9e06129 100644 --- a/README.md +++ b/README.md @@ -1,444 +1,444 @@ -# Essentials Fundamental: - -- Fundamentals -- How Computers Works -- Introduction to Number System -- What is a Program -- Low-level & High-level languages -- Compiler vs Interpreter -- What is an Operating System - -# Program Development: - -- Programming Paradigms Methodologies -- What is an algorithm -- What is a Flow-chart -- Steps for Program Development & Execution - -# C++ Basic: - -- Data Types, Variables -- Operators -- Compound Assignment Operator -- Overflow of data-types -- Bitwise Operators -- Logical Operator -- enum & typedef - -# Conditional Statements: - -- If and else is used for writing conditional statement. -- If condition is true then if block is executed. -- If condition is false then else block is executed. -- 0 => means false. -- 1 => means true or non-zero value is also true. -- If can be nested inside if as well as else statement. -- Nesting of is also written as else-if ladder. - -# Loops: - -- Loops are iterative statements. -- Block of statements are repeatedly executed as long as condition is true. -- Condition given in loop must become false after some finite iterations otherwise its a infinite loop. -- Values used in condition must update inside the body of finite loop. -- Four types of loops: - - pre-tested loop while() - - post-tested loop do..while() - - counter controlled loop for() - - for each loop for Collections for() - -# Arrays: - -- Array is a collection of similar data elements under one name, each element is accessible using its index. -- Memory for array is allocated contiguously. -- for-each loop is used for accessing array. -- N-dimension arrays are supported by C++. -- Two-Dimensional Arrays are sued for Matrices. -- Array can be created in Stack or Heap Section of memory. - -# Pointer: - -- Pointer is address variable. -- It can store the address of data. -- Pointer are used for accessing heap memory. -- 5 Arithmetic operations are allowed pointer: - - p++ => move pointer to next element. - - p-- => move pointer to previous element. - - p+k => gives address of kth element form pointer location to right. - - p-k => gives address of kth element from pointer location to left. - - q-p => gives number of elements between 2 pointers p and q. -- Pointers can be of many levels. -- Double pointer is used for accessing 2D arrays. - -# Reference: - -- Reference is a Alias of variable. -- It must be initialised when declared. -- It doesn’t take any memory. -- It cannot be modified to refer other variable. -- Syntax for reference declaration is. -- Int &y = x; - -# Functions: - -- Function is a module which performs a specific task -- Functions are called by name -- Rules for giving function name is same as variable name -- Function can take 0 or more parameters -- Function can return single value -- Void function don’t return any value -- Default return type is int - -# Function Overloading: - -- If More than one functions can have same name, but different parameter list, then they are overloaded functions -- Return the is not considered in overloading -- Function overloading is used for achieving compile time polymorphism - -# Function Template: - -- Function template are used for defining generic functions -- They work for multiple datatypes -- Datatype is decided based on the type of value passed -- Datatype is a template variable -- Function can have multiple template variables - -# Call by Address: - -- Address pf actual parameters are passed. -- Formal parameters must be pointers -- Formal parameters ca indirectly access actual parameters. -- Changes done using formal parameters will reflect in actual parameters - -# Return by Address: - -- A function can return address of memory -- It should not return address of local variables, which will be disposed after function ends -- It can return address of memory allocated in heap - -# Return by Reference - -- A function cal return reference -- It should not return reference of its local variables -- It can return formal parameters if they are reference - -# Default Arguments - -- Parameters of a function can have default values -- If a parameter is default then , passing its value is options -- Function with default argument can be called with variable number of argument -- Default values to parameters must be given from right side parameter -- Default arguments are much useful in constructors -- Default arguments are useful for defining overloaded functions - -# Static variables - -- They have local scope but remain in memory thru out the execution of program -- They are created in code section -- They are history-sensitive - -# Object-Oriented Programming: - -- Features of OOPS: - - Abstraction - - Data Hiding - - Inheritance - - Polymorphism - -# Classes: - -- Class is a blue print of an object. -- Class in a group of objects. -- Class is a design of object. - -# Many object can be created from same class: - -- Object consumes memory equal to sum of sizes of all data members -- Member functions don’t occupy memory -- Member functions are called depending on object -- . Dot operator is used for accessing members of object -- Memory allocated for object is also called as instance - -# A pointer of type class can be created: - -- A pointer can point on existing object -- A new object can be created in heap using pointer -- Arrow operator is used for accessing members of an object using pointer - -# Data Hiding: - -- Data members of a class class are usually declared as Private or Protected, -- They can be accessed only inside the class and child classes -- Data finding protects data from mishandling - -# Constructors: - -- Constructor is a member function of a class -- It will have same name as class name -- It will not have return type -- Its should be public -- It can be declared as private also in some cases -- It is called when object is created -- It is used for initialising an object -- It can be overloaded -- If its not defined then class will have a default constructor -- Constructor can take default arguments - -# Types of constructors: - -- Non-argument constructor -- Parameterised constructor -- Copy constructor - -# All types of Member Functions: - -- Constructors - called when object is created -- Accessors - used for knowing the value of data members -- Mutators - used for changing value of data member -- Facilitator - actual functions of class -- Enquiry - used for checking if an object satisfies some condition -- Destructor - used for releasing resources used by object - -# Operator Overloading: - -- Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the special meaning to the user-defined data type. Operator overloading is used to overload or redefines most of the operators available in C++. It is used to perform the operation on the user-defined data type. For example, C++ provides the ability to add the variables of the user-defined data type that is applied to the built-in data types. -- The advantage of Operators overloading is to perform different operations on the same operand. - -![image](https://drive.google.com/uc?export=view&id=1VDUwHMnGRW8v0FbNwOv674J-zsLmkGTm) - -# Inheritance: - -- Acquiring the feature of existing class into a new class. -- Deriving a class from a existing class. - -# Access Specifiers: - -- Private -> Accessible only inside a class -- Protected -> Accessible inside a class and inside derived classes -- Public -> accessible inside class, inside derived class and upon object - -# Ways of inheritance: - -- A class can be inherited in flowing ways -- Publicly -> All members of base will have same accessibility in derived class -- Protectedly -> All members of base will become protected in derived class -- Privately -> All members of base will become private in derived class - -# Base class Pointer pointing to derived class object - -- Base class pointer can point on derived class object. -- But only those functions which are in base class, can be called. -- If derived class is having overrides functions they will not be called unless base class functions are declared as virtual. -- Derived class pointer cannot point on base class object - - > Example 1 - - ``` - class Base - { - public: - void fun1() - { - cout<<"fun1 of Base"< Example 2 - - ``` - class Derived: public Base - { - public: - void fun2() - { - cout<<"fun2 of Derived"< Example 3 - - ``` - class BasicCar - { - public: - void start() - { - cout<<"Car started"< Example: - - ``` - class BasicCar - { - public: - virtual void start(){cout<<"BasicCar started"<start(); - } - ``` - -# Polymorphism - -- Same name different actions -- Runtime Polymorphism is achieved using function overriding -- Virtual functions are abstract functions of base class -- Derived class must override virtual function -- Base class pointer pointing to derived class object and a override function is called - - > Summary: class car is defined, then sub classes override, then base class method made as virtual the pure virtual - - > Example: - - ``` - - class Car - { - public: - virtual void start()=0; - }; - - class Innova:public Car - { - public: - void start(){cout<<"Innova Started"<start(); - p=new Swift(); - p->start(); - } - ``` - - > When we assign virtual function to 0. It is pure virtual function. Those function are must be overidded by the derived class. - -# Abstract class: - -- Class having pure virtual function is a abstract class -- Abstract class can have concrete also. -- Object of abstract class cannot be created -- Derived class can must override pure virtual function, otherwise it will also become a abstract class. -- Pointer of abstract class can be created -- Pointer of abstract class can hold object of derived class -- Abstract classes are used for achieving polymorphism -- Base class can be: - - Concrete - - Abstract with some concrete and some pure virtual functions - - All virtual functions - -> Example: - - ``` - class Base //Abstract Class - { - public: - virtual void fun1()=0; - virtual void fun2()=0; - }; - - class Derived :public Base - { - public: - void fun1() - { - cout<<"fun1 of Derived"< Example: - - ``` - class Student{ - public: - int rollNo; - static int admissionNo; - - Student() - { - admissionNo; - rollNo = admissionNo; - } - }; - - int Student::admissionNo = 0; - ``` +# Essentials Fundamental: + +- Fundamentals +- How Computers Works +- Introduction to Number System +- What is a Program +- Low-level & High-level languages +- Compiler vs Interpreter +- What is an Operating System + +# Program Development: + +- Programming Paradigms Methodologies +- What is an algorithm +- What is a Flow-chart +- Steps for Program Development & Execution + +# C++ Basic: + +- Data Types, Variables +- Operators +- Compound Assignment Operator +- Overflow of data-types +- Bitwise Operators +- Logical Operator +- enum & typedef + +# Conditional Statements: + +- If and else is used for writing conditional statement. +- If condition is true then if block is executed. +- If condition is false then else block is executed. +- 0 => means false. +- 1 => means true or non-zero value is also true. +- If can be nested inside if as well as else statement. +- Nesting of is also written as else-if ladder. + +# Loops: + +- Loops are iterative statements. +- Block of statements are repeatedly executed as long as condition is true. +- Condition given in loop must become false after some finite iterations otherwise its a infinite loop. +- Values used in condition must update inside the body of finite loop. +- Four types of loops: + - pre-tested loop while() + - post-tested loop do..while() + - counter controlled loop for() + - for each loop for Collections for() + +# Arrays: + +- Array is a collection of similar data elements under one name, each element is accessible using its index. +- Memory for array is allocated contiguously. +- for-each loop is used for accessing array. +- N-dimension arrays are supported by C++. +- Two-Dimensional Arrays are sued for Matrices. +- Array can be created in Stack or Heap Section of memory. + +# Pointer: + +- Pointer is address variable. +- It can store the address of data. +- Pointer are used for accessing heap memory. +- 5 Arithmetic operations are allowed pointer: + - p++ => move pointer to next element. + - p-- => move pointer to previous element. + - p+k => gives address of kth element form pointer location to right. + - p-k => gives address of kth element from pointer location to left. + - q-p => gives number of elements between 2 pointers p and q. +- Pointers can be of many levels. +- Double pointer is used for accessing 2D arrays. + +# Reference: + +- Reference is a Alias of variable. +- It must be initialised when declared. +- It doesn’t take any memory. +- It cannot be modified to refer other variable. +- Syntax for reference declaration is. +- Int &y = x; + +# Functions: + +- Function is a module which performs a specific task +- Functions are called by name +- Rules for giving function name is same as variable name +- Function can take 0 or more parameters +- Function can return single value +- Void function don’t return any value +- Default return type is int + +# Function Overloading: + +- If More than one functions can have same name, but different parameter list, then they are overloaded functions +- Return the is not considered in overloading +- Function overloading is used for achieving compile time polymorphism + +# Function Template: + +- Function template are used for defining generic functions +- They work for multiple datatypes +- Datatype is decided based on the type of value passed +- Datatype is a template variable +- Function can have multiple template variables + +# Call by Address: + +- Address pf actual parameters are passed. +- Formal parameters must be pointers +- Formal parameters ca indirectly access actual parameters. +- Changes done using formal parameters will reflect in actual parameters + +# Return by Address: + +- A function can return address of memory +- It should not return address of local variables, which will be disposed after function ends +- It can return address of memory allocated in heap + +# Return by Reference + +- A function cal return reference +- It should not return reference of its local variables +- It can return formal parameters if they are reference + +# Default Arguments + +- Parameters of a function can have default values +- If a parameter is default then , passing its value is options +- Function with default argument can be called with variable number of argument +- Default values to parameters must be given from right side parameter +- Default arguments are much useful in constructors +- Default arguments are useful for defining overloaded functions + +# Static variables + +- They have local scope but remain in memory thru out the execution of program +- They are created in code section +- They are history-sensitive + +# Object-Oriented Programming: + +- Features of OOPS: + - Abstraction + - Data Hiding + - Inheritance + - Polymorphism + +# Classes: + +- Class is a blue print of an object. +- Class in a group of objects. +- Class is a design of object. + +# Many object can be created from same class: + +- Object consumes memory equal to sum of sizes of all data members +- Member functions don’t occupy memory +- Member functions are called depending on object +- . Dot operator is used for accessing members of object +- Memory allocated for object is also called as instance + +# A pointer of type class can be created: + +- A pointer can point on existing object +- A new object can be created in heap using pointer +- Arrow operator is used for accessing members of an object using pointer + +# Data Hiding: + +- Data members of a class class are usually declared as Private or Protected, +- They can be accessed only inside the class and child classes +- Data finding protects data from mishandling + +# Constructors: + +- Constructor is a member function of a class +- It will have same name as class name +- It will not have return type +- Its should be public +- It can be declared as private also in some cases +- It is called when object is created +- It is used for initialising an object +- It can be overloaded +- If its not defined then class will have a default constructor +- Constructor can take default arguments + +# Types of constructors: + +- Non-argument constructor +- Parameterised constructor +- Copy constructor + +# All types of Member Functions: + +- Constructors - called when object is created +- Accessors - used for knowing the value of data members +- Mutators - used for changing value of data member +- Facilitator - actual functions of class +- Enquiry - used for checking if an object satisfies some condition +- Destructor - used for releasing resources used by object + +# Operator Overloading: + +- Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the special meaning to the user-defined data type. Operator overloading is used to overload or redefines most of the operators available in C++. It is used to perform the operation on the user-defined data type. For example, C++ provides the ability to add the variables of the user-defined data type that is applied to the built-in data types. +- The advantage of Operators overloading is to perform different operations on the same operand. + +![image](https://drive.google.com/uc?export=view&id=1VDUwHMnGRW8v0FbNwOv674J-zsLmkGTm) + +# Inheritance: + +- Acquiring the feature of existing class into a new class. +- Deriving a class from a existing class. + +# Access Specifiers: + +- Private -> Accessible only inside a class +- Protected -> Accessible inside a class and inside derived classes +- Public -> accessible inside class, inside derived class and upon object + +# Ways of inheritance: + +- A class can be inherited in flowing ways +- Publicly -> All members of base will have same accessibility in derived class +- Protectedly -> All members of base will become protected in derived class +- Privately -> All members of base will become private in derived class + +# Base class Pointer pointing to derived class object + +- Base class pointer can point on derived class object. +- But only those functions which are in base class, can be called. +- If derived class is having overrides functions they will not be called unless base class functions are declared as virtual. +- Derived class pointer cannot point on base class object + + > Example 1 + + ``` + class Base + { + public: + void fun1() + { + cout<<"fun1 of Base"< Example 2 + + ``` + class Derived: public Base + { + public: + void fun2() + { + cout<<"fun2 of Derived"< Example 3 + + ``` + class BasicCar + { + public: + void start() + { + cout<<"Car started"< Example: + + ``` + class BasicCar + { + public: + virtual void start(){cout<<"BasicCar started"<start(); + } + ``` + +# Polymorphism + +- Same name different actions +- Runtime Polymorphism is achieved using function overriding +- Virtual functions are abstract functions of base class +- Derived class must override virtual function +- Base class pointer pointing to derived class object and a override function is called + + > Summary: class car is defined, then sub classes override, then base class method made as virtual the pure virtual + + > Example: + + ``` + + class Car + { + public: + virtual void start()=0; + }; + + class Innova:public Car + { + public: + void start(){cout<<"Innova Started"<start(); + p=new Swift(); + p->start(); + } + ``` + + > When we assign virtual function to 0. It is pure virtual function. Those function are must be overidded by the derived class. + +# Abstract class: + +- Class having pure virtual function is a abstract class +- Abstract class can have concrete also. +- Object of abstract class cannot be created +- Derived class can must override pure virtual function, otherwise it will also become a abstract class. +- Pointer of abstract class can be created +- Pointer of abstract class can hold object of derived class +- Abstract classes are used for achieving polymorphism +- Base class can be: + - Concrete + - Abstract with some concrete and some pure virtual functions + - All virtual functions + +> Example: + + ``` + class Base //Abstract Class + { + public: + virtual void fun1()=0; + virtual void fun2()=0; + }; + + class Derived :public Base + { + public: + void fun1() + { + cout<<"fun1 of Derived"< Example: + + ``` + class Student{ + public: + int rollNo; + static int admissionNo; + + Student() + { + admissionNo; + rollNo = admissionNo; + } + }; + + int Student::admissionNo = 0; + ```