0% found this document useful (0 votes)
11 views28 pages

Stucture - Array + One Pointer

The document defines C++ classes for Student, Employee, Admin, and HR Manager. Each class contains default and parameterized constructors, setter and getter methods for attributes, and a display method. Main functions for each class demonstrate creating and displaying objects, storing multiple objects in arrays, and searching for objects by attribute values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views28 pages

Stucture - Array + One Pointer

The document defines C++ classes for Student, Employee, Admin, and HR Manager. Each class contains default and parameterized constructors, setter and getter methods for attributes, and a display method. Main functions for each class demonstrate creating and displaying objects, storing multiple objects in arrays, and searching for objects by attribute values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

//1.

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

struct Student
{
int roll;
char name[20];
float marks;

Student() // default constructor


{
cout << "**Default constructor**\n\n";
this -> roll = 0;
strcpy(this -> name,"not given") ;
this -> marks = 0;
}

Student(int rol,char* nm,float mks) //parameterised constructor


{
cout << "\n\n**Parameterised constructor**\n\n";
this -> roll = rol;
strcpy(this -> name,nm);
this -> marks = mks;
}

void setRoll(int num)


{
this->roll = num;
}

void setName(const char * name)


{
strcpy(this->name, name);
}

void setMarks(float mk)


{
this->marks = mk;
}

int getRoll()
{
return this -> roll;
}

char* getName()
{
return this -> name;
}

float getMarks()
{
return this -> marks;
}

// display function

void display() // void display(student * this)


{
cout << "\n\nStudent Roll is: " << this -> roll;
cout << "\n\nStudent Name is: " << this -> name;
cout << "\n\nStudent Marks are: " << this -> marks;
}
}; // Struct ends here

int search(Student*,int , char*);


int main()
{

Student* stud;
stud=new Student();
stud->display();
stud=new Student(17,"DANNY",95);
stud->display();

int roll,mks;
int i,n;
char name[30];

cout<<"\n\nEnter number of Student :";


cin>>n;

//store

stud=new Student[n];
for(i=0;i<n;i++)
{
cout<<"\nEnter roll no.of a student :";
cin>>roll;
stud[i].setRoll(roll);

cout<<"\nEnter name of a student :";


cin>>name;
stud[i].setName(name);

cout << "\nEnter Marks of a Student :";


cin >> mks;
stud[i].setMarks(mks);
}

//display

for(i=0;i<n;i++)
{
stud[i].display();
cout<<"\n";
}

//search
int result;
cout<<"\nEnter the search student name:";
cin>>name;
result=search(stud,n,name);
if(result==1)
{
cout<<"Student Name Found.\n";
}
else
{
cout<<"Student Name Not Found.\n";
}
}
int search(Student*stud,int n,char*name) //non member function
{
int flag=0,i,result;

for(i=0;i<n;i++)
{
result=strcmp(stud[i].getName(),name);
if(result==0)
{
flag=1;
return 1;
}
}
if(flag==0)
return 0;
}
//2.Employee:
#include<iostream>
using namespace std;
#include<string.h>
struct Employee
{
int id;
char name[20];
double salary;

Employee() // default constructor


{
cout << "**Default constructor**\n\n";
this -> id = 0;
strcpy(this -> name,"not given");
this -> salary = 0.0;
}
Employee(int id,const char * nm ,double sal) //parameterised constructor
{
cout << "\n\n**Parameterised constructor**\n\n";
this -> id = id;
strcpy(this -> name,nm);
this -> salary = sal;
}

//setter function

void setId(int ad)


{
this->id = ad;
}

void setName(const char * nm)


{
strcpy(this->name, nm);
}

void setSalary(double sal)


{
this->salary = sal;
}

//getter function

int getId()
{
return this -> id;
}
char * getName()
{
return this -> name;
}
double getSalary()
{
return this -> salary;
}

//display function

void display() //void display(employee* ptr)


{
cout << "\nEmpolyee id is:" << this->id ;
cout << "\nEmployee name is: " << this->name ;
cout << "\nEmpolyee Salary is: " << this->salary ;

};// struct ends here

int search(Employee* ,int ,int);


int main()
{
Employee* emp;
emp=new Employee();
emp->display();
emp=new Employee(1,"QWER",28200);
emp->display();

int id;
char name[30];
double salary;
int i,n;

cout<<"\n\nEnter number of Employees :";


cin>>n;

//store

emp=new Employee[n];
for(i=0;i<n;i++)
{
cout<<"\nEnter employee id:";
cin>>id;
emp[i].setId(id);

cout<<"Enter employee name:";


cin>>name;
emp[i].setName(name);

cout<<"Enter employee salary:";


cin>>salary;
emp[i].setSalary(salary);
}

//display

for(i=0;i<n;i++)
{
emp[i].display();
cout<<"\n";
}
//search

int result;
cout<<"Enter ID to be search:";
cin>>n;
result=search(emp,n,id);
if(result==1)
{
cout<<"ID found";
}
else
{
cout<<"ID not found";
}
}
int search(Employee*emp,int n,int id) //Non Member Function
{
int flag=0,i;

for(i=0;i<n;i++)
{
if(emp[i].getId()==id)
{
flag=1;
return 1;
}
}
if(flag==0)
return 0;
}
//3 Admin
#include<iostream>
using namespace std;
#include<string.h>

struct Admin
{
int id;
char name[20];
double salary,allounce;

Admin() // default constructor


{
cout << "**Default constructor**\n\n";
this->id=0;
strcpy(this -> name,"not given") ;
this->salary=0;
this->allounce=0;
}
Admin(int id, char * nm, double sal,double all) //parameterised constructor
{
cout << "\n\n**Parameterised constructor**\n\n";
this-> id =id;
strcpy(this -> name,nm) ;
this-> salary =sal;
this->allounce=all;
}

// setter function

void setId(int ad)


{
this->id = ad;
}

void setName(const char * nm)


{
strcpy(this->name, nm);
}

void setSalary(double sal)


{
this->salary = sal;
}

void setAllounce(double all)


{
this->allounce = all;
}

// getter function

int getId()
{
return this->id;
}

char* getName()
{
return this -> name;
}

double getSalary()
{
return this->salary;
}

double getAllounce()
{
return this->allounce;
}

// display function

void display() // void display(admin* ptr)


{ cout << "\nAdmin id is: " << this->id;
cout << "\nAdmin name is: " << this->name;
cout << "\nAdmin Salary is: " << this->salary;
cout << "\nAdmin allounce is: " << this->allounce;
}

}; // struct ends here

int search(Admin* ,int ,int);


int main()
{
Admin* ad;
ad=new Admin();
ad->display();
ad=new Admin(1,"MNO",38000,1500);
ad->display();

int id;
char name[30];
double salary,allounce;
int i,n;

cout<<"\n\nEnter number of Admins :";


cin>>n;

//store

ad=new Admin[n];
for(i=0;i<n;i++)
{
cout<<"\nEnter admin id :";
cin>>id;
ad[i].setId(id);

cout<<"Enter admin name :";


cin>>name;
ad[i].setName(name);
cout<<"Enter admin salary :";
cin>>salary;
ad[i].setSalary(salary);

cout<<"Enter admin allounce :";


cin>>allounce;
ad[i].setAllounce(allounce);
}

//display

for(i=0;i<n;i++)
{
ad[i].display();
cout<<"\n";
}

//search

int result;
cout<<"\nEnter ID to be search :";
cin>>n;
result=search(ad,n,id);
if(result==1)
{
cout<<"ID found";
}
else
{
cout<<"ID not found";
}
}

int search(Admin*ad,int n,int id) //Non Member Function


{
int flag=0,i;

for(i=0;i<n;i++)
{
if(ad[i].getId()==id)
{
flag=1;
return 1;
}
}
if(flag==0)
return 0;

}
//4. HR Manager:
#include<iostream>
using namespace std;
#include<string.h>
struct Hr_manager
{
int id;
char name[20];
double salary,commision;

Hr_manager() //Default Constructor


{
cout << "**Default constructor**\n\n";
this -> id=0;
strcpy(this -> name,"not given");
this -> salary = 0;
this -> commision = 0;

Hr_manager(int i, char * nm, double sal, double com) //parameterised Constructor


{
cout << "\n\n**Parameterised constructor**\n\n";
this -> id=i;
strcpy(this -> name,nm);
this -> salary = sal;
this -> commision = com;
}

//setter function

void setId(int ad)


{
this->id = ad;
}

void setName(const char * nm)


{
strcpy(this->name, nm);
}

void setSalary(double sal)


{
this->salary = sal;
}

void setCommision(double com)


{
this->commision = com;
}

//getter function

int getId()
{
return this -> id;
}
char * getName()
{
return this -> name;
}
double getSalary()
{
return this -> salary;
}
double getCommision()
{
return this -> commision;
}

//display function

void display() //void display(hr_manager * ptr)


{
cout << "\nHr manager id is:" << this->id ;
cout << "\nHr manager name is: " << this->name ;
cout << "\nHr manager Salary is: " << this->salary ;
cout << "\nHr manager Comission is: " << this->commision ;

}; //struct ends here

int search(Hr_manager* hr,int ,int);


int main()
{
Hr_manager* hr;
hr=new Hr_manager();
hr->display();
hr=new Hr_manager(23,"Pratham",45000,2500);
hr->display();

int id,target;
char name[30];
double salary,commision;
int i,n;

cout<<"\n\nEnter number of hr_manager :";


cin>>n;

//store
hr=new Hr_manager[n];
for(i=0;i<n;i++)
{
cout<<"\nEnter id of hr manager:";
cin>>id;
hr[i].setId(id);

cout<<"Enter name of hr manager:";


cin>>name;
hr[i].setName(name);
cout<<"Enter hr manager salary:";
cin>>salary;
hr[i].setSalary(salary);

cout<<"Enter hr manager commision:";


cin>>commision;
hr[i].setCommision(commision);

//display
for(i=0;i<n;i++)
{
hr[i].display();
cout<<"\n";
}

//search
int result;
cout<<"\nEnter ID to be search :";
cin>>n;
result=search(hr,n,id);
if(result!=-1)
{
cout<<"ID found";
}
else
{
cout<<"ID not found";
}
}

int search(Hr_manager*hr,int n,int id) //Non Member Function


{
int flag=0,i;

for(i=0;i<n;i++)
{
if(hr[i].getId()==id)
{
flag=1;
return 1;
}
}
if(flag==0)
return 0;
}
//5.Sales Manager:
#include<iostream>
using namespace std;
#include<string.h>
struct Salesmanager
{
int id;
char name[20];
double target,salary,incentive;

Salesmanager() //Default Constructor


{
cout << "**Default constructor**\n\n";
this -> id=0;
strcpy(this -> name,"not given");
this -> target = 0;
this -> salary = 0;
this -> incentive = 0;
}

Salesmanager(int i, char * nm, double tar,double sal, double inc) //parameterised


Constructor
{
cout << "\n\n**Parameterised constructor**\n\n";
this -> id=i;
strcpy(this -> name,nm);
this -> target = tar;
this -> salary = sal;
this -> incentive = inc;
}

//setter function

void setId(int ad)


{
this->id = ad;
}

void setName(const char * nm)


{
strcpy(this->name, nm);
}

void setTarget(double tar)


{
this->target = tar;
}

void setSalary(double sal)


{
this->salary = sal;
}

void setIncentive(double inc)


{
this->incentive = inc;
}

//getter function

int getId()
{
return this ->id;
}
char * getName()
{
return this -> name;
}
double getTarget()
{
return this -> target;
}
double getSalary()
{
return this -> salary;
}
double getIncentive()
{
return this -> incentive;
}

//display function

void display() //void display(salesmanager * ptr)


{
cout << "\nSalesmanager id is:" << this->id ;
cout << "\nSalesmanager name is: " << this->name ;
cout << "\nSalesmanager Salary is: " << this->salary ;
cout << "\nSalesmanager Target is: " << this->target ;
cout << "\nSalesmanager incentive is: " << this->incentive ;

}; //struct ends here

int search(Salesmanager* ,int ,int);


int main()
{
Salesmanager* sm;
sm=new Salesmanager();
sm->display();
sm=new Salesmanager(15,"XYZ",45000,1500,500);
sm->display();

int id,target;
char name[30];
double salary,incentive;
int i,n;

cout<<"\n\nEnter number of salesmanager :";


cin>>n;
//store

sm=new Salesmanager[n];
for(i=0;i<n;i++)
{
cout<<"\nEnter id of sales manager:";
cin>>id;
sm[i].setId(id);

cout<<"Enter name of sales manager:";


cin>>name;
sm[i].setName(name);

cout<<"Enter manager salary:";


cin>>salary;
sm[i].setSalary(salary);

cout<<"Enter manager target:";


cin>>target;
sm[i].setTarget(target);

cout<<"Enter manager incentive:";


cin>>incentive;
sm[i].setIncentive(incentive);
}

//display

for(i=0;i<n;i++)
{
sm[i].display();
cout<<"\n";
}

//search

int result;
cout<<"\nEnter ID to be search :";
cin>>n;
result=search(sm,n,id);
if(result!=-1)
{
cout<<"ID found";
}
else
{
cout<<"ID Not found";
}
}

int search(Salesmanager*sm,int n,int id) //Non Member Function


{
int flag=0,i;

for(i=0;i<n;i++)
{
if(sm[i].getId()==id)
{
flag=1;
return 1;
}
}
if(flag==0)
return 0;
}
//6.Date:
#include<iostream>
using namespace std;
#include<string.h>
struct Date
{
int day,month,year;

Date() //Default Constructor


{
cout << "**Default constructor**\n\n";
this -> day=0;
this -> month=0;
this -> year=0;
}
Date(int d,int m, int y) //parameterised Constructor
{
cout << "\n\n**Parameterised constructor**\n\n";
this -> day=d;
this -> month=m;
this -> year=y;
}

//setter function

void setDay(int dy)


{
this->day = dy;
}

void setMonth (int mon)


{
this->month = mon;
}

void setYear (int yr)


{
this->year = yr;
}

//getter function

int getDay()
{
return this -> day;
}

int getMonth()
{
return this -> month;
}

int getYear()
{
return this -> year;
}

//display function

void display() //void display(date * ptr)


{
cout << "Date :" << this->day << "/" << this->month << "/" << this->year;
}

}; //struct ends here.

int search(Date * , int, Date *);


int main()
{
Date * dt;
dt=new Date();
dt->display();
dt=new Date(17,05,2015);
dt->display();

int da,mo,yr;
int i,n;

cout << "\n\nEnter the Number of Dates to get :";


cin >> n;

dt = new Date[n];

//Store
for(i=0;i<n;i++)
{
cout << "\nEnter Day :";
cin >> da;
dt[i].setDay(da);

cout << "Enter Month :";


cin >> mo;
dt[i].setMonth(mo);

cout << "Enter Year :";


cin >> yr;
dt[i].setYear(yr);
cout << "\n";
}

//display

for(i=0;i<n;i++)
{
cout << dt[i].getDay() << "/" << dt[i].getMonth() << "/" <<dt[i].getYear() << "\n";

//search
Date *d1;
d1 = new Date;
cout << "Enter Date You Want to search :\n";
cout << "Day :";
cin >> da;
d1->setDay(da);
cout << "Month :";
cin >> mo;
d1->setMonth(mo);
cout << "Year :";
cin >> yr;
d1->setYear(yr);

cout << "\nEntered Date is :";


d1->display();

int result;
result = search(dt,n,d1);
if(result==-1)
{
cout << "\nDate Not Found\n";
}
else
{
cout << "\nDate Found\n";
d1[result].display();
}
return 0;
}

//Non Member Function


int search(Date * dt, int n, Date * d1)
{
int i,flag;

for(i=0;i<n;i++)
{
if((dt[i].day == d1->getDay()) && (dt[i].month == d1->getMonth()) && (dt[i].year ==
d1->getYear()))
{
flag = 0;
break;
}
else
{
flag = -1;
}
}
return flag;
}
//7.Time:

#include<iostream>
using namespace std;
struct Time
{

int hr,min,sec;

Time()//Default Constructor
{
cout << "**Default constructor**\n\n";
this -> hr=0;
this -> min=0;
this -> sec=0;
}
Time(int h,int m, int s)//parameterised Constructor
{
cout << "\n\n**Parameterised constructor**\n\n";
this -> hr=h;
this -> min=m;
this -> sec=s;
}

//setter function

void setHr(int h)
{
this->hr = h;
}

void setMin(int m)
{
this->min = m;
}

void setSec(int s)
{
this->sec = s;
}

//getter function

int getHr()
{
return this -> hr;
}
int getMin()
{
return this -> min;
}
int getSec()
{
return this -> sec;
}
//display function

void display() //void display(time * ptr)


{
cout << "Time :" << this->hr << ":" << this->min << ":" << this->sec;
}

}; // Struct ends here

int search(Time * , int, Time *);


int main()
{
Time * ti;
ti=new Time();
ti->display();
ti=new Time(17,05,2015);
ti->display();

int hr,min,sec;
int i,n;

cout << "\n\nEnter the no. of Times to store :";


cin >> n;

ti = new Time[n];

//Store
for(i=0;i<n;i++)
{
cout << "\nEnter Hours :";
cin >> hr ;
ti[i].setHr(hr);

cout << "Enter Minutes :";


cin >> min;
ti[i].setMin(min);

cout << "Enter Seconds :";


cin >> sec;
ti[i].setSec(sec);
cout << "\n";
}

//display

for(i=0;i<n;i++)
{
cout << ti[i].getHr() << ":" << ti[i].getMin() << ":" << ti[i].getSec() << "\n";

//search

Time * t1;
t1 = new Time;
cout << "Enter Time You Want to search :\n";
cout << "Hours :";
cin >> hr;
t1->setHr(hr);
cout << "Minutes :";
cin >> min;
t1->setMin(min);
cout << "Seconds :";
cin >> sec;
t1->setSec(sec);

cout << "\nEntered Time is :";


t1->display();

int result;
result = search(ti,n,t1);
if(result==-1)
{
cout << "\nTime Not Found\n";
}
else
{
cout << "\nTime Found\n";
t1[result].display();
}
return 0;
}

//Non Member Function


int search(Time * ti, int n, Time * t1)
{
int i,flag;

for(i=0;i<n;i++)
{
if((ti[i].hr == t1->getHr()) && (ti[i].min == t1->getMin()) && (ti[i].sec == t1->getSec()))
{
flag = 0;
break;
}
else
{
flag = -1;
}
}
return flag;
}
//8.Distance:
#include<iostream>
using namespace std;
#include<string.h>

struct Distance
{
int feet,inch;

Distance() // Default constructor


{
cout << "**Default constructor**\n\n";
this -> feet = 0;
this -> inch = 0;
}
Distance(int f, int i) // Parameterised constructor
{
cout << "\n\n**Parameterised constructor**\n\n";
this -> feet = f;
this -> inch = i;
}

//setter function

void setFeet(int fe)


{
this->feet = fe;
}

void setInch (int in)


{
this->inch = in;
}

//getter function

int getFeet()
{
return this -> feet;
}

int getInch()
{
return this -> inch;
}

//display function

void display() //void display(distance * ptr)


{
cout << "\nfeet :" << this->feet;
cout << "\ninch :" << this->inch;
}

}; //Struct ends here


int search(Distance * , int, Distance *);
int main()
{
Distance * ds;
ds=new Distance();
ds->display();
ds=new Distance(15,6);
ds->display();

int feet,inch;
int i,n;

cout << "\n\nEnter the Number of Distance to get :";


cin >> n;

ds = new Distance[n];

//Store
for(i=0;i<n;i++)
{
cout << "\nEnter Feet :";
cin >> feet;
ds[i].setFeet(feet);

cout << "Enter Inchs :";


cin >> inch;
ds[i].setInch(inch);

cout << "\n";


}

//display

for(i=0;i<n;i++)
{
cout << "Feet :" << ds[i].getFeet() << "\n" ;
cout << "Inches :" << ds[i].getInch() << "\n\n";
}

//search
Distance *ds1;
ds1 = new Distance;
cout << "Enter Distance You Want to search :\n";
cout << "Feet :";
cin >> feet;
ds1->setFeet(feet);
cout << "Inchs :";
cin >> inch;
ds1->setInch(inch);

cout << "\nEntered Distance is :";


ds1->display();

int result;
result = search(ds, n, ds1);
if(result==-1)
{
cout << "\n\nDistance Not Found\n";
}
else
{
cout << "\n\nDistance Found :\n";
ds1[result].display();
}
return 0;
}

//Non Member Function


int search(Distance * ds, int n, Distance * ds1)
{
int i,flag;

for(i=0;i<n;i++)
{
if((ds[i].feet == ds1->getFeet()) && (ds[i].inch == ds1->getInch()))
{
flag = 0;
break;
}
else
{
flag = -1;
}
}
return flag;
}
//9.Complex:
#include<iostream>
using namespace std;
#include<string.h>

struct Complex
{
int real,imag;

Complex()// default constructor


{
cout << "**Default constructor**\n\n";
this->real=0;
this->imag=0;
}
Complex(int re, int im)//parameterised constructor
{
cout << "\n\n**Parameterised constructor**\n\n";
this-> real =re;
this-> imag =im;
}

void setReal(int rel)


{
this->real = rel;
}

void setImag(int img)


{
this->imag = img;
}

int getReal()
{
return this -> real;
}

int getImag()
{
return this -> imag;
}

void display() // void display(complex * this)


{
cout << this -> real << "+" << this -> imag << "i";
}

}; // Struct ends here

int search(Complex * , int, Complex *);


int main()
{
Complex * cp;
cp=new Complex();
cp->display();
cp=new Complex(10,5);
cp->display();

int real,imag;
int i,n;

cout << "\n\nEnter the Number of Complex no. to get :";


cin >> n;

cp = new Complex[n];

//Store
for(i=0;i<n;i++)
{
cout << "\nEnter Real no :";
cin >> real;
cp[i].setReal(real);

cout << "Enter Imaginery no :";


cin >> imag;
cp[i].setImag(imag);

cout << "\n";


}

//display

for(i=0;i<n;i++)
{
cout << cp[i].getReal() << "+" << cp[i].getImag() << "i" << "\n";

//search
Complex *c1;
c1 = new Complex;
cout << "Enter Complex number You Want to search :\n";
cout << "Real no :";
cin >> real;
c1->setReal(real);
cout << "Imaginery no :";
cin >> imag;
c1->setImag(imag);

cout << "\nEntered Complex number is :";


c1->display();

int result;
result = search(cp, n, c1);
if(result==-1)
{
cout << "\nComplex number Not Found\n";
}
else
{
cout << "\nComplex number Found :\n";
c1[result].display();
}
return 0;
}

//Non Member Function


int search(Complex * cp, int n, Complex * c1)
{
int i,flag;

for(i=0;i<n;i++)
{
if((cp[i].real == c1->getReal()) && (cp[i].imag == c1->getImag()))
{
flag = 0;
break;
}
else
{
flag = -1;
}
}
return flag;
}

You might also like