0% found this document useful (0 votes)
30 views39 pages

Cen 23

Uploaded by

Khalifa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views39 pages

Cen 23

Uploaded by

Khalifa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 39

NAME NFOR BRIAN ASONYU

MATRICLE UBa21PH008

DEPARTMENT COMPUTER ENGINEERING(CNSM)

COURSE Object Oriented Programming(C++) CA

TAST 1: Student database management


A basic c++ program which illustrates basic file operation in c++.
File: main_student_manager
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <climits>
#include "manage_departments.cpp";
#include "manage_student.cpp";
#include "manage_course.cpp";
#include "manage_marks.cpp";
using namespace std;
void
title()
{ cout<<"STUDENT MANAGER\n\
n\n";
}
int main()
{
int option;
system("CLS");
title();
cout<<"[1] MANAGE STUDENT\n";
cout<<"[2] MANAGE COURSE\n";
cout<<"[3] MANAGE MARKS\n";
cout<<"[4] MANAGE DEPARTMENTS\n";
cout<<"[5] EXITl\n";
cout<<"Select
option:";
cin>>option;
switch
(option)
{ case 1: xstudent();
break; case 2: xcourse();
break; case 3: xmarks();
break; case 4:
xdepartments(); break;
case 5: break;

default:
break;
}
return 0;
}

Output
File: manage_student
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <climits>
using namespace
std;
class
student
{ private:
int id; int
age; char
name[200]; char
gender[5]; int
departmentId;
char level[10];

public:
void get_student();
void put_student();
int
get_stuId(){
return id;
} void
update_age(int num)
{ age =
age - num;
}
}; void
student::get_student()
{ cout<<"Select department
id:"; cin>>departmentId;
cout<<"Enter student Id:";
cin>>id;
fflush(stdin);
cout<<"Enter student name:";
cin.get(name, 200);
cout<<"Enter student gender:";
cin>>gender;
cout<<"Enter student level:";
cin>>level;
} void
student::put_student()
{ cout<<"----------------------------------------
--\n"; cout<<"Department Id:"<<departmentId;
cout<<"\nSudent Id:"<<id; cout<<"\nstudent
name:"<<name; cout<<"\nGender:"<<gender;
cout<<"\nLevel"<<level; cout<<"\
n------------------------------------------\n"; }
student myStudent; fstream stux; void add_student()
{ char ch = 'y'; dsp_departments();
stux.open("Students.dat", ios::app |
ios::binary); while(ch=='y' || ch=='Y')
{
myStudent.get_student();
fflush(stdin);
stux.write((char*)&myStudent,
sizeof(myStudent)); cout<<"Register another
student[y/n]?:"; cin>>ch;
}
stux.close();
}
void dsp_student()
{ stux.open("Students.dat", ios::in |
ios::binary); if(!stux)
{
cout<<"Error openning file..."<<endl;
exit(0); }
else{ stux.read((char*)&myStudent,
sizeof(myStudent));
while(!
stux.eof())
{ myStudent.put_student();
stux.read((char*)&myStudent, sizeof(myStudent));
} }
stux.close();
} void
srch_student()
{ int flag=0,
cod;
stux.open("Students.dat", ios::in |
ios::binary); if(!stux)
{ cout<<"Error openning file..."<<endl;
exit(0);
} else{ cout<<"Enter the student Id
to search:"; cin>>cod;
stux.read((char*)&myStudent, sizeof(myStudent));
while (!stux.eof())
{
if(cod==myStudent.get_stuId()){
flag=1;
myStudent.put_student();
break;
} stux.read((char*)&myStudent,
sizeof(myStudent));
}
if(flag==0)
{ cout<<"Not
found!"<<endl;
}

}
stux.close();
}
void delete_student()
{ int cod; cout<<"Enter the student Id
to delete record:";
cin>>cod; ofstream file2;
file2.open("newstu.dat", ios::app |
ios::binary); stux.open("Students.dat",
ios::in | ios::binary); if(!stux)
{ cout<<"Error openning file...";
exit(0);
}
else{ stux.read((char*)&myStudent,
sizeof(myStudent)); while (!stux.eof())
{ if(cod==myStudent.get_stuId()){
file2.write((char*)&myStudent, sizeof(myStudent));
} stux.read((char*)&myStudent,
sizeof(myStudent));
}
} file2.close();
stux.close();
remove("Students.dat");
rename("newstu.dat", "Students.dat");
} void
updt_student()
{
int cod, num;
cout<<"Enter Student Id to
Update:"; cin>>cod;
cout<<"Enter student Age:";
cin>>num;
stux.open("Sudents.dat", ios::in | ios::out |
ios::binary); if(!stux){
cout<<"Error openning file...";
exit(0);
} while(stux.read((char*)&myStudent,
sizeof(myStudent)))
{
if(myStudent.get_stuId()==cod)

{ myStudent.update_age(n
um); int pos =
sizeof(myStudent);
stux.seekp(pos, ios::cur);
stux.write((char*)&myStudent,
sizeof(myStudent));
} }
stux.close();
} void
xstudent()
{ int option;
system("CLS"); cout<<"[1]
CREATE STUDENT\n";
cout<<"[2] SEARCH STUDENT\n";
cout<<"[3] DELETE STUDENT\n";
cout<<"[4] UPDATE STUDENT\n";
cout<<"[5] EXIT\n";
cout<<"Select
option:";
cin>>option;
switch
(option)
{ case 1: add_student();
break; case 2:
srch_student(); break; case
3: delete_student(); break;
case 4: updt_student(); break;
case 5: break; default:
break;
}
}
Output files
File: manage_course
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <climits>
using namespace
std;
class
course
{ private:
int id; int
credit_value;
char title[200];
int departmentId;

public:
void get_course();
void put_course();
int
get_courseId(){
return id;
}
void update_cv(int num)
{ credit_value =
credit_value - num;
}

}; void
course::get_course()
{ cout<<"Select department
id:";

cin>>departmentId;
cout<<"Enter course Id:";
cin>>id; fflush(stdin);
cout<<"Enter course title:";
cin.get(title, 200);

void course::put_course()
{ cout<<"----------------------------------------
--\n"; cout<<"Department Id:"<<departmentId;
cout<<"\nCourse Id:"<<id; cout<<"\nCourse
title:"<<title; cout<<"\
n------------------------------------------\n"; }
course myCourse; fstream crx; void add_course()
{ char ch = 'y';
dsp_departments(); crx.open("Course.dat",
ios::app | ios::binary); while(ch=='y' ||
ch=='Y')
{ myCourse.get_course();
fflush(stdin);
crx.write((char*)&myCourse, sizeof(myCourse));
cout<<"Register another course[y/n]?:";
cin>>ch;
}
crx.close();
} void
dsp_course()
{
crx.open("Course.dat", ios::in |
ios::binary); if(!crx)
{ cout<<"Error openning
file..."<<endl;

exit(0);
}
else{ crx.read((char*)&myCourse,
sizeof(myCourse));

while(!crx.eof())
{ myCourse.put_course();
crx.read((char*)&myCourse, sizeof(myCourse));
}
}
crx.close();
}

void srch_course()
{ int flag=0,
cod;

crx.open("Course.dat", ios::in |
ios::binary); if(!crx)
{ cout<<"Error openning
file..."<<endl; exit(0);
} else{ cout<<"Enter the student
Id to search:"; cin>>cod;
crx.read((char*)&myCourse, sizeof(myCourse));
while (!crx.eof())
{

if(cod==myCourse.get_courseId()){
flag=1;
myCourse.put_course();
break;
}
crx.read((char*)&myCourse, sizeof(myCourse));
}
if(flag==0)
{ cout<<"Not
found!"<<endl;
}
} crx.close();
} void xcourse()
{ int option; system("CLS"); cout<<"[1] CREATE COURSE\
n"; cout<<"[2] SEARCH COURSE\n"; cout<<"[3] DELETE COURSE\
n"; cout<<"[4] UPDATE COURSE\n"; cout<<"[5] EXIT\n";
cout<<"Select option:"; cin>>option;
switch (option)
{ case 1: add_course(); break; case 2: srch_course(); break;
//case 3: delete_student(); break; //case 4: updt_student(); break;
case 5: break;
default: break;
}

Output files
File: manage_marks
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <climits>

using namespace
std;
class
marks
{ private:
int id; int
stu_id; int
score; char
grade; char
remark[200]; int
course_id; int
department_id;

public:
void get_marks();
void put_marks();
int
get_marks_id(){
return id;
} void
update_score(int num)
{ score =
score - num;
}

}; void
marks::get_marks()
{
cout<<"Select department
id:"; cin>>department_id;
cout<<"Enter course id:";
cin>>course_id;
cout<<"Enter marks Id:";
cin>>id;
cout<<"Enter student
id:"; cin>>stu_id;
fflush(stdin);
cout<<"Enter the
score:"; cin>>score;
cout<<"Grade:";
cin>>grade;
fflush(stdin);

cout<<"Enter marks remark:";


cin.get(remark, 200);

void marks::put_marks()
{ cout<<"----------------------------------------
--\n"; cout<<"Department Id:"<<department_id;
cout<<"\nCourse Id:"<<course_id; cout<<"\nMarks
id:"<<id; cout<<"\nStudent id:"<<stu_id;
cout<<"\nScore:"<<score; cout<<"\nGrade:"<<grade;
cout<<"\nRemark:"<<remark; cout<<"\
n------------------------------------------\n"; }
marks myMarks; fstream mks; void add_marks()
{ char ch = 'y';
dsp_departments(); mks.open("Marks.dat",
ios::app | ios::binary); while(ch=='y' ||
ch=='Y')
{ myMarks.get_marks();
fflush(stdin);
mks.write((char*)&myMarks, sizeof(myMarks));
cout<<"Uplaod new marks[y/n]?:";
cin>>ch;
}
mks.close();
} void
dsp_marks()
{
mks.open("Marks.dat", ios::in |
ios::binary); if(!mks)
{ cout<<"Error openning
file..."<<endl;
exit(0);
}
else{ mks.read((char*)&myMarks,
sizeof(myMarks));

while(!mks.eof())
{ myMarks.put_marks();
mks.read((char*)&myMarks, sizeof(myMarks));
}
}
mks.close();
}

void srch_marks()
{ int flag=0,
cod;

mks.open("Marks.dat", ios::in |
ios::binary); if(!mks)
{ cout<<"Error openning
file..."<<endl; exit(0);
} else{ cout<<"Enter the marks
id to search record:"; cin>>cod;
mks.read((char*)&myMarks, sizeof(myMarks));
while (!mks.eof())
{

if(cod==myMarks.get_marks_id()){
flag=1;
myMarks.put_marks();
break;
}
mks.read((char*)&myMarks, sizeof(myMarks));
}
if(flag==0)
{ cout<<"Not
found!"<<endl;
}
} mks.close();
} void xmarks()
{ int option; system("CLS"); cout<<"[1]
CREATE MARKS\n"; cout<<"[2] SEARCH MARKS\n";
cout<<"[3] DELETE MARKS\n"; cout<<"[4] UPDATE
MARKS\n"; cout<<"[5] EXIT\n";
cout<<"Select option:"; cin>>option;
switch (option)
{ case 1: add_marks(); break; case 2:
srch_marks(); break; //case 3: delete_student(); break;
//case 4: updt_student(); break; case 5: break;
default: break;
}

Output files
File: manage_departments
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <climits>
using namespace std;

class department
{ private:
int Id;
char Name[200];
int Capacity;

public:
void get_department();
void put_department();
int
get_code(){
return Id;
} void
update_Capacity(int num)
{
Capacity = Capacity - num;
}

}; void
department::get_department()
{ cout<<"Enter the department iD(integer from 1 to
100):"; cin>>Id; fflush(stdin);
cout<<"Enter the department name:"; cin.get(Name,
200); cout<<"Enter the department capacity:";
cin>>Capacity;
} void
department::put_department()
{
cout<<setw(2)<<Id<<"\t"<<setw(2)<<Name<<"\
t"<<setw(2)<<Capacity<<endl; }
department myDepartment;
fstream depx;
void add_department()
{ char ch = 'y';
depx.open("Departments.dat", ios::app |
ios::binary); while(ch=='y' || ch=='Y')
{ myDepartment.get_department();
fflush(stdin); depx.write((char*)&myDepartment,
sizeof(myDepartment));
cout<<"Add another
department[y/n]?:";
cin>>ch;
}
depx.close();
} void
dsp_departments()
{ depx.open("Departments.dat", ios::in |
ios::binary); if(!depx)
{ cout<<"Error openning
file..."<<endl; exit(0);
} else{ depx.read((char*)&myDepartment,
sizeof(myDepartment));
cout<<"------------------------------------\n";
cout<<setw(2)<<"Id"<<"\t"<<setw(2)<<"Name"<<"\t"<<setw(2)<<"Capacity"<<en
dl; cout<<"------------------------------------\n";
while(!depx.eof())
{ myDepartment.put_department();
depx.read((char*)&myDepartment, sizeof(myDepartment));
} }
depx.close();
} void
srch_departments()
{ int flag=0,
cod;

depx.open("Departments.dat", ios::in | ios::binary);


if(!depx){
cout<<"Error openning file..."<<endl;
exit(0);
} else{ cout<<"Enter the department Id
to search:"; cin>>cod;
depx.read((char*)&myDepartment, sizeof(myDepartment));

while (!depx.eof())
{
if(cod==myDepartment.get_code()){
flag=1;
cout<<"------------------------------------\
n";
cout<<setw(2)<<"Code"<<"\t"<<setw(2)<<"Name"<<"\
t"<<setw(2)<<"Cap acity"<<endl;
cout<<"------------------------------------\n";
myDepartment.put_department(); break;
} depx.read((char*)&myDepartment,
sizeof(myDepartment));
}
if(flag==0)
{ cout<<"Not
found!"<<endl;
}

}
depx.close();
} void
delete_department()
{ int cod; cout<<"Enter the department Id
to dlete record:"; cin>>cod; ofstream
file2; file2.open("new.dat", ios::app |
ios::binary); depx.open("Departments.dat",
ios::in | ios::binary); if(!depx)
{ cout<<"Error openning file...";
exit(0);
}
else{
depx.read((char*)&myDepartment, sizeof(myDepartment));
while (!depx.eof())
{
if(cod==myDepartment.get_code())
{ file2.write((char*)&myDepartment,
sizeof(myDepartment));
}
depx.read((char*)&myDepartment, sizeof(myDepartment));
}

}
file2.close();
depx.close();
remove("Departments.dat");
rename("new.dat",
"Departments.dat");
}
void
updt_department()
{ int cod, num; cout<<"Enter
department Id to Update:";
cin>>cod; cout<<"Enter student
capacity:"; cin>>num;
depx.open("Departments.dat", ios::in | ios::out |
ios::binary); if(!depx){ cout<<"Error openning
file..."; exit(0);
} while(depx.read((char*)&myDepartment,
sizeof(myDepartment)))
{
if(myDepartment.get_code()==cod)
{ myDepartment.update_Capacity(num);
int pos = sizeof(myDepartment); depx.seekp(pos,
ios::cur); depx.write((char*)&myDepartment,
sizeof(myDepartment));
} }
depx.close();

} void
xdepartments()
{
int option;
system("CLS");
cout<<"[1] CREATE DEPARTMENT\n";
cout<<"[2] SEARCH DEPARTMENT\n";
cout<<"[3] DELETE DEPARTMENT\n";
cout<<"[4] UPDATE DEPARTMENT\n";
cout<<"[5] EXIT\n";

cout<<"Select option:";

cin>>option;

switch (option)
{
case 1: add_department(); break;
case 2: srch_departments();
break; case 3:
delete_department(); break;
case 4: updt_department(); break;
case 5: break;

default:
break;
}

TASK1 OUTPUT SCREENSHOTS


Image1: Create a new student
Image2: Search student by id

Image3: Create a new course


Image4: Create marks record

Image5: Search marks


TASK2: FACTORIAL
#include<iostream>
using namespace std;
int factorial(int
n); int main() {
int n; cout << "Enter
a number: "; cin >> n;

cout << "Factorial of " << n << " = " << factorial(n);

return 0;
} int factorial(int
n) { if(n > 1)
return n * factorial(n -
1); else return 1
}

output
TASK3: FUNCTION OVERLOAING
// C++ Program to concatenate two string using a unary operator
overloading
#include <iostream>
#include <string.h>
using namespace
std;

// Class to implement operator


overloading // function for
concatenating the strings class AddString
{

public:
// Classes object of string
char s1[20], s2[20];

// Parameterized Constructor
AddString(char str1[], char
str2[]) {
// Initialize the string to class
object strcpy(this->s1, str1);
strcpy(this->s2, str2);
}

// Overload Operator+ to concat the string


void operator+()
{ cout <<"\nConcatenation: " <<
strcat(s1, s2);
}
};

// genesis of the code


int main()
{ char str1[20],
str2[20];
// getting two strings from user by means of
prompt cout<<"Enter string1: "; cin>>str1;

cout<<"Enter string2: ";


cin>>str2;

// Declaring and initializing the class


// with above two strings
AddString a1(str1,
str2);
// Call operator function
+a1;
return 0;
}

output

TASK4: UPPERCASE CONVERTER


#include<iostream>
#include<stdio.h>
#include<string>
using namespace
std;

int main()
{
system("CLS");
char strLower[50],
strUpper[50]; int x, i=0,
chr=0; cout<<"Enter the
String: "; cin.get(strLower,
50); while(strLower[i]!='\
0')
{
if(strLower[i]>='a' && strLower[i]<='z')
{ x =
strLower[i];
x = x-32;
strUpper[i] = x;
chr++;
}
else strUpper[i] = strLower[i];
i++; } strUpper[i]='\0'; if(chr==0)
cout<<"\nString is already in Uppercase"<<endl;
else cout<<"\nUppercase of String:
"<<strUpper<<endl; return 0; }
output

TASK5: GENERATE LIST OF ALL USER ID’s


#include <iostream>
#include <string>
using namespace
std;
string generate_id(string a,
string b){ int i, j, k, l;
string id; string all_ids;

// first digit
for (i = 0; i < 10; i++)
// second digit
for (j = 0; j < 10; j++)
// First letter for (k = 0;
k < a.length(); k++)
// Second letter
for (l = 0; l < a.length(); l++)
all_ids += to_string(i) + to_string(j) + a[k] + a[l]
+ ","; return all_ids;
} int
main(){

system("CLS"); string possible_ids =


generate_id("abcdefghijklmnopqrstuvwxyz",
"0123456789"); int i, count = 0;
for (i = 0; i < possible_ids.length(); i++)
{ if (possible_ids[i+4] == ','){
cout << "\t"; count++;
} cout <<
possible_ids[i];
} cout << "\n-------------------------- \nThere are " << count
<< " possible ids" << endl; return 0;
}

output

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy