Professional Documents
Culture Documents
Object Oriented Programming Summary Notes
Object Oriented Programming Summary Notes
Object Oriented Programming Summary Notes
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
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();
Solved Code:
#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;
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;
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
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;
}
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();
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;
}
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;
Solved Code:
#include <iostream>
int main()
int x;
cin >> x;
else
system("pause");
return 0;
if (array[i] == x)
return true;
#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;
}
Solved Code:
#include<iostream>
int main()
printArray(ra, 10);
updateArray(ra, 5, 999);
printArray(ra, 10);
system("pause");
return 0;
cout<<"{";
for(int i=0;i<size;i++)
cout<<*(ptr+i)<<",";
cout <<"}"<<endl;
#include<fstream>
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.
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;
Pointers Guidelines
#include <iostream>
#include<string>
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 value of the address pointer q is pointing towards\n";
cout << *w << " is the value of the address pointer w is pointing towards\n";
cout << *e << " is the value of the address pointer e is pointing towards\n";
cout << *r << " is the value of the address pointer r is pointing towards\n";
system("pause");
return 0;}
#include <iostream>
#include<string>
int main()
system("pause");
return 0;}
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;
}
#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;
}
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:
Note: Create separate header and cpp files for the class and main file in source.cpp
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;
}
}
}
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
In-Class activities
#include<iostream>
int main()
compute1(student);
compute2(sptr);
system("pause");
return 0;}
{a[5] = 10;}
{*(x + 5) = 10;}
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>
int temp;
int main()
swap(&num1, &num2);
system("pause");
return 0;
OBJECT ORIENTED PROGRAMMING MOHAMMED ABDULLA MOH ABDULLA AHLI
24
temp = *x;
*x = *y;
*y = temp;
int main()
int x = 10;
int z = 5;
xptr = &z;
system("pause");
return 0;
Eg2
#include<iostream>
int main()
int *intlist;
int arraysize;
delete[] intlist;
system("pause");
return 0;
Eg3
#include<iostream>
int hour;
int minute;
int seconds;
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
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.
ptr->hour = 20;
ptr->minute = 10;
ptr->seconds = 30;
system("pause");
return 0;