Structures and ENUM

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Structures and ENUM

Structure is collection of simple variables with diff data types


////////////////////////////////////////////////////////////////
struct part //declare a structure
{
int modelnumber; //ID number of widget
int partnumber; //ID number of widget part
float cost; //cost of part
};
////////////////////////////////////////////////////////////////
int main()
{
part part1; //define a structure variable
part1.modelnumber = 6244; //give values to structure members
part1.partnumber = 373;
part1.cost = 217.55F;
//display structure members
cout << Model << part1.modelnumber;
cout << , part << part1.partnumber;
cout << , costs $ << part1.cost << endl;
return 0;
}

Defining structure

Structure members in memory


Access structure members

Intialization of structure members - part part1 = { 6244, 373, 217.55F };

Structures with in structures

// englarea.cpp
// demonstrates nested structures
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
struct Distance //English distance
{
int feet;

float inches;
};
////////////////////////////////////////////////////////////////
struct Room //rectangular area
{
Distance length; //length of rectangle
Distance width; //width of rectangle
};
////////////////////////////////////////////////////////////////
int main()
{
Room dining; //define a room
dining.length.feet = 13; //assign values to room
dining.length.inches = 6.5;
dining.width.feet = 10;
dining.width.inches = 0.0;
//convert length & width
float l = dining.length.feet + dining.length.inches/12;
float w = dining.width.feet + dining.width.inches/12;
//find area and display it
cout << Dining room area is << l * w
<< square feet\n ;
return 0;
}

Accessing nested structure members

Initializing nested structures - Room dining = { {13, 6.5}, {10, 0.0} };

Enumerators list of all possible values


structures can be looked at as a way to provide user-defined data types. A different
approach to defining your own data type is the enumeration.

Days of a week
enum days_of_week { Sun, Mon, Tue, Wed, Thu, Fri, Sat };

Enumerations are treated internally as integers. This explains why you can perform
arithmetic
and relational operations on them. Ordinarily the first name in the list is given the value 0,
the
next name is given the value 1, and so on

specifying integet vaue to enum - enum Suit { clubs=1, diamonds, hearts, spades };
Abstract data type

Strings

Two typoes csting array of charecters - char* strings(char str[MAX];), anf other string

String constants - char str[] = Farewell! thou art too dear for my possessing.;

Cin.get() to read string

#include <iostream>
using namespace std;
int main()
{
const int MAX = 80; //max characters in string
char str[MAX]; //string variable str
cout << \nEnter a string: ;
cin.get(str, MAX); //put string in str
cout << You entered: << str << endl;
return 0;
}

Read string with multiple lines - cin.get(str, MAX, $);

Copying string in hard way - for(int j=0; j<strlen(str1); j++) //copy strlen characters
str2[j] = str1[j];

copying string in easy way - strcpy(str2, str1);

Array of strings

char star[DAYS][MAX] = { Sunday, Monday, Tuesday,


Wednesday, Thursday,
Friday, Saturday };

Prob not possible like s1=s2

Standard c++ string class easier

//sstrass.cpp
//defining and assigning string objects
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1(Man); //initialize
string s2 = Beast; //initialize
string s3;
s3 = s1; //assign
cout << s3 = << s3 << endl;
s3 = Neither + s1 + nor ; //concatenate
s3 += s2; //concatenate
cout << s3 = << s3 << endl;
s1.swap(s2); //swap s1 and s2
cout << s1 << nor << s2 << endl;
return 0;
}

Getline to read string handles blanks and multiple lines - getline(cin, address, $);

Normal cin works

Finding string objects string.find, find_first_of


Accessing charecters of string object

Somany other string functions length,size etc

Pointers
& - get address
* - print value
Every byte in the computers memory has an address. Addresses are numbers, just as they
are for houses on a street.

Int *ptr, char *ptr

You might also like