You are on page 1of 5

Object Oriented Programming

Assignment#4
Name: Muhammad Ali Yousaf
Program: BSCS
Section: 3-C
SAP ID: 70077969
Date: 1/09/2021
Submitted To: Mr. Muhammad Idrees
Task#1
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int vowels=0,len=0;
char a[]={'a','b','c','d','e'};

ofstream obj1;
obj1.open("File Handling.txt",ios::out|ios::trunc);
obj1<<a;
obj1.close();

ifstream obj;
obj.open("File Handling.txt",ios::in);
obj>>a;

//number of vowels
for(int i=0;i < a[i];i++)
{
++len;
if(a[i]=='a' || a[i]=='e' || a[i]=='i' || a[i]=='o' || a[i]=='u' || a[i]=='A' || a[i]=='E' ||
a[i]=='I' || a[i]=='O' || a[i]=='U')
++vowels;
}
cout<<"Characters stored in file: "<<a;
cout<<"\nTotal no. of Characters: "<<len;
cout<<"\nNumber of vowels in file: "<<vowels;
obj.close();
}

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

class MYCLASS{
char a[30];
public:
MYCLASS()
{
cout<<"Enter string: ";
cin>>a;
}
int operator ==(MYCLASS obj)
{
if(!strcmp(a,obj.a))
return 1;
else
return 0;
}
};

int main()
{
MYCLASS obj1,obj2;

ofstream out;
out.open("file 2.txt",ios::out|ios::trunc);
out.write((char*)&obj1,sizeof(obj1));
out.write((char*)&obj2,sizeof(obj2));
out.close();

ifstream in;
in.open("file 2.txt",ios::in);
in.read((char*)&obj1,sizeof(obj1));
in.read((char*)&obj2,sizeof(obj2));
if(obj1==obj2)
{
cout<<"\nStrings are Equal\n";
}
else
{
cout<<"\nStrings are Not Equal\n";
}
in.close();
}

Task#3
#include <iostream>
#include <cmath>
using namespace std;
template <typename T>
T odd(T t)
{
for(int i=0;i<5;i++)
{
if(fmod(t[i],2) != 0)
{
cout<<t[i]<<" ";
}
}
}
int main()
{
int a[5]={1,2,3,4,5};
double d[5]={10.199209,15.118064,20.0000,13.132556,16.0000};
cout<<"Odd in Int Array: ";
odd(a);
cout<<"\nOdd in Double Array: ";
odd(d);
}

You might also like