Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  


Shop
CPP All : Structures Basics
Concept Learning Code Sheets

Structures demo program with int and char array #2767

A program using structure with two different type of variables int and char[] grouped in one structure.

#include <iostream>
using namespace std;
struct Person
{
int age;
char name[50];
};
int main()
{
Person P1,P2;
P1.age=21;P2.age=23;
cout<<"Give Name 1 : ";
cin.getline(P1.name,50);
cout<<"Give Name 2 : ";
cin.getline(P2.name,50);
cout<<P1.name<<" "<<P1.age<<endl;
cout<<P2.name<<" "<<P2.age<<endl;
return 0;
}

Creating global instances or objects of structures #2777

A technique to create instances of a structure which can be used by many functions,

#include <iostream>
using namespace std;
void setApple(void);
struct Fruit
{
int weight;
float price;
}apple,banana;

int main()
{
banana.weight=45;
banana.price=3.25;
cout<<"FruitttWeighttPrice"<<endl;
cout<<"-----------------------------"<<endl;
cout<<"Bananatt"
<<banana.weight<<"t"<<banana.price<<endl;
setApple();
cout<<"Applett"
<<apple.weight<<"t"<<apple.price<<endl;
return 0;
}

void setApple()
{
apple.weight=55;
apple.price=10.50;
}

Initialising structure variables while creating instances #2795

A demonstration of initialisation of values of variables while creating the instances of the structures.

#include <iostream>
using namespace std;
struct Monument
{
char name[50];
int year;
};

int main()
{
Monument m1={"Taj Mahal",1653};
Monument m2={"Eiffel Tower",1889};
Monument m3={"Statue of Liberty",1886};

cout<<"MonumentttYear Completedt"<<endl;
cout<<"--------------------------------------"<<endl;
cout<<m1.name<<"tt"<<m1.year<<endl;
cout<<m2.name<<"tt"<<m2.year<<endl;
cout<<m3.name<<"t"<<m3.year<<endl;
return 0;
}

Effect of partial or no intialisation of structure instance variables #2799

Understanding default values in case of partial or no initialisation of structure instances.

#include <iostream>
using namespace std;
struct Book
{
int ID;
char name[50];
int year;
float price;
};

int main()
{
//Book b1={1,"Freedom at midnight",1975,200.25};

Book b1={1};

/*
Book b1={1,"Freedom at midnight"};
Book b1={1,"Freedom at midnight"};
Book b1={1,"Freedom at midnight",1975};
*/
cout<<b1.ID<<" "<<b1.name
<<" "<<b1.year<<" "<<b1.price<<endl;

Book b2;
cout<<b2.ID<<" "<<b2.name
<<" "<<b2.year<<" "<<b2.price<<endl;

return 0;
}

Structure instance creation using pointer based notation #2911

A system of creating structure instances using pointer based notation or the method indicating the address location of the structure.

#include <iostream>
using namespace std;
struct Date
{
int day;
int month;
int year;
};

int main()
{
Date *d1=new Date();
Date *d2,d; d2=&d;
d1->day=11;d1->month=11;d1->year=2011;
d2->day=12;d2->month=12;d2->year=2012;
cout<<"Date 1= "<<d1->day<<"-"
<<d1->month<<"-"
<<d1->year<<endl;
cout<<"Date 2= "<<d.day<<"-"
<<d2->month<<"-"
<<d2->year<<endl;
return 0;
}

Structure instance copying #2916

Copying one instance of structure to another for the instances derived from the same structure definition

#include <iostream>
using namespace std;
struct Mobile
{
int screen_h;
int screen_w;
float price;
};

int main()
{
Mobile m1,m2;
m1.screen_h=1280;
m1.screen_w=720;
m1.price=7000;
m2=m1;
m2.price=8000;
cout<<"Details of Mobile 1"<<endl;
cout<<"Screen = "<<m1.screen_h<<"x"
<<m1.screen_w<<", Price = "
<<m1.price<<endl;
cout<<"Details of Mobile 2"<<endl;
cout<<"Screen = "<<m2.screen_h<<"x"
<<m2.screen_w<<", Price = "
<<m2.price<<endl;

return 0;
}

Case where structure instance copy is not allowed #2920

A program to explain the case where instance copying is not permitted.

#include <iostream>
using namespace std;
struct X{ int x;}X1;
struct Y{ int x;}Y1;
int main()
{
X1.x=10;
//Y1=X1; //This is not allowed as Y1 instance is from different structure
Y1.x=X1.x;
cout<<"X1.x ="<<X1.x<<endl;
cout<<"Y1.x ="<<Y1.x<<endl;
return 0;
}

Nesting of strucutres #2924

A program to demonstrate nested structures with one structure called within another structure.

#include <iostream>
#include <string.h>
using namespace std;
struct Name
{
char fname[40];
char lname[40];
};
struct Person
{
Name name;
int age;
};
int main()
{
Person p1;
strcpy(p1.name.fname,"Raju");
strcpy(p1.name.lname,"Verma");
p1.age=40;
cout<<p1.name.fname<<" "<<p1.name.lname
<<", "<<p1.age<<endl;
return 0;
}

Nested Structures Initialisation #2934

Program to demonstrate the initialisation process of nested structures.

#include <iostream>
using namespace std;
struct Name
{
char fname[40];
char lname[40];
};
struct Person
{
Name name;
int age;
};
int main()
{
Person p1={{"Raju","Verma"},40};
cout<<p1.name.fname<<" "<<p1.name.lname
<<", "<<p1.age<<endl;
return 0;
}

Preparing Array of Structures #2938

Program to demonstrate how an array of structures is prepared to get entry of multiple similar type of data entries.

#include <iostream>
using namespace std;
struct Student
{
char name[60];
int marks;
};

Student s[3];

int main()
{
for(int i=0;i<3;i++)
{
cout<<"Enter Student Name : ";
cin.getline(s[i].name,60);
cout<<"Enter Marks out of 100: ";
cin>>s[i].marks;
cin.ignore(100,'n');
}
cout<<endl<<"Entered data is -- "<<endl;
for(int i=0;i<3;i++)
{
cout<<s[i].name<<", "<<s[i].marks<<endl;
}
return 0;
}
Solved Problems

Collect student details to structure and update selectively #2781

Write a program to collect follow entry about an student in a structure type

Nested Structure Initialisation #2928

Write the output of the following program.

Finding errors in structure declaration #2961

Find the errors in the following structure declaration.

struct Computer 

Output writing using strcpy for structure string variables #2964

Write the output of the following program.


Code Sheets:10  Solved Problems:4 
Back