Menu

Custom Search

C++ lab Program

Laboratory Worksheet #4
Structure and Union in c++

Objectives :
A. To review structure and be familiar with its additional features in C++
B. To be familiar with unions and its application.
A.Structures in C++;
----Structure is the collection of different datatype . In structure by default all members are public where as all member are private in class by default. In C++ structure is also like a class.
.

#include
#include
struct student{

char name[30];
int roll;
float marks[3];
public:
void getdata();
void display();
/* void initialize()
{ name=”sudip”;
roll=61012;
marks={10,20,30};
}*/
};
void student :: getdata()
{
cout<<"Name :"; cin>>name;
cout<<"Roll no.:"; cin>>roll;
cout<<"Enter marks for 3 subjects"; for(int a=0;a<3;a++) { cin>>marks[a];
}
}
void student ::display()
{float sum=0.0;
cout<<"Name :"<
#include
union u_intflo
{
int intnum;
float floatnum;
};
struct s_intflo
{
int intnum;
float floatnum;
};

int main(){
u_intflo unionex;
s_intflo structex;
clrscr();
cout<<"size of unionex ="<>title;
cout<>author;
cout<>code;
cout<>price.yens;
else
cin>>price.dollars;
}

void book::display(void){
cout<
#include
#include

static union {
float dollars;
int yens;
};
struct book{
private:
char title[30];
char author[30];
char code;
public:
void getData();
void display();
};

void main(){
book OOP;
clrscr();
OOP.getData();
OOP.display();
getch();
}

void book::getData(void){
cout<<"Title: "; cin>>title;
cout<<"Author: "; cin>>author;
cout<<"Enter j/J for Japanese and a/A for American: "; cin>>code;
cout<<"Enter price: "; if(tolower(code)=='j') cin>>yens;
else
cin>>dollars;
}

void book::display(void){
cout< cout<<"Author: "< cout<<"Type: ";
if(tolower(code)=='j'){
cout<<"Japanese"< cout<<"Price: "< }
else{
cout<<"American"< cout<<"Price: "<<"$ "< }
}





No comments:

Post a Comment