CS1201: Programming Language 2 Structure By: Nouf Almunyif.

23
CS1201: Programming Language 2 Structure By: Nouf Almunyif

Transcript of CS1201: Programming Language 2 Structure By: Nouf Almunyif.

Page 1: CS1201: Programming Language 2 Structure By: Nouf Almunyif.

CS1201: Programming Language 2

Structure

By: Nouf Almunyif

Page 2: CS1201: Programming Language 2 Structure By: Nouf Almunyif.

Data Types

•C++ has several primitive data types:

bool int unsigned long int

char long int float

unsigned char unsigned short int double

short int unsigned int long double

Page 3: CS1201: Programming Language 2 Structure By: Nouf Almunyif.

Abstract data types (ADTs)

•are data types created by the programmer.

• ADTs have their own range (or domain) of data and their own set of operations that may be performed on them.▫The programmer decides what values are

acceptable for the data type▫The programmer decides what operations

may be performed on the data type

Page 4: CS1201: Programming Language 2 Structure By: Nouf Almunyif.

•C++ allows you to group several variables together into a single item known as a structure.

Page 5: CS1201: Programming Language 2 Structure By: Nouf Almunyif.

Structure•A data structure is a group of data

elements joined together under one name.• These data elements, known as members, can have different types and different lengths.

Page 6: CS1201: Programming Language 2 Structure By: Nouf Almunyif.

Structure• Data structures are declared in C++

using the following syntax:

struct structure_name {member_type1 member_name1;member_type2 member_name2;member_type3 member_name3;..} ;

Note semicolon!!!

Page 7: CS1201: Programming Language 2 Structure By: Nouf Almunyif.

StructureExample:struct PayRoll{ int EmpNumber; string Name; float Hours; float PayRate; float salary;};

Page 8: CS1201: Programming Language 2 Structure By: Nouf Almunyif.

8

empNumber

name

hours

payRate

Salary

Emp1

Structure Variable Name

Members

Page 9: CS1201: Programming Language 2 Structure By: Nouf Almunyif.

StructureExample:struct PERSON { // Declare PERSON struct type int age; // Declare member types long ss; float weight; char name[25]; } ;

int main() { PERSON brother; // C++ style structure declaration brother.age = 7; // assign values to members}

Page 10: CS1201: Programming Language 2 Structure By: Nouf Almunyif.

StructureExample:struct product { int weight; float price; } apple, banana, melon;

Page 11: CS1201: Programming Language 2 Structure By: Nouf Almunyif.

Two steps to implementing structures:

•Create the structure declaration. This establishes the tag (or name) of the structure and a list of items that are members.

•Declare variables (or instances) of the structure and use them in the program to hold data.

Page 12: CS1201: Programming Language 2 Structure By: Nouf Almunyif.

12

Accessing Structure Members

•The dot operator (.) allows you to access structure members in a program

Page 13: CS1201: Programming Language 2 Structure By: Nouf Almunyif.

13

Program#include <iostream>

Using namesspace std;

struct PayRoll

{ int EmpNumber;

string Name;

float Hours;

float PayRate;

float salary; };

void main(void)

{

PayRoll employee;

cout << "Enter the employee's number: ";

cin >> employee.empNumber;

cout << "Enter the employee's name: ";

cin>>employee.name;

cout << "How many hours did the employee work? ";

cin >> employee.hours;

cout << "What is the employee's hourly payrate? ";

cin >> employee.payRate;

employee.salary= employee.hours * employee.payRate;

cout << "Here is the employee's payroll data:\n";

cout << "Name: " << employee.name << endl;

cout << "Number: " << employee.empNumber << endl;

cout << "Hours worked: " << employee.hours << endl;

cout << "Hourly Payrate: " << employee.payRate << endl;

cout << “salary: $" << employee.salary

<< endl;

}

Page 14: CS1201: Programming Language 2 Structure By: Nouf Almunyif.

14

Program Output with Example Input

Enter the employee's number: 489 [Enter]Enter the employee's name: Jill Smith [Enter]How many hours did the employee work? 40 [Enter]What is the employee's hourly payrate? 20 [Enter]Here is the employee's payroll data:Name: Jill SmithNumber: 489Hours worked: 40Hourly Payrate: 20salary: $800.00

Page 15: CS1201: Programming Language 2 Structure By: Nouf Almunyif.

15

Displaying a Structure

•The contents of a structure variable cannot be displayed by passing the entire variable to cout. For example, assuming employee is a PayRoll structure variable, the following statement will not work:

cout << employee << endl; //won’t work!

Page 16: CS1201: Programming Language 2 Structure By: Nouf Almunyif.

16

Program#include <iostream>

#include <math.h>

struct Circle {

float radius; // القطر نصفfloat diameter; // القطر float area; //مساحة };

const float pi = 3.14159;

void main(void)

{

Circle c;

cout << "Enter the diameter of a circle: ";

cin >> c.Diameter;

c.Radius = C.Diameter / 2;

c.Area = pi * pow(c.Radius, 2.0);

cout << "The radius and area of the circle are:\n";

cout << "Radius: " << c.radius << endl;

cout << "Area: " << c.area << endl;

}

Area = pi * radius^2

Enter the diameter of a circle: 10 [Enter]The radius and area of the circle are:Radius: 5Area: 78.54

Page 17: CS1201: Programming Language 2 Structure By: Nouf Almunyif.

17

Strings as Structure Members

•When a character array is a structure member, use the same sting manipulation techniques with it as you would with any other character array.

Page 18: CS1201: Programming Language 2 Structure By: Nouf Almunyif.

18

Program// This program uses a structure to hold someone's first,// middle, and last name.

#include <iostream>#include <string.h>

struct Name{

char first[15];char middle[15];char last[15];char full[45];

};

READ

Page 19: CS1201: Programming Language 2 Structure By: Nouf Almunyif.

19

Program continues

void main(void){

Name person;cout << "Enter your first name: ";cin >> person.first;cout << "Enter your middle name: ";cin >> person.middle;cout << "Enter your last name: ";cin >> person.last;strcpy(person.full, person.first);strcat(person.full, " ");strcat(person.full, person.middle);strcat(person.full, " ");strcat(person.full, person.last);cout << "\nYour full name is " << person.full << endl;

}

READ

Page 20: CS1201: Programming Language 2 Structure By: Nouf Almunyif.

20

Program Output with Example Input

Enter your first name: Josephine [Enter]Enter your middle name: Yvonne [Enter]Enter your last name: Smith [Enter]

Your full name is Josephine Yvonne Smith

READ

Page 21: CS1201: Programming Language 2 Structure By: Nouf Almunyif.

21

Structures as Function Arguments

Structure variables may be passed as arguments to functions.

Page 22: CS1201: Programming Language 2 Structure By: Nouf Almunyif.

22

showRect(box);

void showRect(Rectangle r){

cout << r.length << endl;cout << r.width << endl;cout << r.area << endl;

}

Page 23: CS1201: Programming Language 2 Structure By: Nouf Almunyif.

23

Program #include <iostream>

struct Iitem{

int partNum;

char description[50];

int onHand;

float price; };

void ShowItem(Item); // Function prototype

void main(void)

{ Item Part = {171, "Industrial Widget", 25, 150.0};

ShowItem(part); }

void ShowItem(InvItem piece)

{ cout << "Part Number: " << piece.partNum << endl;

cout << "Description: " << piece.description << endl;

cout << "Units On Hand: " << piece.onHand << endl;

cout << "Price: $" << piece.price << endl;

}

Part Number: 171 Description: Industrial Widget Units On Hand: 25 Price: $150.00