OOPS
OOPS
OOPS
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream file("Num.txt",ios::out);
cout << "Enter a passage (type '$' to end input):" << std::endl;
char ch;
while (cin.get(ch)) {
if (ch == '$') {
break;
}
file.put(ch);
}
file.close();
int n;
ifstream f1("Num.txt",ios::out);
while(!f1.eof()){
if(isdigit(f1.get(ch))){
n++;
}
}
f1.close();
cout<<"No.of numerals in Text file:"<<n;
return 0;
}
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
struct Employee {
int id;
char name[30];
float salary;
};
void write(string filename) {
ofstream outFile(filename,ios::binary);
Employee emp;
cout << "Enter Employee ID: ";
cin >> emp.id;
cout << "Enter Employee Name: ";
cin >> emp.name;
cout << "Enter Employee Salary: ";
cin >> emp.salary;
outFile.write((char*) &emp , sizeof(emp));
outFile.close();
cout << "Employee information saved to file.\n";
}
void display(string filename) {
Employee emp;
cout << "\nAll Employees:\n";
cout << left << setw(10) << "ID" << setw(30) << "Name" <<
setw(10) << "Salary" << endl;
ifstream inFile(filename,ios::binary);
while (!inFile.eof()){
inFile.read((char*) &emp, sizeof(emp));
cout << left << setw(10) << emp.id << setw(30) <<
emp.name
<< fixed << setprecision(2) << setw(10) << emp.salary
<<endl;
}
inFile.close();
}
void displayspecific(string filename, int searchID) {
ifstream inFile(filename, std::ios::binary);
Employee emp;
bool found = false;
while (!inFile.eof()){
inFile.read((char*) &emp , sizeof(emp));
if (emp.id == searchID) {
cout << "\nEmployee Found:\n";
cout << left << setw(15) << "Employee ID" << ": " <<
emp.id << "\n"
<< setw(15) << "Name" << ": " <<
emp.name << "\n"
<< setw(15) << "Salary" << ": $" << fixed <<
setprecision(2) << emp.salary << "\n";
found = true;
break;
}
}
if (!found) {
cout << "Employee with ID " << searchID << " not found.\
n";
}
inFile.close();
}
int main() {
string filename = "employees.dat";
int choice;
do {
cout << "\n1. Add Employee\n2. Display All Employees\n3.
Search for Employee by ID\n4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
write(filename);
break;
case 2:
display(filename);
break;
case 3:
int searchID;
cout << "Enter Employee ID to search: ";
cin >> searchID;
displayspecific(filename, searchID);
break;
case 4:
cout << "Exiting program.\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 4);
return 0;
}
EX 11
#include<iostream>
using namespace std;
class employee{
int salary;
public:
employee (int s): salary (s){}
virtual void getsalary(){
cout<<"Employee Salary:"<<salary;
}
};
class permanent: public employee{
int salary;
public:
permanent (int s): employee (s) {
salary = s;
}
void getsalary (){
cout <<"Permanent employee salary"<<salary;
}
};
class dailywages: public employee{
int salary;
public:
dailywages (int s): employee (s){
salary =s;
}
void getsalary(){
cout <<"Dailywages employee salary:"<<salary;
}
};
int main (){
employee e = employee(10000);
e.getsalary();
permanent p= permanent (50000);
employee *e1 = &p;
e1->getsalary();
dailywages d = dailywages(2500);
employee *e2 = &d;
e2-> getsalary ();
}
EX 10
#include<iostream>
#include<string>
using namespace std;
class volley{
public:
string name;
int roll;
string dept;
volley(string n,int no,string d){
name = n;
roll = no;
dept = d;
}
void vdetails(){
cout<<"volleyball object called";
cout<<name<<endl<<roll<<endl<<dept<<endl;
}
};
class science{
public:
string name;
int no;
string dept;
science(string n,int roll,string d):name(n),no(roll),dept(d){}
void get(){
cout<<"science objects called";
cout<<name<<endl<<no<<endl<<dept<<endl;
}
};
class sports:public volley,public science{
private:
int a;
string name;
string dept;
string game;
public:
sports(string n,string d,int i,string g):science(n,i,d),volley(n,i,d){
name = n;
dept = d;
game = g;
a = i;
}
void print(){
cout<<"sports object called";
cout<<name<<endl<<dept<<endl<<a<<endl<<game<<endl;
}
};
int main(){
sports s= sports("HARI","IT",53,"FOOTBALL");
s.print();
s.get();
s.vdetails();
}
‘#include <iostream>
#include <string>
using namespace std;
class bag{
string type;
string color;
public:
bag(string t,string c){
type =t;
color = c;
}
void getbag(){
cout<<type<<endl<<color<<endl;
}
};
class college:public bag{
private:
string name;
public:
college(string n,string t,string c):bag(t,c){
name = n;
}
void clg(){
cout<<name<<endl;
getbag();
}
};
int main(){
college c1 = college("ARTS","TRAVEL","RED");
college c2 = college("ENGG.","SPORTS","BLUE");
c1.clg();
c2.clg();
}
#include <iostream>
#include <string>
using namespace std;
class cloth{
string type;
string name;
public:
cloth(string t,string c){
type =t;
name = c;
}
void getcloth(){
cout<<type<<endl<<name<<endl;
}
};
class stitch:public cloth{
private:
string color;
public:
stitch(string t,string c,string n):cloth(t,c){
color = n;
}
void getstitch(){
getcloth();
cout<<color<<endl;
}
};
class unstitch:public cloth{
private:
int size;
public:
unstitch(string t,string c,int s):cloth(t,c){
size = s;
}
void getunstitch(){
getcloth();
cout<<size<<endl;
}
};
int main(){
stitch s = stitch("SHIRT","Cotton","RED");
s.getstitch();
unstitch u = unstitch("PANT","JEANS",32);
u.getunstitch();
}