Search
Log in
Latest topics
Statistics
We have 30 registered usersThe newest registered user is deigavr
Our users have posted a total of 191 messages in 126 subjects
Who is online?
In total there is 1 user online :: 0 Registered, 0 Hidden and 1 Guest None
Most users ever online was 125 on Wed Apr 07, 2021 9:18 am
3rd lab of 2nd semister
Page 1 of 1
3rd lab of 2nd semister
Lab # 3
Objective
Understanding the use of Structures, classes & constructors.
Theory
Strictures
“A structure is a collection of variables under a single name.”
We have seen how simple variable can hold one piece of information at a time and how arrays can hold a number of pieces of information of the same data type. These two data storage mechanisms can handle a great variety of situations. But we often want to operate on data items of different types together as a unit. In this case, neither the variable nor the array is adequate. So it is convenient to use Structure in programming languages.
The variables used in structure can be of different types, and each has a name which is used to select it from the structure. A structure is a convenient way of grouping several pieces of related information together. A structure can be defined as a new named type, thus extending the number of available types. It can use other structures, arrays or pointers as some of its members, though this can get complicated unless you are careful.
A structure type is defined by
struct struct-name {
type field-name;
type field-name;
...
}
Classes
Defines the abstract characteristics of a thing (object), including the thing's characteristics (its attributes, fields or properties) and the thing's behaviors (the things it can do, or methods, operations or features). A class is a blueprint or factory that describes the nature of something. For example, the class Dog would consist of traits shared by all dogs, such as breed and fur color (characteristics), and the ability to bark and sit (behaviors). Classes provide modularity and structure in an object-oriented computer program.
Constructors
Constructors are a new concept for people doing structured programming. Constructors do not normally exist in non-OO languages such as C and Basic. Earlier we spoke about special methods that are used to construct objects. In Java, C# and C++, as well as in other Object Oriented languages, constructors are methods that share the same name as the class and have no return type. Note again that a constructor does not have a return value, if you provide a return value, the compiler will not treat the method as a constructor.
Task
1. Write a program that maintains a Bio data of 5 persons, using Structures.
[The record includes the Name, Father’s Name, Age, NIC # etc]
2. Write a program that accepts the name, age, and basic salary from the user into a Structure. Write a Display function in the Structure to display the data accepted.
3. Create a class containing integer as its field. The class should have 2 constructors, one should initialize the field to 10 and another should take in an integer parameter and initialize the field to that parameter value. The class should also contain a method that displays the value of the field.
4. Write a program that defines a Sphere class with three constructors. The first accepts no arguments. It assumes the sphere is centered at the origin and has a radius of one unit. The second accepts one double value that represents the radius of the sphere. It assumes the sphere is centered at the origin. The third accepts four double arguments. These specify the coordinates of the center and the radius.
Objective
Understanding the use of Structures, classes & constructors.
Theory
Strictures
“A structure is a collection of variables under a single name.”
We have seen how simple variable can hold one piece of information at a time and how arrays can hold a number of pieces of information of the same data type. These two data storage mechanisms can handle a great variety of situations. But we often want to operate on data items of different types together as a unit. In this case, neither the variable nor the array is adequate. So it is convenient to use Structure in programming languages.
The variables used in structure can be of different types, and each has a name which is used to select it from the structure. A structure is a convenient way of grouping several pieces of related information together. A structure can be defined as a new named type, thus extending the number of available types. It can use other structures, arrays or pointers as some of its members, though this can get complicated unless you are careful.
A structure type is defined by
struct struct-name {
type field-name;
type field-name;
...
}
Classes
Defines the abstract characteristics of a thing (object), including the thing's characteristics (its attributes, fields or properties) and the thing's behaviors (the things it can do, or methods, operations or features). A class is a blueprint or factory that describes the nature of something. For example, the class Dog would consist of traits shared by all dogs, such as breed and fur color (characteristics), and the ability to bark and sit (behaviors). Classes provide modularity and structure in an object-oriented computer program.
Constructors
Constructors are a new concept for people doing structured programming. Constructors do not normally exist in non-OO languages such as C and Basic. Earlier we spoke about special methods that are used to construct objects. In Java, C# and C++, as well as in other Object Oriented languages, constructors are methods that share the same name as the class and have no return type. Note again that a constructor does not have a return value, if you provide a return value, the compiler will not treat the method as a constructor.
Task
1. Write a program that maintains a Bio data of 5 persons, using Structures.
[The record includes the Name, Father’s Name, Age, NIC # etc]
2. Write a program that accepts the name, age, and basic salary from the user into a Structure. Write a Display function in the Structure to display the data accepted.
3. Create a class containing integer as its field. The class should have 2 constructors, one should initialize the field to 10 and another should take in an integer parameter and initialize the field to that parameter value. The class should also contain a method that displays the value of the field.
4. Write a program that defines a Sphere class with three constructors. The first accepts no arguments. It assumes the sphere is centered at the origin and has a radius of one unit. The second accepts one double value that represents the radius of the sphere. It assumes the sphere is centered at the origin. The third accepts four double arguments. These specify the coordinates of the center and the radius.
Re: 3rd lab of 2nd semister
1. Write a program that maintains a Bio data of 5 persons, using Structures.
[The record includes the Name, Father’s Name, Age, NIC # etc]
#include
void main(void)
{
struct data
{
/*1. Write a program that maintains a Bio data of 5 persons, using Structures.
[The record includes the Name, Father’s Name, Age, NIC # etc]*/
char name[15];
char father[15];
int age;
int nic;
}dataa[5];
int i;
for(i=0;i<=4;i++)
{
printf("ENTER YOUR NAME\n");
scanf("%s",&dataa[i].name);
printf("ENTER FATHER NAME\n");
scanf("%s",&dataa[i].father);
printf("ENTER AGE\n");
scanf("%d",&dataa[i].age);
printf("ENTER NIC\n");
scanf("%d",&dataa[i].nic);
}
}
[The record includes the Name, Father’s Name, Age, NIC # etc]
#include
void main(void)
{
struct data
{
/*1. Write a program that maintains a Bio data of 5 persons, using Structures.
[The record includes the Name, Father’s Name, Age, NIC # etc]*/
char name[15];
char father[15];
int age;
int nic;
}dataa[5];
int i;
for(i=0;i<=4;i++)
{
printf("ENTER YOUR NAME\n");
scanf("%s",&dataa[i].name);
printf("ENTER FATHER NAME\n");
scanf("%s",&dataa[i].father);
printf("ENTER AGE\n");
scanf("%d",&dataa[i].age);
printf("ENTER NIC\n");
scanf("%d",&dataa[i].nic);
}
}
Re: 3rd lab of 2nd semister
#include
void main(void)
{
struct data
{
/*2. Write a program that accepts the name, age, and basic salary from the user into a Structure.
Write a Display function in the Structure to display the data accepted.*/
char name[15];
int age;
int sal;
}dataa;
{
printf("ENTER YOUR NAME\n");
scanf("%s",&dataa.name);
printf("ENTER AGE");
scanf("%d",&dataa.age);
printf("ENTER SALERY");
scanf("%d",&dataa.sal);
}
printf("\nYOUR NAME %s",dataa.name);
printf("\nYOUR AGE %d ",dataa.age);
printf("\nYOUR SALERY %d \n ",dataa.sal);
}
void main(void)
{
struct data
{
/*2. Write a program that accepts the name, age, and basic salary from the user into a Structure.
Write a Display function in the Structure to display the data accepted.*/
char name[15];
int age;
int sal;
}dataa;
{
printf("ENTER YOUR NAME\n");
scanf("%s",&dataa.name);
printf("ENTER AGE");
scanf("%d",&dataa.age);
printf("ENTER SALERY");
scanf("%d",&dataa.sal);
}
printf("\nYOUR NAME %s",dataa.name);
printf("\nYOUR AGE %d ",dataa.age);
printf("\nYOUR SALERY %d \n ",dataa.sal);
}
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Mon Aug 01, 2011 11:44 pm by Guest
» Netbook Brands
Mon Aug 01, 2011 5:39 am by Guest
» backlinks checker backlink service
Sun Jul 31, 2011 8:56 am by Guest
» how to buy facebook fans f4
Sat Jul 30, 2011 2:34 pm by Guest
» Alle bijzondere dingen in de zaanstreek
Sat Jul 30, 2011 8:11 am by Guest
» HERE YOU CAN POST ALL WEBSITE LINKS...
Wed Aug 26, 2009 2:16 pm by onesimpletech
» cool web site for all must check it
Mon Jul 06, 2009 2:32 pm by Snopy Cobra
» poetry spirit
Mon Jul 06, 2009 2:31 pm by Snopy Cobra
» best to learn VC++
Sun May 10, 2009 3:22 pm by Guest