Professional Documents
Culture Documents
Classes
Classes
Classes
#include<iostream>
using namespace std;
int main()
{
emp e1; //size of e1 = 56 B (20+4+10+8+10 =52) and bit padding is 8
//Structure:
#include<iostream>
using namespace std;
int main()
{
emp e1; //size of e1 = 56 B (20+4+10+8+10) and bit padding is 8
cout<<"Size of structure = "<<sizeof(e1);
cin>>e1.ename>>e1.eid>>e1.dept>>e1.sal>>e1.role;
cout<<"\n details are:\n";
cout<<e1.ename<<endl<<e1.eid<<endl<<e1.dept<<endl<<e1.sal<<endl<<e1.role;
return 0;
}
------------
// data members are private and not accessible outside of class
#include<iostream>
using namespace std;
int main()
{
emp e1; //size of e1 = 56 B (20+4+10+8+10) and bit padding is 8
cout<<"Size of structure = "<<sizeof(e1);
cin>>e1.ename>>e1.eid>>e1.dept>>e1.sal>>e1.role;
cout<<"\n details are:\n";
cout<<e1.ename<<endl<<e1.eid<<endl<<e1.dept<<endl<<e1.sal<<endl<<e1.role;
return 0;
}
------
// data members are protected and not accessible outside of class
#include<iostream>
using namespace std;
int main()
{
emp e1; //size of e1 = 56 B (20+4+10+8+10) and bit padding is 8
cout<<"Size of structure = "<<sizeof(e1);
cin>>e1.ename>>e1.eid>>e1.dept>>e1.sal>>e1.role;
cout<<"\n details are:\n";
cout<<e1.ename<<endl<<e1.eid<<endl<<e1.dept<<endl<<e1.sal<<endl<<e1.role;
return 0;
}
------
int main()
{
emp e1; //size of e1 = 56 B (20+4+10+8+10) and bit padding is 8
cout<<"Size of structure = "<<sizeof(e1);
cin>>e1.ename>>e1.eid>>e1.dept>>e1.sal>>e1.role;
return 0;
}
---------------
// data members are public and accessible outside of class
#include<iostream>
using namespace std;
public:
void getdata() // member function
{
cout<<"\n Enter employee details:\n";
cin>>ename>>eid>>dept>>sal>>role;
}
void putdata()
{
cout<<"\n details are:\n";
cout<<ename<<endl<<eid<<endl<<dept<<endl<<sal<<endl<<role;
}
};
int main()
{
emp e1; //size of e1 = 56 B (20+4+10+8+10) and bit padding is 8
cout<<"Size of class = "<<sizeof(e1);
e1.getdata();
e1.putdata();
return 0;
}
-------
#include<iostream>
using namespace std;
class emp //class definition
{
private:
char ename[20]; //data members
int eid;
char dept[10];
double sal;
char role[10];
public:
void putdata()
{
getdata();
e1.putdata();
return 0;
}
// structures of cpp
#include<iostream>
using namespace std;
void putdata()
{
cout<<"\n details are:\n";
cout<<ename<<endl<<eid<<endl<<dept<<endl<<sal<<endl<<role;
}
};
int main()
{
emp e1; //object
cout<<"Size of structure = "<<sizeof(e1);
e1.getdata();
e1.putdata();
return 0;
}
// structures of c
//error: ‘struct emp’ has no member named ‘getdata’
// e1.getdata();
#include<stdio.h>
void putdata()
{
printf("\n details are:\n");
printf("%s %d",ename,eid);
}
};
int main()
{
struct emp e1;
e1.getdata();
e1.putdata();
return 0;
}
……..