Skip to content

Commit 281d3a4

Browse files
committedJul 9, 2019
Add Advanced C++ features
1 parent 3da9b20 commit 281d3a4

23 files changed

+290
-15
lines changed
 

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.DS_Store
2+
13
# Prerequisites
24
*.d
35

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

‎2-OOP/README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
## Basics of Classes
22

3-
1. [Declaring a class](box.cpp)
4-
2. [Two ways to define member functions](memberFunctions.cpp)
5-
3. [Constructor](constructor.cpp)
6-
4. [Destructor](destructor.cpp)
7-
5. [Copy Constructor](matrix.cpp)
8-
6. [Static members](staticData.cpp)
3+
1. [Declaring a class](Classes/box.cpp)
4+
2. [Two ways to define member functions](Classes/memberFunctions.cpp)
5+
3. [Constructor](Classes/constructor.cpp)
6+
4. [Destructor](Classes/destructor.cpp)
7+
5. [Copy Constructor](Classes/matrix.cpp)
8+
6. [Static members](Classes/staticData.cpp)
99

1010
### Inheritance
1111

12-
[Shape class example](inheritance.cpp)
12+
[Shape class example](Inheritance/inheritance.cpp)
1313

1414
### Polymorphism
1515

‎Exception-Handling/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Exception Handling
2+
3+
[Division by zero exception](divisionByZero.cpp)

‎Exception-Handling/divisionByZero.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
double division(int a, int b) {
5+
if( b == 0 ) {
6+
throw "Division by zero condition!";
7+
}
8+
return (a/b);
9+
}
10+
11+
int main () {
12+
int x = 50;
13+
int y = 0;
14+
double z = 0;
15+
16+
try {
17+
z = division(x, y);
18+
cout << z << endl;
19+
} catch (const char* msg) {
20+
cerr << msg << endl;
21+
}
22+
23+
return 0;
24+
}
25+

‎File-Handling/README.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## File Handling
2+
3+
1. [Reading from a file](readFile.cpp)
4+
2. [Writing to a file](writeFile.cpp)

‎File-Handling/input.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Hello this is
2+
a sample file

‎File-Handling/output.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Writing sample text to file

‎File-Handling/readFile.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Program to demonstrate reading of a file in C++
3+
*/
4+
#include <iostream>
5+
#include <fstream> // contains ifstream, ofstream and fstream classes
6+
7+
using namespace std;
8+
int main () {
9+
10+
ifstream fin;
11+
// fin.open("input.txt", ios::in);
12+
fin.open("input.txt"); // by default: input mode
13+
14+
// while you reach the end of file
15+
// fin.eof() denotes end of file
16+
string s;
17+
cout << "File contents are:\n";
18+
while (!fin.eof()) {
19+
// read file word by word
20+
fin >> s;
21+
cout << s << "\n";
22+
}
23+
fin.close();
24+
return 0;
25+
}

‎File-Handling/writeFile.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Program to demonstrate writing to a file
3+
*/
4+
5+
#include <iostream>
6+
#include <fstream>
7+
8+
using namespace std;
9+
10+
int main () {
11+
12+
ofstream fout;
13+
fout.open("output.txt"); // by default: out mode
14+
15+
fout << "Writing sample text to file";
16+
17+
// good practice to close the opened files at the end
18+
fout.close();
19+
return 0;
20+
}

‎Memory-Allocation/README.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Memory Allocation in C++
2+
3+
[New and delete operators](newOperator.cpp)

‎Memory-Allocation/newOperator.cpp

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Program to understand new and delete operators in C++
3+
*/
4+
5+
#include <iostream>
6+
using namespace std;
7+
8+
class Matrix {
9+
private:
10+
int **matrix;
11+
int rows;
12+
int columns;
13+
public:
14+
Matrix(int r, int c) {
15+
cout << "Creating the matrix of size: " << r << "x" << c << "\n";
16+
rows = r;
17+
columns = c;
18+
// matrix = new int[rows][columns];
19+
matrix = new int*[rows];
20+
for (int i = 0; i < rows; i ++) {
21+
matrix[i] = new int[columns];
22+
}
23+
}
24+
void printMatrix() {
25+
cout << "Printing the matrix: \n";
26+
for (int i = 0; i < rows; i ++) {
27+
for (int j = 0; j < columns; j ++) {
28+
cout << matrix[i][j] << " ";
29+
}
30+
cout << endl;
31+
}
32+
}
33+
};
34+
35+
int main () {
36+
// new operator allocates memory from the heap memory
37+
// and returns the address of the space allocated
38+
39+
// more powerful than malloc in C
40+
// because it constructs the object rather
41+
// than just allocating the memory, that will be clear when you use it with classes
42+
43+
// single integer
44+
int *a = new int;
45+
*a = 10;
46+
cout << "Value pointed by a: " << *a << endl;
47+
48+
// array
49+
int *b = new int[5];
50+
for (int i = 0; i < 5; i ++) {
51+
b[i] = (i + 1);
52+
}
53+
cout << "Array of size 5 allocated dynamically: \n";
54+
for (int i = 0; i < 5; i ++) {
55+
cout << b[i] << " ";
56+
}
57+
cout << endl;
58+
59+
// a new matrix object will also be constructed
60+
// using the default constructor
61+
Matrix *m = new Matrix(3, 4);
62+
63+
// make sure to delete the allocated memory
64+
delete(a);
65+
delete[](b);
66+
delete(m);
67+
return 0;
68+
}

‎README.md

+32-8
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,43 @@ This repository contains the programs in C illustrated during the sessions.
1111

1212
### Basics of Classes
1313

14-
1. [Declaring a class](2-OOP/box.cpp)
15-
2. [Two ways to define member functions](2-OOP/memberFunctions.cpp)
16-
3. [Constructor](2-OOP/constructor.cpp)
17-
4. [Destructor](2-OOP/destructor.cpp)
18-
5. [Copy Constructor](2-OOP/matrix.cpp)
19-
6. [Static members](2-OOP/staticData.cpp)
14+
1. [Declaring a class](2-OOP/Classes/box.cpp)
15+
2. [Two ways to define member functions](2-OOP/Classes/memberFunctions.cpp)
16+
3. [Constructor](2-OOP/Classes/constructor.cpp)
17+
4. [Destructor](2-OOP/Classes/destructor.cpp)
18+
5. [Copy Constructor](2-OOP/Classes/matrix.cpp)
19+
6. [Static members](2-OOP/Classes/staticData.cpp)
2020

2121
### Inheritance
2222

23-
[Shape class example](2-OOP/inheritance.cpp)
23+
[Shape class example](2-OOP/Inheritance/inheritance.cpp)
2424

2525
### Polymorphism
2626

2727
1. [Constructor overloading](2-OOP/Polymorphism/constructorOverloading.cpp)
2828
2. [Intro to Virtual functions](2-OOP/Polymorphism/generalPolymorphism.cpp)
29-
3. [Operator overloading](2-OOP/Polymorphism/operatorOverloading.cpp)
29+
3. [Operator overloading](2-OOP/Polymorphism/operatorOverloading.cpp)
30+
31+
## File Handling
32+
33+
1. [Reading from a file](File-Handling/readFile.cpp)
34+
2. [Writing to a file](File-Handling/writeFile.cpp)
35+
36+
## Exception Handling
37+
38+
[Division by zero exception](Exception-Handling/divisionByZero.cpp)
39+
40+
## Memory Allocation in C++
41+
42+
[New and delete operators](Memory-Allocation/newOperator.cpp)
43+
44+
## Templates
45+
46+
### Template Function
47+
48+
[Smaller of two values](Templates/smallerOfTwo.cpp)
49+
50+
### Template Class (Generic Class)
51+
52+
[Student Record Header file](Templates/studentRecord.h)
53+
[Student Record Main file](Templates/studentRecord.cpp)

‎Templates/README.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## Templates
2+
3+
### Template Function
4+
5+
[Smaller of two values](smallerOfTwo.cpp)
6+
7+
### Template Class (Generic Class)
8+
9+
[Student Record Header file](studentRecord.h)
10+
[Student Record Main file](studentRecord.cpp)

‎Templates/smallerOfTwo.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Program to understand templates in C++
3+
*/
4+
5+
#include <iostream>
6+
using namespace std;
7+
8+
// typename T denotes that 'T' is used to refer to
9+
// the general type
10+
// general function
11+
template <typename T>
12+
T smallerOfTwo(T a, T b) {
13+
if (a < b) {
14+
return a;
15+
}
16+
return b;
17+
}
18+
19+
int main () {
20+
21+
int a = 3, b = 4;
22+
cout << "Smaller of 3 and 4: " << smallerOfTwo(a, b) << endl;
23+
24+
float c = 3.4, d = 1.3;
25+
cout << "Smaller of 3.4 and 1.3: " << smallerOfTwo(c, d) << endl;
26+
return 0;
27+
}

‎Templates/studentRecord.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Goal: study generic classes
3+
*/
4+
5+
#include "studentRecord.h"
6+
7+
int main() {
8+
// StudentRecord is the generic class
9+
// The constructor sets the grade value
10+
StudentRecord<int> srInt(3);
11+
srInt.setId(111111);
12+
srInt.printGrades();
13+
14+
StudentRecord<char> srChar('B');
15+
srChar.setId(222222);
16+
srChar.printGrades();
17+
18+
StudentRecord<float> srFloat(3.333);
19+
srFloat.setId(333333);
20+
srFloat.printGrades();
21+
22+
StudentRecord<string> srString("B-");
23+
srString.setId(4444);
24+
srString.printGrades();
25+
26+
return 0;
27+
}

‎Templates/studentRecord.h

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// header file for studentRecord.cpp
2+
3+
#include <iostream>
4+
using namespace std;
5+
6+
// tell compiler this class uses a generic value
7+
template <class T>
8+
class StudentRecord {
9+
private:
10+
const int size = 5;
11+
T grade;
12+
int studentId;
13+
public:
14+
StudentRecord(T input);
15+
void setId(int idIn);
16+
void printGrades();
17+
};
18+
19+
template<class T>
20+
StudentRecord<T>::StudentRecord(T input) {
21+
grade=input;
22+
}
23+
24+
template<class T>
25+
void StudentRecord<T>::setId(int idIn) {
26+
studentId = idIn;
27+
}
28+
29+
template<class T>
30+
void StudentRecord<T>::printGrades(){
31+
cout << "ID# " << studentId << ": ";
32+
cout << grade << "\n ";
33+
cout << "\n";
34+
}

0 commit comments

Comments
 (0)
Please sign in to comment.