0% found this document useful (0 votes)
10 views

Lecture 02

The document provides an overview and introduction to fundamentals of object-oriented programming concepts including classes, functions, strings, files, vectors and more. It was written by Rizoan Toufiq, an assistant professor, as course material for an object-oriented programming course. The document includes code examples and explanations of key OOP concepts to help students learn.

Uploaded by

Hasanul Banna
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lecture 02

The document provides an overview and introduction to fundamentals of object-oriented programming concepts including classes, functions, strings, files, vectors and more. It was written by Rizoan Toufiq, an assistant professor, as course material for an object-oriented programming course. The document includes code examples and explanations of key OOP concepts to help students learn.

Uploaded by

Hasanul Banna
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Fundamentals of OOP

Rizoan Toufiq1

1 Assistant Professor

Department of Computer Science & Engineering


Rajshahi University of Engineering & Technology
[email protected]

Course Title: Object Oriented Programming

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 1 / 33


Overview
1 Introduction to Class string
2 Type Casting
3 Overloading Function Name
4 File I/O
5 Appending to a File
6 The eof Member Function
7 Array
8 Introduction to the Standard Class string
9 String Processing with the Class string
10 Converting Between string Objects and C Strings
11 Converting Between Strings and Numbers
12 Vector
13 Pointer and Dynamic Array
14 Resources

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 2 / 33


Introduction to Class string

1 #include <iostream>
2 #include <string>
3 using namespace std;
4 int main()
5 {
6 string middleName, petName;
7 string alterEgoName;
8
9 cout << "Enter your middle name and the name of your pet.\n";
10 cin >> middleName;
11 cin >> petName;
12
13 alterEgoName = petName + " " + middleName;
14
15 cout << "The name of your alter ego is ";
16 cout << alterEgoName << "." << endl;
17
18 return 0;
19 }

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 3 / 33


Introduction to Class string

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 4 / 33


Type Casting

New version:
static_cast<double>(9)
Old Version:
double(totalCandy)

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 5 / 33


Overloading Function Name
1 //Illustrates overloading the function name ave.
2 #include <iostream>
3
4 double ave(double n1, double n2);
5 //Returns the average of the two numbers n1 and n2.
6
7 double ave(double n1, double n2, double n3);
8 //Returns the average of the three numbers n1, n2, and n3.
9
10 int main( )
11 {
12 using namespace std;
13 cout << "The average of 2.0, 2.5, and 3.0 is "
14 << ave(2.0, 2.5, 3.0) << endl;
15
16 cout << "The average of 4.5 and 5.5 is "
17 << ave(4.5, 5.5) << endl;
18
19 return 0;
20 }
21
22 double ave(double n1, double n2)
23 {
24 return ((n1 + n2)/2.0);
25 }
26
27 double ave(double n1, double n2, double n3)
28 {
29 return ((n1 + n2 + n3)/3.0);
30 }

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 6 / 33


Overloading Function Name

Output

The average of 2.0, 2.5, and 3.0 is 2.50000


The average of 4.5 and 5.5 is 5.00000

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 7 / 33


File I/O

1 //Reads three numbers from the file infile.dat, sums the


2 //and writes the sum to the file outfile.dat.
3 //(A better version of this program will be given in Di
4 #include <fstream>
5 int main( )
6 {
7 using namespace std;
8 ifstream inStream;
9 ofstream outStream;
10

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 8 / 33


File I/O

11 inStream.open("infile.dat");
12 outStream.open("outfile.dat");
13 int first, second, third;
14 inStream >> first >> second >> third;
15 outStream << "The sum of the first 3\n"
16 << "numbers in infile.dat\n"
17 << "is " << (first + second + third)
18 << endl;
19 inStream.close( );
20 outStream.close( );
21 return 0;
22 }

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 9 / 33


File I/O

Sample Output:

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 10 / 33


Appending to a File

fout.open("data.txt", ios::app);

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 11 / 33


Appending to a File

1 //Appends data to the end of the file data.txt.


2 #include <fstream>
3 #include <iostream>
4
5 int main( )
6 {
7 using namespace std;
8
9 cout << "Opening data.txt for appending.\n";
10 ofstream fout;
11 fout.open("data.txt", ios::app);
12 if (fout.fail( ))
13 {
14 cout << "Input file opening failed.\n";
15 exit(1);
16 }
17
18 fout << "5 6 pick up sticks.\n"
19 << "7 8 ain’t C++ great!\n";
20
21 fout.close( );
22 cout << "End of appending to file.\n";
23
24 return 0;
25 }

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 12 / 33


The Member Functions get and put

char c1, c2, c3;


cin.get(c1);
cin.get(c2);
cin.get(c3);

cout.put(’a’);

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 13 / 33


The eof Member Function

if (! fin.eof( ))
cout << "Not done yet.";
else
cout << "End of the file.";

inStream.get(next);
while (! inStream.eof( ))
{
cout << next;
inStream.get(next);
}

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 14 / 33


Array

Array (Same as C)

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 15 / 33


Introduction to the Standard Class string

1 //Demonstrates the standard class string.


2 #include <iostream>
3 #include <string>
4 using namespace std;
5 int main( )
6 {
7 string phrase;
8 string adjective("fried"), noun("ants");
9 string wish = "Bon appetit!";
10 phrase = "I love " + adjective + " " + noun + "!";
11 cout << phrase << endl
12 << wish << endl;
13 return 0;
14 }

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 16 / 33


String Processing with the Class string
A string Object Can Behave Like an Array:

1 //Demonstrates using a string object as if it were an array.


2 #include <iostream>
3 #include <string>
4 using namespace std;
5 int main( )
6 {
7 string firstName, lastName;
8 cout << "Enter your first and last name:\n";
9 cin >> firstName>>lastName;
10 cout << "Your last name is spelled:\n";
11 int i;
12 for (i = 0; i <lastName.length( ); i++)
13 {
14 cout << lastName[i] << " ";
15 lastName[i] = ’-’;
16 }
17 cout << endl;
18 for (i = 0; i <lastName.length( ); i++)
19 cout << lastName[i] << " "; //Places a "-" under each letter.
20 cout << endl;
21 cout << "Good day " << firstName << endl;
22 return 0;
23 }

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 17 / 33


String Processing with the Class string

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 18 / 33


String Processing with the Class string

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 19 / 33


String Processing with the Class string

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 20 / 33


String Processing with the Class string

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 21 / 33


String Processing with the Class string

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 22 / 33


Converting Between string Objects and C Strings

char aCString[] = "This is my C string.";


string stringVariable;
stringVariable = aCString;

aCString = stringVariable; //ILLEGAL

strcpy(aCString, stringVariable); //ILLEGAL

strcpy(aCString, stringVariable.c_str( )); //Legal;

aCString = stringVariable.c_str( ); //ILLEGAL

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 23 / 33


Converting Between Strings and Numbers

int i;
double d;
string s;
i = stoi("35");
// Converts the string "35" to an integer 35
d = stod("2.5");
// Converts the string "2.5" to the double 2.5
s = to_string(d*2);
// Converts the double 5.0 to a string "5.0000"
cout << i << " " << d << " " << s << endl;

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 24 / 33


Vector

1 #include <iostream>
2 #include <vector>
3 using namespace std;
4 int main( )
5 {
6 vector<int> v;
7 cout << "Enter a list of positive numbers.\n"
8 cout << "Place a negative number at the end.\n";
9 int next;
10 cin >> next;
11 while (next > 0)
12 {
13 v.push_back(next);
14 cout << next << " added. ";
15 cout << "v.size( ) = " <<v.size( ) <<endl;
16 cin >> next;
17 }
18 cout << "You entered:\n";
19 for (unsigned int i = 0; i <v.size( ); i++)
20 cout << v[i] << " ";
21 cout << endl;
22 return 0;
23 }

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 25 / 33


Vector

Sample Output

Enter a list of positive numbers.


Place a negative number at the end.
2 4 6 8 -1
2 added. v.size( ) = 1
4 added. v.size( ) = 2
6 added. v.size( ) = 3
8 added. v.size( ) = 4
You entered:
2 4 6 8

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 26 / 33


Vector Size and Capacity

The size of a vector is the number of elements in the vector.


The capacity of a vector is the number of elements for which it currently
has memory allocated.
For a vector v, the size and capacity can be recovered with the member
functions v.size( ) and v.capacity( ).
You can use the member function reserve to explicitly increase the
capacity of a vector. For example,
v.reserve(32);
v.reserve(v.size( ) + 10);
You can change the size of a vector using the member function resize.
v.resize(24);

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 27 / 33


Pointer

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 28 / 33


Dynamic Array

Dynamically allocate memory:


typedef double* DoublePtr;
DoublePtr p;
p = new double[10];
De-allocate Memory:
delete [] p;

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 29 / 33


Dynamic Memory Allocation

int main( )
{
using namespace std;
cout << "This program sorts numbers from lowest to highest.\n";

int array_size;
cout << "How many numbers will be sorted? ";
cin >> array_size;

IntArrayPtr a;
a = new int[array_size];

fill_array(a, array_size);
sort(a, array_size);

cout << "In sorted order the numbers are:\n";


for (int index = 0; index < array_size; index++)
cout << a[index] << " ";
cout << endl;

delete [] a;

return 0;
}

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 30 / 33


Read

Walter Savitch,
Problem Solving with C++
- Chapter 2 to 9

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 31 / 33


Resources

Walter Savitch
Problem Solving with C++
Herbert Schildt
Teach Yourself C++

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 32 / 33


The End

Rizoan Toufiq (Assistant Prof.,CSE,RUET) Fundamentals of OOP 33 / 33

You might also like