Compare commits
3 Commits
ad185c994e
...
4824ab9855
| Author | SHA1 | Date | |
|---|---|---|---|
| 4824ab9855 | |||
| 7ba9554ed5 | |||
| 21afaa311b |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -3,4 +3,5 @@
|
||||
\#*
|
||||
_minted-file/
|
||||
.\#
|
||||
*html
|
||||
*html
|
||||
lab*/*pdf
|
||||
9
file.org
9
file.org
@@ -1,6 +1,7 @@
|
||||
#+title: Object Orientated Programming Lab
|
||||
#+author: Amneesh Singh I6
|
||||
#+LATEX_HEADER: \usepackage[margin=0.5in]{geometry}
|
||||
#+OPTIONS: ^:nil
|
||||
|
||||
#+LATEX: \clearpage
|
||||
|
||||
@@ -31,3 +32,11 @@
|
||||
* Lab 6
|
||||
#+INCLUDE: ./lab6/file.org
|
||||
#+LATEX: \clearpage
|
||||
|
||||
* Lab 7
|
||||
#+INCLUDE: ./lab7/file.org
|
||||
#+LATEX: \clearpage
|
||||
|
||||
* Lab 8
|
||||
#+INCLUDE: ./lab8/file.org
|
||||
#+LATEX: \clearpage
|
||||
|
||||
@@ -35,9 +35,6 @@ void Student::input() {
|
||||
cout << "Gender (m/f): ";
|
||||
cin >> this->gender;
|
||||
|
||||
if (this->gender != 'm' || this->gender != 'f')
|
||||
exit(1);
|
||||
|
||||
cout << "Enter Marks for" << endl;
|
||||
|
||||
for (i = 0; i < NSUBS; i++) {
|
||||
@@ -64,6 +61,8 @@ int main() {
|
||||
for (i = 0; i < NSTUDENTS; i++)
|
||||
a[i].input();
|
||||
|
||||
cout << "\nStudents with percentage greater than 70:\n";
|
||||
|
||||
for (i = 0; i < NSTUDENTS; i++)
|
||||
if (a[i].getPercentage() > 70)
|
||||
cout << a[i].getName() << endl;
|
||||
|
||||
@@ -100,11 +100,6 @@ int main() {
|
||||
}
|
||||
#+end_src
|
||||
|
||||
#+RESULTS:
|
||||
#+begin_src text
|
||||
-8 -3689 4 28892
|
||||
6 1227 -4 -9718
|
||||
#+end_src
|
||||
#+LATEX: \clearpage
|
||||
|
||||
* Create a class TIME with members hours, minutes, seconds. Take input, add two time objects passing objects to function and display the result. [Hint: use class object as parameter of function add()]
|
||||
@@ -169,17 +164,12 @@ int main() {
|
||||
}
|
||||
#+end_src
|
||||
|
||||
#+RESULTS:
|
||||
#+begin_src text
|
||||
Hours: 68
|
||||
Minutes: 32
|
||||
Seconds: 0
|
||||
#+end_src
|
||||
#+LATEX: \clearpage
|
||||
|
||||
* Create a class Student which has data members as name, branch, roll no, age , gender, marks in five subjects. Display the name of the student and his percentage who has more than 70%.Use array of objects size 5.[Hint: also declare percentage variable and calculate it and then compare all the object of student class to find name of student with percentage > 70]
|
||||
|
||||
#+begin_src cpp :tangle 6.cpp :results output :exports both :wrap src text
|
||||
#+ATTR_LATEX: :options frame=single,breaklines=true
|
||||
#+begin_src cpp :tangle 6.cpp :results output :wrap src text
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
@@ -217,9 +207,6 @@ void Student::input() {
|
||||
cout << "Gender (m/f): ";
|
||||
cin >> this->gender;
|
||||
|
||||
if (this->gender != 'm' || this->gender != 'f')
|
||||
exit(1);
|
||||
|
||||
cout << "Enter Marks for" << endl;
|
||||
|
||||
for (i = 0; i < NSUBS; i++) {
|
||||
@@ -246,6 +233,8 @@ int main() {
|
||||
for (i = 0; i < NSTUDENTS; i++)
|
||||
a[i].input();
|
||||
|
||||
cout << "\nStudents with percentage greater than 70:\n";
|
||||
|
||||
for (i = 0; i < NSTUDENTS; i++)
|
||||
if (a[i].getPercentage() > 70)
|
||||
cout << a[i].getName() << endl;
|
||||
@@ -253,3 +242,56 @@ int main() {
|
||||
return 0;
|
||||
}
|
||||
#+end_src
|
||||
|
||||
#+begin_src text
|
||||
Name: Amaang
|
||||
Branch: InformationTechnology
|
||||
Enter Enrollment number: 413
|
||||
Age: 14
|
||||
Gender (m/f): m
|
||||
Enter Marks for
|
||||
Subject 1: 99
|
||||
Subject 2: 93
|
||||
Subject 3: 9
|
||||
Subject 4: 91
|
||||
Subject 5: 3
|
||||
Name: Allu
|
||||
Branch: Physics
|
||||
Enter Enrollment number: 444
|
||||
Age: 19
|
||||
Gender (m/f): f
|
||||
Enter Marks for
|
||||
Subject 1: 100
|
||||
Subject 2: 0
|
||||
Subject 3: 100
|
||||
Subject 4: 0
|
||||
Subject 5: 100
|
||||
Name: Asli
|
||||
Branch: Law
|
||||
Enter Enrollment number: 33
|
||||
Age: 99
|
||||
Gender (m/f): m
|
||||
Enter Marks for
|
||||
Subject 1: 1
|
||||
Subject 2: 1
|
||||
Subject 3: 1
|
||||
Subject 4: 1
|
||||
Subject 5: 2
|
||||
Name: how
|
||||
Branch: Music
|
||||
Enter Enrollment number: 9999
|
||||
Age: 9
|
||||
Gender (m/f): m
|
||||
Enter Marks for
|
||||
Subject 1: 12
|
||||
Subject 2: 22
|
||||
Subject 3: 32
|
||||
Subject 4: 42
|
||||
Subject 5: 52
|
||||
Name: sabji
|
||||
Branch: CivilEngineering
|
||||
Enter Enrollment number: 91
|
||||
|
||||
Students with percentage greater than 70:
|
||||
sabji
|
||||
#+end_src
|
||||
|
||||
@@ -28,10 +28,6 @@ int main() {
|
||||
}
|
||||
#+end_src
|
||||
|
||||
#+RESULTS:
|
||||
#+begin_src text
|
||||
Factorial of 5 is 120
|
||||
#+end_src
|
||||
#+LATEX: \clearpage
|
||||
|
||||
* Write a program to perform addition of two complex numbers using constructor overloading. The first constructor which takes no argument is used to create objects which are not initialized, second which takes one argument is used to initialize real and imag parts to equal values and third which takes two arguments is used to initialize real and imag to two different values.
|
||||
@@ -85,14 +81,10 @@ int main() {
|
||||
}
|
||||
#+end_src
|
||||
|
||||
#+RESULTS:
|
||||
#+begin_src text
|
||||
38-2120i
|
||||
#+end_src
|
||||
#+LATEX: \clearpage
|
||||
|
||||
#+ATTR_LATEX: :options frame=single,breaklines=true
|
||||
* Write a program to generate a Fibonacci series using a copy constructor.
|
||||
|
||||
#+begin_src cpp :tangle 9.cpp :results output :exports both :wrap src text
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
@@ -29,15 +29,12 @@ int main() {
|
||||
}
|
||||
#+end_src
|
||||
|
||||
#+RESULTS:
|
||||
#+begin_src text
|
||||
444
|
||||
#+end_src
|
||||
#+LATEX: \clearpage
|
||||
|
||||
* Write a program to demonstrate the use of friend function with Inline assignment.
|
||||
All friend functions are inline functions.
|
||||
|
||||
#+ATTR_LATEX: :options frame=single,breaklines=true
|
||||
* Write a program to find the greatest of two given numbers in two different classes using friend function.
|
||||
#+begin_src cpp :tangle 12.cpp :results output :exports both :wrap src text
|
||||
#include <algorithm>
|
||||
@@ -74,8 +71,3 @@ int main() {
|
||||
cout << bigger(a, b);
|
||||
}
|
||||
#+end_src
|
||||
|
||||
#+RESULTS:
|
||||
#+begin_src text
|
||||
55
|
||||
#+end_src
|
||||
|
||||
22
lab7/19.cpp
Normal file
22
lab7/19.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Foo {
|
||||
int x;
|
||||
|
||||
public:
|
||||
Foo(int _x) { x = _x; };
|
||||
Foo &operator++();
|
||||
int get() { return x; }
|
||||
};
|
||||
|
||||
Foo &Foo::operator++() {
|
||||
x++;
|
||||
return *this;
|
||||
}
|
||||
|
||||
int main() {
|
||||
Foo bar = Foo(-3);
|
||||
++bar;
|
||||
cout << bar.get();
|
||||
}
|
||||
22
lab7/20.cpp
Normal file
22
lab7/20.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Foo {
|
||||
int x;
|
||||
|
||||
public:
|
||||
Foo(int _x) { x = _x; };
|
||||
Foo operator+(Foo);
|
||||
int get() { return x; }
|
||||
};
|
||||
|
||||
Foo Foo::operator+(Foo bar) {
|
||||
return Foo(x + bar.get());
|
||||
}
|
||||
|
||||
int main() {
|
||||
Foo bar = Foo(-3);
|
||||
Foo baz = Foo(8);
|
||||
|
||||
cout << (bar + baz).get();
|
||||
}
|
||||
22
lab7/21.cpp
Normal file
22
lab7/21.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Foo {
|
||||
int x;
|
||||
|
||||
public:
|
||||
Foo(int _x) { x = _x; };
|
||||
Foo operator*(Foo);
|
||||
int get() { return x; }
|
||||
};
|
||||
|
||||
Foo Foo::operator*(Foo bar) {
|
||||
return Foo(x * bar.get());
|
||||
}
|
||||
|
||||
int main() {
|
||||
Foo bar = Foo(-3);
|
||||
Foo baz = Foo(8);
|
||||
|
||||
cout << (bar * baz).get();
|
||||
}
|
||||
87
lab7/file.org
Normal file
87
lab7/file.org
Normal file
@@ -0,0 +1,87 @@
|
||||
* Write a program to overload (++) operator.
|
||||
|
||||
#+ATTR_LATEX: :options frame=single,breaklines=true
|
||||
#+begin_src cpp :tangle 19.cpp :results output :exports both :wrap src text
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Foo {
|
||||
int x;
|
||||
|
||||
public:
|
||||
Foo(int _x) { x = _x; };
|
||||
Foo &operator++();
|
||||
int get() { return x; }
|
||||
};
|
||||
|
||||
Foo &Foo::operator++() {
|
||||
x++;
|
||||
return *this;
|
||||
}
|
||||
|
||||
int main() {
|
||||
Foo bar = Foo(-3);
|
||||
++bar;
|
||||
cout << bar.get();
|
||||
}
|
||||
#+end_src
|
||||
|
||||
#+LATEX: \clearpage
|
||||
|
||||
* Write a program to overload + operator.
|
||||
|
||||
#+ATTR_LATEX: :options frame=single,breaklines=true
|
||||
#+begin_src cpp :tangle 20.cpp :results output :exports both :wrap src text
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Foo {
|
||||
int x;
|
||||
|
||||
public:
|
||||
Foo(int _x) { x = _x; };
|
||||
Foo operator+(Foo);
|
||||
int get() { return x; }
|
||||
};
|
||||
|
||||
Foo Foo::operator+(Foo bar) {
|
||||
return Foo(x + bar.get());
|
||||
}
|
||||
|
||||
int main() {
|
||||
Foo bar = Foo(-3);
|
||||
Foo baz = Foo(8);
|
||||
|
||||
cout << (bar + baz).get();
|
||||
}
|
||||
#+end_src
|
||||
|
||||
#+LATEX: \clearpage
|
||||
|
||||
* Write a program to overload * operator.
|
||||
|
||||
#+ATTR_LATEX: :options frame=single,breaklines=true
|
||||
#+begin_src cpp :tangle 21.cpp :results output :exports both :wrap src text
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Foo {
|
||||
int x;
|
||||
|
||||
public:
|
||||
Foo(int _x) { x = _x; };
|
||||
Foo operator*(Foo);
|
||||
int get() { return x; }
|
||||
};
|
||||
|
||||
Foo Foo::operator*(Foo bar) {
|
||||
return Foo(x * bar.get());
|
||||
}
|
||||
|
||||
int main() {
|
||||
Foo bar = Foo(-3);
|
||||
Foo baz = Foo(8);
|
||||
|
||||
cout << (bar * baz).get();
|
||||
}
|
||||
#+end_src
|
||||
19
lab8/22.cpp
Normal file
19
lab8/22.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <iostream>
|
||||
|
||||
template <typename T, std::enable_if_t<std::is_arithmetic<T>::value, T> = 0>
|
||||
void swap(T &x, T &y) {
|
||||
x += y;
|
||||
y = x - y;
|
||||
x -= y;
|
||||
}
|
||||
|
||||
int main() {
|
||||
char x = '[';
|
||||
char y = 'w';
|
||||
|
||||
swap(x, y);
|
||||
|
||||
std::cout << "x = " << x << ", y = " << y;
|
||||
|
||||
return 0;
|
||||
}
|
||||
14
lab8/23.cpp
Normal file
14
lab8/23.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <iostream>
|
||||
|
||||
template <typename T>
|
||||
std::enable_if_t<std::is_arithmetic<T>::value, T> sq(T x) {
|
||||
return x * x;
|
||||
}
|
||||
|
||||
int main() {
|
||||
float x = 4.4;
|
||||
|
||||
std::cout << "double: " << sq((double)5.3) << "\nlong: " << sq(4l);
|
||||
|
||||
return 0;
|
||||
}
|
||||
19
lab8/24.cpp
Normal file
19
lab8/24.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <iostream>
|
||||
|
||||
template <typename T>
|
||||
std::enable_if_t<std::is_integral<T>::value, T> fn(T x) {
|
||||
return x * x;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::enable_if_t<std::is_floating_point<T>::value, T> fn(T x) {
|
||||
return --x;
|
||||
}
|
||||
|
||||
int main() {
|
||||
float x = 4.4;
|
||||
|
||||
std::cout << "floating: " << fn((double)5.3) << "\nintegral: " << fn(4l);
|
||||
|
||||
return 0;
|
||||
}
|
||||
73
lab8/file.org
Normal file
73
lab8/file.org
Normal file
@@ -0,0 +1,73 @@
|
||||
* Write a program to define the function template for swapping two items of the various data types such as integer ,float,and characters.
|
||||
|
||||
#+ATTR_LATEX: :options frame=single,breaklines=true
|
||||
#+begin_src cpp :tangle 22.cpp :results output :exports both :wrap src text
|
||||
#include <iostream>
|
||||
|
||||
template <typename T, std::enable_if_t<std::is_arithmetic<T>::value, T> = 0>
|
||||
void swap(T &x, T &y) {
|
||||
x += y;
|
||||
y = x - y;
|
||||
x -= y;
|
||||
}
|
||||
|
||||
int main() {
|
||||
char x = '[';
|
||||
char y = 'w';
|
||||
|
||||
swap(x, y);
|
||||
|
||||
std::cout << "x = " << x << ", y = " << y;
|
||||
|
||||
return 0;
|
||||
}
|
||||
#+end_src
|
||||
|
||||
#+LATEX: \clearpage
|
||||
|
||||
* Write a program to define the function template for calculating the square of given numbers with different data types.
|
||||
|
||||
#+ATTR_LATEX: :options frame=single,breaklines=true
|
||||
#+begin_src cpp :tangle 23.cpp :results output :exports both :wrap src text
|
||||
#include <iostream>
|
||||
|
||||
template <typename T>
|
||||
std::enable_if_t<std::is_arithmetic<T>::value, T> sq(T x) {
|
||||
return x * x;
|
||||
}
|
||||
|
||||
int main() {
|
||||
float x = 4.4;
|
||||
|
||||
std::cout << "double: " << sq((double)5.3) << "\nlong: " << sq(4l);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#+end_src
|
||||
|
||||
#+LATEX: \clearpage
|
||||
|
||||
* Write a program to illustrate how template functions can be overloaded.
|
||||
|
||||
#+ATTR_LATEX: :options frame=single,breaklines=true
|
||||
#+begin_src cpp :tangle 24.cpp :results output :exports both :wrap src text
|
||||
#include <iostream>
|
||||
|
||||
template <typename T>
|
||||
std::enable_if_t<std::is_integral<T>::value, T> fn(T x) {
|
||||
return x * x;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::enable_if_t<std::is_floating_point<T>::value, T> fn(T x) {
|
||||
return --x;
|
||||
}
|
||||
|
||||
int main() {
|
||||
float x = 4.4;
|
||||
|
||||
std::cout << "floating: " << fn((double)5.3) << "\nintegral: " << fn(4l);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#+end_src
|
||||
Reference in New Issue
Block a user