Object Oriented Programming Summary Notes

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 26

1

Object Oriented Programming lessons summary


Instructor: Dr. Talal Butt
Start date:16th /9/2019
Updated on: 21st /9/2019

OBJECT ORIENTED PROGRAMMING MOHAMMED ABDULLA MOH ABDULLA AHLI


2

Contents
First homework ............................................................................................................................................. 3
Brief: .......................................................................................................................................................... 3
Task: .......................................................................................................................................................... 3
Guidelines: ................................................................................................................................................ 3
Solved Code: ............................................................................................................................................. 3
Second homework ........................................................................................................................................ 5
Brief: .......................................................................................................................................................... 5
Task: .......................................................................................................................................................... 5
(un-effective coding) Solved Code: ........................................................................................................... 5
(Advanced|good coding) Solved Code:check again.................................................................................. 6
Third homework............................................................................................................................................ 8
Brief: .......................................................................................................................................................... 8
Task: .......................................................................................................................................................... 8
Guidelines: ................................................................................................................................................ 8
Solved Code: ............................................................................................................................................. 9

OBJECT ORIENTED PROGRAMMING MOHAMMED ABDULLA MOH ABDULLA AHLI


3

First homework(functions)
Brief:
Use functions with parameters and return values. Different datatypes must be used
such as strings, integers, and doubles.
Task:
Take input of user about student name, student id, student address, CGPA, and age
by using functions with datatypes and returning values. Then print the user details
by using a void function outputting whatever was taken as input.

Guidelines:

Following C++ program getting student details from user and print them back. You have to
define missing functions of the program to achieve this task.

#include<iostream>
#include<string>
using namespace std;

string getStudentId();
string getStudentName();
string getStudentAddress();
double getCgpa();
int getAge();
void printStudentDetails();

int main() {
string studentID = getStudentId();
string studentName = getStudentName();
string studentAddress = getStudentAddress();
double cgpa = getCgpa();
int age = getAge();

printStudentDetails(studentID, studentName, studentAddress, cgpa, age);


system("pause");
return 0;
}

Solved Code:
#include<iostream>
#include<string>
using namespace std;
// signatures.
string getStudentid();
string getStudentName();
string getStudentAdress();

OBJECT ORIENTED PROGRAMMING MOHAMMED ABDULLA MOH ABDULLA AHLI


4

double getCgpa();
int getAge();
void printStudentDetails(string a,string b, string c, double d,int e);
// declaring xes to be used inside functions for returning purposes.
string x;
int x2;
double x3;
// starting point of the application.
int main()
{
string StudentId = getStudentid();
string StudentName = getStudentName();
string StudentAddress = getStudentAdress();
double cgpa = getCgpa();
int Age = getAge();
printStudentDetails(StudentId,StudentName,StudentAddress,cgpa,Age);

system("pause");
return 0;
}
// my functions in this program
string getStudentid()
{
cout << "please enter your student ID\n";
cin >> x;
return x;
}
string getStudentName()
{
cout << "please enter your Name\n";
cin >> x;
return x;
}
string getStudentAdress()
{
cout << "please enter your address\n";
cin >> x;
return x;
}
double getCgpa()
{
cout << "please enter your Cgpa\n";
cin >> x3;
return x3;
}
int getAge()
{
cout << "please enter your age\n";
cin >> x2;
return x2;
}
void printStudentDetails(string a, string b, string c, double d, int e)
{
cout << "the student id is " << a << endl;
cout << "the student name is " << b << endl;
cout << "the student adress is " << c << endl;
cout << "the student cgpa is " << d << endl;
cout << "the student age is " << e << endl;

OBJECT ORIENTED PROGRAMMING MOHAMMED ABDULLA MOH ABDULLA AHLI


5

Second homework(arrays & functions)


Brief:
Use Arrays and loops and functions to recreate the homework 1. However take
input from more than one users now using arrays.
Task:
Use arrays and loops to take input of a certain amount of users and display their
details later on in an arranged way. In simpler words, recreate the homework 1 but
with arrays instead.
(un-effective coding) Solved Code:
#include<iostream>
#include<string>
using namespace std;

int main1()
{// declarations being done.
const int a = 10;
// arrays are declared over here.
string studentname[a];
string studentid[a];
string studentaddress[a];
double cgpa[a];
int age[a];
int x=1;

// loops here.
for (int i = 0; i < 10; i++)
{
cout << "Hello student number " << x << " please enter your details
here!\n";
cout << "please enter your name\n";
cin >> studentname[i];
cout << "please enter your student id\n";
cin >> studentid[i];
cout << "please enter your address\n";
cin >> studentaddress[i];
cout << "please enter your cgpa\n";
cin >> cgpa[i];
cout << "please enter your age\n";
cin >> age[i];
x+=1;
}

// loop of printing

for (int i = 0; i < 10; i++)


{

OBJECT ORIENTED PROGRAMMING MOHAMMED ABDULLA MOH ABDULLA AHLI


6

cout << "STUDENT NUMBER " << x << " HAS THE FOLLOWING DETAILS\n";
cout << "Student Name : "<<studentname[i]<<"\n";
cout << "Student ID: "<< studentid[i] << "\n";
cout << "Student Address: " << studentaddress[i] << "\n";
cout <<"Cgpa: " << cgpa[i] << "\n";
cout << "Age: " << age[i] << "\n";
x+=1;
}

system("pause");
return 0;
}

(Advanced|good coding) Solved Code:check again


#include<iostream>
#include<string>
using namespace std;
// signatures.
string getStudentid();
string getStudentName();
string getStudentAdress();
double getCgpa();
int getAge();
//void printstudentDetails(string a[], string b[], string c[], double d[], int e[]);

// declaring xes to be used inside functions for returning purposes.


string x;// returning values of strings.
int x2;// returning values of integers
double x3;// returning values of doubles.

int main()
{
int z = 1;// used to show user number
const int a = 2;// amount of users
// arrays are declared over here.
string studentname[a];
string studentid[a];
string studentaddress[a];
double cgpa[a];
int age[a];
// end of declarations

// start of loops
for (int i = 0; i < a; i++)
{
cout << "Hello student number " << z << " please enter your details
here!\n";
studentname[i] = getStudentName();
studentid[i] = getStudentid();
studentaddress[i] = getStudentAdress();
cgpa[i] = getCgpa();
age[i] = getAge();

OBJECT ORIENTED PROGRAMMING MOHAMMED ABDULLA MOH ABDULLA AHLI


7

z+=1;
}
z = 1;
// loop of printing
for (int i = 0; i < a; i++)
{

cout << "STUDENT NUMBER " << z << " HAS THE FOLLOWING DETAILS\n";
cout << "Student Name : " << studentname[i] << "\n";
cout << "Student ID: " << studentid[i] << "\n";
cout << "Student Address: " << studentaddress[i] << "\n";
cout << "Cgpa: " << cgpa[i] << "\n";
cout << "Age: " << age[i] << "\n";
z+=1;
}
system("pause");
return 0;
}
// my functions in this program
string getStudentid()
{
cout << "please enter your student ID\n";
cin >> x;
return x;
}
string getStudentName()
{
cout << "Please enter your Name\n";
cin >> x;
return x;
}
string getStudentAdress()
{
cout << "Please enter your Address\n";
cin >> x;
return x;
}
double getCgpa()
{
cout << "Please enter your CGPA\n";
cin >> x3;
return x3;
}
int getAge()
{
cout << "Please enter your Age\n";
cin >> x2;
return x2;
}

OBJECT ORIENTED PROGRAMMING MOHAMMED ABDULLA MOH ABDULLA AHLI


8

Third homework (searching array elements)


Brief:
Write a function for searching an array of integers
Task:
Write a function searchArray() to search an integer in the array and returns true if
found, returns false otherwise. Using the following main function.
Guidelines:

Write a function searchArray() to search an integer in the array and returns true if found, returns false
otherwise. Using the following main function.

#include <iostream>
using namespace std;

bool searchArray(int[], int, int);


int main()
{
int xArray[] = { 1,4,9,16,25,36,49,64,81,100 };
int x;
int size = sizeof(xArray) / sizeof(int);
cout << "Enter a number for searching: ";
cin >> x;

if (searchArray(xArray, size, x))


cout << "Found" << endl;
else
cout << "Not Found" << endl;
system("pause");
return 0;
}

OBJECT ORIENTED PROGRAMMING MOHAMMED ABDULLA MOH ABDULLA AHLI


9

Solved Code:
#include <iostream>

using namespace std;

bool searchArray(int array[], int y, int x);

int main()

int xArray[] = { 1,4,9,16,25,36,49,64,81,100 };

int x;

int size = sizeof(xArray) / sizeof(int);

cout << "Enter a number for searching: ";

cin >> x;

if (searchArray(xArray, size, x) == true)

cout << "Found" << endl;

else

cout << "Not Found" << endl;

system("pause");

return 0;

bool searchArray(int array[], int y, int x)

for (int i = 0; i < y; i++)

if (array[i] == x)

return true;

OBJECT ORIENTED PROGRAMMING MOHAMMED ABDULLA MOH ABDULLA AHLI


10

fourth homework (printing array elements and updating elements)


Brief:
Write two functions . first function should print the element inside an array. The
second function should update an element in the array and replace it with another
element given inside function.
Task:
Write function two functions of the following signature.
Guidelines:

Write two functions of the following signature


void printArray(int *ptr, int size);
void updateArray(int *ptr, int index, int value);

so as to run the following application

#include<iostream>
using namespace std;
int main()
{
int ra[] = { 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 };
printArray(ra, 10);
updateArray(ra, 5, 999);
printArray(ra, 10);
system("pause");
return 0;
}

OBJECT ORIENTED PROGRAMMING MOHAMMED ABDULLA MOH ABDULLA AHLI


11

Solved Code:

void printArray(int *ptr, int size);

void updateArray(int *ptr, int index, int value);

#include<iostream>

using namespace std;

int main()

int ra[] = { 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 };

printArray(ra, 10);

updateArray(ra, 5, 999);

printArray(ra, 10);

system("pause");

return 0;

void printArray(int *ptr, int size)

cout<<"{";

for(int i=0;i<size;i++)

cout<<*(ptr+i)<<",";

cout <<"}"<<endl;

void updateArray(int *ptr, int index, int value)

*(ptr + index) = value;

OBJECT ORIENTED PROGRAMMING MOHAMMED ABDULLA MOH ABDULLA AHLI


12

File handling guidelines


#include<iostream>

#include<fstream>

Using namespace std;

Int main()

Ifstream indata;// object of ifsteam which is responsible of taking input from the file.

Ofstream outdata;// object of ofstream which is responsible of outputting data into file.

Indata.open(“text.txt”);// file text.txt in the directory of the project must be used as file handling with
object indata.

Outdata.open(“outdata.txt”);// file outdata.txt should be used as file to keep output in.

If(indata.fail())// to check if the file is open or not.

Cout<<”file indata has not been found”<<endl;

If(outdata.fail())// to check if the file is open or not.

Cout<<”file outdata has not been found”<<endl;

Using indata to print all lines


String line;
while (!inData.eof())
{
while (getline(inData, line))
{
cout << line << endl;
}

OBJECT ORIENTED PROGRAMMING MOHAMMED ABDULLA MOH ABDULLA AHLI


13

Searching in file
While(!indata.eof())

While(getline(indata,line)

{
if ((position = line.find(search, 0)) != string::npos)
{
cout << line << endl;
outData<< line<<endl;// storing searched word into
outdata.
}
}

Using outdata
String x=”hello”;

Int y=5,z=15;

Outdata<<x<<” “<<y<<” “<<z<<” “<<endl;

Indata.close();// file should be closed

Outdata.close();// file should be closed.

OBJECT ORIENTED PROGRAMMING MOHAMMED ABDULLA MOH ABDULLA AHLI


14

Pointers Guidelines

#include <iostream>

#include<string>

using namespace std;

int main()

{int x = 10;

double y = 9.9;

char a = 'a';

string s = "Hellow";

int *q = &x;

double *w = &y;

char *e = &a;

string *r = &s;

cout << q << " is the pointer q\n";

cout << *q << " is the value of the address pointer q is pointing towards\n";

cout << w << " is the pointer w\n";

cout << *w << " is the value of the address pointer w is pointing towards\n";

cout << e << " is the pointer e\n";

cout << *e << " is the value of the address pointer e is pointing towards\n";

cout <<r << " is the pointer r\n";

cout << *r << " is the value of the address pointer r is pointing towards\n";

system("pause");

return 0;}

OBJECT ORIENTED PROGRAMMING MOHAMMED ABDULLA MOH ABDULLA AHLI


15

Cheatsheet for pointers

#include <iostream>

#include<string>

using namespace std;

int main()

{int vals[] = { 4,7,11 };

cout << vals[0] << endl;// value of zeroth integer in array

cout << *(vals + 0) << endl;// value of zeroth integer in array

cout << vals << endl; // address of zeroth integer in array

cout << &vals[0] << endl;// address of zeroth integer in array

cout << &vals[1] << endl;// address of first integer in array

cout << vals+1 << endl;// address of first integer in array

system("pause");

return 0;}

OBJECT ORIENTED PROGRAMMING MOHAMMED ABDULLA MOH ABDULLA AHLI


16

Polymorphism

#pragma once
#include "line.h"
#include<iostream>
using namespace std;
class Circle : public Line
{
protected:
int radius;
public:
Circle(int, int, int);
void draw(void);
int GetArea(void);
};
#include "Circle.h"
Circle::Circle(int a, int b, int c) : Line(a, b)
{
radius = c;
}
void Circle::draw(void)
{
cout << "Circle drawing code" << endl;
}
int Circle::GetArea(void)
{
cout << "Circle area code" << endl;
return 0;
}
#pragma once
#include<iostream>
using namespace std;
class Line {
protected:
int x, y;
public:
Line(int, int);
Virtual void draw(void);
Virtual int GetArea(void);
};
#include "line.h"
Line::Line(int a, int b)
{
x = a;
y = b;
}
void Line::draw(void)
{
cout << "\n Line Drawing code" << endl;
}
int Line::GetArea(void)
{
cout << "\nLine Area " << endl;
return 0;
}

OBJECT ORIENTED PROGRAMMING MOHAMMED ABDULLA MOH ABDULLA AHLI


17

#pragma once
#include "line.h"
#include<iostream>
using namespace std;
class Rectangle : public Line
{
protected:
int Width, Height;
public:
Rectangle(int, int, int, int);
void draw(void);
int GetArea(void);
};
#include "Rectangle.h"
Rectangle::Rectangle(int a, int b, int c, int d) : Line(a, b)
{
Width = c; Height = d;
}
void Rectangle::draw(void)
{
cout << "Rectangle drawing code" << endl;
}
int Rectangle::GetArea(void)
{
cout << "Rectangle area code" << endl;
return 0;
}
#include<iostream>
#include "line.h"
#include "Circle.h"
#include "Rectangle.h"
using namespace std;

int main()
{
Circle c1(3, 4, 5);
Rectangle r1(3, 4, 10, 20);
c1.draw();
cout << "The area is " << c1.GetArea() << endl;
r1.draw();
cout << "The area is " << r1.GetArea() << endl;
system("pause");
return 0;
}

OBJECT ORIENTED PROGRAMMING MOHAMMED ABDULLA MOH ABDULLA AHLI


18

Assignment 2 (searching files)


Brief:
File processing is one of the most vital functionalities of modern programming languages. C++ uses files
to store and read information.
Using the following text file of automotive “Automotive.txt”, create a C++ application to search a specific
type of automotive (car / bus / truck / bike) and copy only those lines containing that type to an output file
“Found.txt” and to the screen.
Figure 1 below shows the screenshot of the text file and figure 2 shows the proposed output.

Task:
Create an app which searches for cars and types and gives the searched elements found in txt file. Just
like in picture in guidelines.

Guidelines:

Figure 1: Text file of automotive

Figure 2: Output of the program

Note: Create separate header and cpp files for the class and main file in source.cpp

OBJECT ORIENTED PROGRAMMING MOHAMMED ABDULLA MOH ABDULLA AHLI


19

Solved Code:
Source.cpp
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
ifstream inData;
ofstream outData;
inData.open("Automotive.txt");
outData.open("found.txt");
string line, search;
int position,choice;

if (!inData)
{
cout << "Can't open automotive.txt file" << endl;
return 0;
}
if (!outData)
{
cout << "Can't open found.txt file" << endl;
return 0;
}

cout << "1.Car Manufacturers\n2.Bus Manufacturers\n3.Truck Manufacturers\n4.Bike


Manufacturers\n5.Quit\n";
cin >> choice;
if (inData)
{
if (choice == 1)
{
search = "car";
}
else if (choice == 2)
{
search = "bus";
}
else if (choice == 3)
{
search = "truck";
}
else if (choice == 4)
{
search = "bike";
}
else if (choice == 5)
{
return 0;
}
while (!inData.eof())
{
while (getline(inData, line))
{

OBJECT ORIENTED PROGRAMMING MOHAMMED ABDULLA MOH ABDULLA AHLI


20

if ((position = line.find(search, 0)) != string::npos)


{
cout << line << endl;
outData<< line<<endl;
}
}

}
}

inData.close();
outData.close();

system("pause");
return 0;
}

Automotive.txt
car Bmw
car Mercedes-Benz
car Toyota
car Honda
car Hyndai
bus Mercedes-Benz
bus Volvo
bus Scania
bus Tata
truck Man
truck Mercedes-Benz
truck Volvo
truck Scania
bike Honda
bike BMW

OBJECT ORIENTED PROGRAMMING MOHAMMED ABDULLA MOH ABDULLA AHLI


21

OBJECT ORIENTED PROGRAMMING MOHAMMED ABDULLA MOH ABDULLA AHLI


22

In-Class activities

Example :arrays and pointers


//Tasleek of errors when the pointer holds an array and the change is done in pointer or called element
doesn’t exist.

#include<iostream>

using namespace std;

void compute1(int a[]);

void compute2(int *x);

int main()

{int student[5] = { 1,2,3,4,5 };

int *sptr = student;

compute1(student);

compute2(sptr);

system("pause");

return 0;}

void compute1(int a[])

{a[5] = 10;}

void compute2(int *x)

{*(x + 5) = 10;}

Example array in pointers


void printArray(int *ptr, int size);
void updateArray(int *ptr, int index, int value);
#include<iostream>
using namespace std;
int main()
{
int ra[] = { 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 };
printArray(ra, 10);
updateArray(ra, 5, 999);

OBJECT ORIENTED PROGRAMMING MOHAMMED ABDULLA MOH ABDULLA AHLI


23

printArray(ra, 10);
system("pause");
return 0;
}
void printArray(int *ptr, int size)
{
cout<<"{";
for(int i=0;i<size;i++)
{
cout<<*(ptr+i)<<",";
}
cout <<"}"<<endl;
}
void updateArray(int *ptr, int index, int value)
{
*(ptr + index) = value;
}

Swapping Example
#include<iostream>

using namespace std;

void swap(int *x, int *y);

int temp;

int main()

int num1 = 2, num2 = -3;

swap(&num1, &num2);

cout << n << endl;

system("pause");

return 0;
OBJECT ORIENTED PROGRAMMING MOHAMMED ABDULLA MOH ABDULLA AHLI
24

void swap(int *x, int *y)

temp = *x;

*x = *y;

*y = temp;

Const pointers, pointers to consts, and normal pointers.


#include<iostream>

using namespace std;

int main()

int x = 10;

const int y = 15;

int z = 5;

int *xptr = &x;

const int *yptr = &y;

int const *zptr = &z;

cout << xptr << endl;

xptr = &z;

cout << xptr << endl;

cout << zptr << endl;

system("pause");

return 0;

OBJECT ORIENTED PROGRAMMING MOHAMMED ABDULLA MOH ABDULLA AHLI


25

Eg2

#include<iostream>

using namespace std;

int main()

int *intlist;

int arraysize;

cout << "enter array size: ";

cin >> arraysize;

cout << endl;

intlist = new int[arraysize];

delete[] intlist;

system("pause");

return 0;

Eg3

#include<iostream>

using namespace std;

class time// this is a class which is named as time

public:// the class is publicly accessable

int hour;

int minute;

int seconds;

};// the class is closed over here

OBJECT ORIENTED PROGRAMMING MOHAMMED ABDULLA MOH ABDULLA AHLI


26

Eg4

int main()

time t1;// new object in memory that uses the class time as a template

time *ptr = new time();// new pointer which has the address to the class

t1.hour = 10;// the first variable in the t1 which is hour is equal to 10

t1.minute = 20;// the second variable in the t1 which is minute is equal to 20

t1.seconds = 30;// the third variable in the t1 which is seconds is equal to 30

cout << t1.hour << ":";// outputting the t1 which is a template of class time and variable hour.

cout << t1.minute<< ":";// outputting the t1 which is a template of class time and variable minute.

cout << t1.seconds<< endl;// outputting the t1 which is a template of class time and variable second.

/////// editing pointers

ptr->hour = 20;

ptr->minute = 10;

ptr->seconds = 30;

system("pause");

return 0;

OBJECT ORIENTED PROGRAMMING MOHAMMED ABDULLA MOH ABDULLA AHLI

You might also like