Structures were included in C and carried forward into C++.
Structures "encapsulate" data members and the member functions that operate
on that data.
Structures are similar to classes
The major difference between a struct and a class is that data members and
member functions in a struct are public by default. In a class, they are
private by default.
The struct must be placed before the main function
Remember that an array can hold only one data type. You can, however, have
an array of struct objects - a very powerful approach.
Note that the body of a structure is enclosed with braces. Structure
definitions must end with a semicolon.
Members of a structure are accessed by
Creating an object of the struct
Using the dot operator
Structures can be self-referential. This means they can have a member that
points to the structure.
struct listNode
{
int data;
listNode *next;
//next is a pointer to a listNode - more on this second semester
};
#include <iostream>
using namespace std;
struct Alien
{
int age;
double height;
int nameAlien;
void printStuff ()
{
cout << "I am alien
"<<nameAlien<<endl;
cout <<"my age is "<<age<<endl;
cout <<"my height is "<<height<<endl;
}
};
#include <iostream.h>
struct Monkey
{
int age;
int height;
void Greeting ()
{
cout <<"Hi, I am a monkey"<<endl;
}
int askQuestion()
{
int theAge;
cout<<"what is your age?"<<endl;
cin >> theAge;
return theAge;
}
};
int main ( )
{
Monkey Green;
Green.age = 1000;
int yourAge;
yourAge = Green.askQuestion();
cout <<"Your age is: "<<yourAge<<endl;
return 0;
}
#include <iostream.h>
struct fish
//Established before the main
{
int age;
//Data members
double weight;
int printAge ()
//printAge and printWeight are member functions
{
return age;
}
double printWeight ()
{
return weight;
}
};
//Note the semicolon - class also use this notation
int main ()
{
fish greenFish;
//Declaring an object of fish struct
greenFish.age = 1;
//Accessing and initializing data members through the
object
greenFish.weight = 2.67;
fish pond [10][10];
//Declaring a 2-D array of type fish
for (int i = 0;i<10;i++)
//Initializing the pond array with greenFish objects
for (int j = 0;j<10;j++)
pond [i][j] =
greenFish;
cout << "The age of the greenFish is: "<< greenFish.printAge
()<<endl;
cout << "The weight of the greenFish is:
"<<greenFish.printWeight ()<<endl;
cout << "The weight of the greenFish in 1st row, 1st col of
pond is: "<<pond[0][0].printWeight()<<endl;
return 0;
}
Create a struct
named Venusians that has
age - an integer
weight - a double
numberEyes - a double
Create a main
function that will
Part a: Sludge
age - integer
weight - double
length - double
It also has member functions as appropriate for the problem
Part b: Sludge
Battle
Modify the above
code for the Time struct to accomplish the following.
Your submission
should include output for each of the 6 cases listed below, properly
labeled.
2.
3. Incorrect standard format entered
2nd time with appropriate response.
4. Correct military format entered
along with the conversion.
6. Incorrect military format entered
2nd time along with appropriate response.