SlideShare a Scribd company logo
Mohammad Shaker 
mohammadshaker.com 
@ZGTRShaker 
2010, 11, 12, 13, 14 
C++ 
Programming Language 
L07 -STRUCTS
Structs
Float, double, long double 
C++ data types 
Structured 
Simple 
Address 
Pointer 
Reference 
enum 
Floating 
Array 
Struct 
Union 
Class 
Char, Short, int, long, bool 
Integral
Structs 
•Differentiate: 
–Array: 
•Is a collection of variables of the same type 
–Struct“Structure” 
•Is a collection of variable of one or more type 
•Structis definition not a declaration 
–No memory allocation 
•“Memory is allocated just when we declare a variable” 
–Examples: 
•Student record, banks, players, addresses
Structs 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
structMyInner 
{ 
intKey; 
intSalary; 
}; 
structEmployee 
{ 
MyInner info; 
charName[20]; 
}; 
} 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 0; 
structEmployee 
{ 
inta, b, c; 
} TempStruct [2] = {{2,4,5}, {1,6,8}}; 
cout << TempStruct[i].a << endl; 
cout << TempStruct[i].b << endl; 
cout << TempStruct[i].c << endl; 
} 
2 
4 
5 
Press any key to continue
Structs 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 0; 
structEmployee 
{ 
inta, b, c; 
} TempStruct [2] = {{2,4}, {1,6,8}}; 
cout << TempStruct[i].a << endl; 
cout << TempStruct[i].b << endl; 
cout << TempStruct[i].c << endl; 
} 
2 
4 
0 
Press any key to continue 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 1; 
structEmployee 
{ 
inta, b, c; 
} TempStruct [2] = {{2,4} }; 
cout << TempStruct[i].a << endl; 
cout << TempStruct[i].b << endl; 
cout << TempStruct[i].c << endl; 
} 
0 
0 
0 
Press any key to continue
Structs 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 1; 
structEmployee 
{ 
inta, b, c; 
}; 
Employee TempStruct [2] = {{2,4} }; 
cout << TempStruct[i].a << endl; 
cout << TempStruct[i].b << endl; 
cout << TempStruct[i].c << endl; 
} 
0 
0 
0 
Press any key to continue 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 1; 
structEmployee 
{ 
inta, b, c; 
} TempStruct [2] = {{2,4} } 
cout << TempStruct[i].a << endl; 
cout << TempStruct[i].b << endl; 
cout << TempStruct[i].c << endl; 
} 
Compiler error. 
Missing semi colon be4 cout “;” (After TempStruct)
Structs 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 1; 
structEmployee 
{ 
inta, b, c; 
} TempStruct [2] = {{2,4} } 
cout << TempStruct[i].a << endl; 
cout << TempStruct[i].b << endl; 
cout << TempStruct[i].c << endl; 
} 
Compiler error. 
Missing semi colon be4 cout “;” (After TempStruct) 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 0; 
struct 
{ 
inta, b, c; 
} TempStruct [2] = {{2,8} }; 
cout << TempStruct[i].a << endl; 
cout << TempStruct[i].b << endl; 
cout << TempStruct[i].c << endl; 
} 
2 
8 
0 
Press any key to continue
Structs 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = -1; 
struct 
{ 
inta, b, c; 
} TempStruct [2] = {{2,8} }; 
TempStruct[i].c = 67; 
cout << TempStruct[i].a << endl; 
cout << TempStruct[i].b << endl; 
cout << TempStruct[i].c << endl; 
} 
Compiler error. When not writing the name of the struct we should declare variable immediately 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = -1; 
struct 
{ 
inta, b, c; 
} TempStruct [2] = {{2,8} }; 
cout << TempStruct[i].a << endl; 
cout << TempStruct[i].b << endl; 
cout << TempStruct[i].c << endl; 
} 
2030155668 
1 
1698745032 
Press any key to continue
Structs 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 0; 
struct 
{ 
inta, b, c; 
}; 
TempStruct [2] = {{2,8} }; 
cout << TempStruct[i].a << endl; 
cout << TempStruct[i].b << endl; 
cout << TempStruct[i].c << endl; 
} 
1231378292 
1 
1689373384 
Press any key to continue 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 1; 
struct 
{ 
inta, b, c; 
} TempStruct [2] = {{2,8} }; 
TempStruct[i].c = 67; 
cout << TempStruct[i].a << endl; 
cout << TempStruct[i].b << endl; 
cout << TempStruct[i].c << endl; 
} 
0 
0 
67 
Press any key to continue
Structs 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 1; 
struct 
{ 
inta, b, c; 
} TempStruct [2] = {{2,8} }; 
TempStruct.c = 67; 
cout << TempStruct[i].a << endl; 
cout << TempStruct[i].b << endl; 
cout << TempStruct[i].c << endl; 
} 
Compiler error 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 1; 
struct 
{ 
inta, b, c; 
} TempStruct [2] = {{2,8} }; 
TempStruct[i].c = 67; 
cout << TempStruct << endl; 
} 
001BF048 
Press any key to continue
Structs 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
structstudent 
{ 
charName[20]; 
intNum; 
intid; 
}; 
student st1 = {"mee", 196, 7}; 
} 
Compile and run 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
structstudent 
{ 
charName[20]; 
intNum; 
intid; 
}; 
student st1 = {"mee", 196, 7}; 
st1 = {"zee", 111, 4}; 
} 
Compiler error
Structs 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
structstudent 
{ 
charName[20]; 
intNum; 
intid; 
}; 
student st1 = {"mee", 196, 7}; 
student st2 = {"zee", 111, 4}; 
student Tempst1 = {"mee", 196, 7}; 
if(st1.Num == Tempst1.Num) 
{ 
cout << "Yeah!"<< endl; 
} 
else 
{ 
cout << "No!"<< endl; 
} 
} 
Yeah! 
because Structs are “value” type unlike classes which are “reference”type 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
structstudent 
{ 
charName[20]; 
intNum; 
intid; 
}; 
student st1 = {"mee", 196, 7}; 
student st2 = {"zee", 111, 4}; 
student Tempst1 = {"mee", 196, 7}; 
if (st1 == Tempst1) 
{ 
cout << "Yeah!"<< endl; 
} 
else 
{ 
cout << "No!"<< endl; 
} 
} 
Compiler error
Structs 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
structstudent 
{ 
charName[20]; 
intNum; 
intid; 
}; 
student st1 = {"mee", 196, 7}; 
student st2 = {"zee", 111, 4}; 
student Tempst1 = {"mee", 196, 7}; 
if(st1.Num = st2.Num) 
{ 
cout << "Yeah!"<< endl; 
} 
else 
{ 
cout << "No!"<< endl; 
} 
cout << st1.Num << endl; 
cout << st2.Num << endl; 
} 
Yeah! 
111 
111
structsand Pointers
Structs 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{structInner 
{ 
intNum; 
intid; 
}; 
structstudent 
{ 
charName[20]; 
Inner info; 
}; 
student st1 = {"mee", { 11, 3 } }; 
student st2 = {"zee", 111, 4}; 
student &Tempst1 = st1; 
cout << st1. Name << endl; 
cout << st1.info. Num++ << endl; 
cout << st1.info.id << endl; 
cout << ++Tempst1.info. Num << endl; 
} 
mee 
11 
3 
13 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{structInner 
{ 
intNum; 
intid; 
}; 
structstudent 
{ 
charName[20]; 
Inner info; 
}; 
student st1 = {"mee", { 11, 3 } }; 
student st2 = {"zee", 111, 4}; 
student &Tempst1 = st1; 
cout << st1. Name << endl; 
cout << st1. Num++ << endl; 
cout << st1.info.id << endl; 
cout << ++Tempst1.info.Num << endl; 
} 
Compiler error
Structs 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
structInner 
{ 
intNum; 
intid; 
}; 
structstudent 
{ 
charName[20]; 
Inner info; 
}; 
student st1 = {"mee", 11, 3 }; 
student st2 = {"zee", 111, 4}; 
student &Tempst1 = st1; 
cout << st1. Name << endl; 
cout << st2.info. Num++ << endl; 
cout << st2. info. id << endl; 
cout << ++Tempst1. info. Num << endl; 
} 
mee 
111 
4 
12 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
structInner 
{ 
intNum; 
intid; 
}; 
structstudent 
{ 
string Name; 
Inner info; 
}; 
student st1 = {"mee", 11, 3 }; 
student st2 = {"zee", 111, 4}; 
student &Tempst1 = st1; 
st1.Name = "foo"; 
cout << st1.Name << endl; 
} 
Compiler error
Structs 
#include<iostream> 
#include<string> 
usingnamespace::std; 
voidmain(void) 
{ 
structInner 
{ 
intNum; 
intid; 
}; 
structstudent 
{ 
string Name; 
Inner info; 
}; 
student st1 = {"mee", 11, 3 }; 
student st2 = {"zee", 111, 4}; 
student &Tempst1 = st1; 
st1.Name = "foo"; 
cout << st1.Name << endl; 
} 
foo 
#include<iostream> 
#include<string> 
usingnamespace::std; 
voidmain(void) 
{ 
structInner 
{ 
intNum; 
intid; 
}; 
structstudent 
{ 
string Name; 
Inner info; 
}; 
student st1 = {"mee", 11, 3 }; 
student st2 = {"zee", 111, 4}; 
student &Tempst1 = st1; 
st1.Name = "foo"; 
cout << st1 << endl; 
cout << st1.Name << endl; 
} 
Compiler error
Structas Function Parameter
Structs 
#include<iostream> 
#include<string> 
usingnamespace::std; 
structInner{intNum;intid;}; 
structstudent{string Name;Inner info;}; 
student Print(student stu) 
{ 
cout << stu.Name << endl; 
returnstu; 
} 
voidmain(void) 
{ 
student st1 = {"mee", 11, 3 }; 
student st2 = {"zee", 111, 4}; 
student &Tempst1 = st1; 
Print(st1); 
} 
mee 
#include<iostream> 
#include<string> 
usingnamespace::std; 
student Print(student stu) 
{ 
cout << stu.Name << endl; 
returnstu; 
} 
voidmain(void) 
{ 
structInner 
{intNum;intid;}; 
structstudent 
{string Name;Inner info;}; 
student st1 = {"mee", 11, 3 }; 
student st2 = {"zee", 111, 4}; 
student &Tempst1 = st1; 
Print(st1); 
} 
Compiler error 
Inner student are in main and not global to let the function see them!
Structs 
#include<iostream> 
#include<string> 
usingnamespace::std; 
structInner 
{ 
intNum; 
intid; 
}; 
structstudent 
{ 
string Name; 
Inner info; 
}; 
student Print(student stu) 
{ 
cout << stu.Name << endl; 
stu.Name = "GoGo"; 
returnstu; 
} 
voidmain(void) 
{student st1 = {"mee", 11, 3 }; 
student st2 = {"zee", 111, 4}; 
student &Tempst1 = st1; 
Print(st1); 
cout << st1.Name << endl; 
} 
mee 
mee 
#include<iostream> 
#include<string> 
usingnamespace::std; 
structInner 
{ 
intNum; 
intid; 
}; 
structstudent 
{ 
string Name; 
Inner info; 
}; 
student Print(student &stu) 
{ 
cout << stu.Name << endl; 
stu.Name = "GoGo"; 
returnstu; 
} 
voidmain(void) 
{student st1 = {"mee", 11, 3 }; 
student st2 = {"zee", 111, 4}; 
student &Tempst1 = st1; 
Print(st1); 
cout << st1.Name << endl; 
} 
mee 
GoGo
Structs 
#include<iostream> 
#include<string> 
usingnamespace::std; 
structInner 
{intNum;intid;}; 
structstudent 
{string Name;Inner info; }; 
student Go() 
{ 
student stu; 
cout << stu.Name << endl; 
stu.Name = "GoGo"; 
returnstu; 
} 
voidmain(void) 
{student st1 = {"mee", 11, 3 }; 
student st2 = {"zee", 111, 4}; 
st1=st2; 
cout << st1.Name << endl; 
student &Tempst1 = st1; 
st1=Go(); 
cout << st1.Name << endl; 
} 
zee 
GoGo
Pointers and Structs
Pointers and Structs 
#include<iostream> 
#include<string> 
usingnamespace::std; 
structInner 
{intNum;intid;}; 
structstudent 
{string Name;Inner info;}; 
voidmain(void) 
{ 
student st1 = {"mee", 196, 7}; 
student *ptr = &st1; 
cout << ptr << endl; 
} 
Compile and run 
#include<iostream> 
#include<string> 
usingnamespace::std; 
structInner 
{intNum;intid;}; 
structstudent 
{string Name;Inner info;}; 
voidmain(void) 
{ 
student st1 = {"mee", 196, 7}; 
student *ptr = &st1; 
cout << ptr << endl; 
cout << *ptr << endl; 
} 
Compiler error 
structInner 
{intNum;intid;}; 
structstudent 
{string Name;Inner info;}; 
voidmain(void) 
{ 
student st1 = {"mee", 196, 7}; 
student *ptr = &st1; 
cout << ptr << endl; 
cout << ptr->Name << endl; 
cout << ptr->info.id << endl; 
} 
0025F27C 
mee 
7 
structInner 
{intNum;intid;}; 
structstudent 
{string Name;Inner info;}; 
voidmain(void) 
{ 
student st1 = {"mee", 196, 7}; 
student *ptr = &st1; 
cout << ptr << endl; 
cout << ptr->Name << endl; 
cout << ptr->info->id << endl; 
} 
Compiler error, -> before info
Pointers and Structs 
structInner 
{intNum;intid;}; 
structstudent 
{string Name;Inner info;}; 
voidmain(void) 
{ 
student st1 = {"mee", 196, 7}; 
student *ptr = &st1; 
cout << ptr << endl; 
cout << *(ptr->Name) << endl; 
cout << *(ptr->info.id) << endl; 
} 
Compiler error 
structInner 
{intNum;intid;}; 
structstudent 
{string Name;Inner info;}; 
voidmain(void) 
{ 
student st1 = {"mee", 196, 7}; 
student *ptr = &st1; 
cout << (*ptr).Name << endl; 
cout << (*ptr).info.id << endl; 
} 
mee 
7 
structInner 
{intNum;intid;}; 
structstudent 
{string Name;Inner info;}; 
voidmain(void) 
{ 
student st1 = {"mee", 196, 7}; 
student *ptr = &st1; 
cout << (*ptr)->Name << endl; 
cout << (*ptr)->info.id << endl; 
} 
Compiler error
Pointers and Structs 
#include<iostream> 
#include<string> 
usingnamespace::std; 
structInner 
{intNum; intArr[3];}; 
structstudent 
{string Name;Inner info;}; 
voidCount(student stu) 
{ 
inttotal = 0; 
for(inti = 0; i<3; i++) 
{ 
total += stu.info.Arr[i]; 
} 
cout << total << endl; 
stu.info.Arr[0]=3; 
} 
voidmain(void) 
{ 
student st1 = {"mee", 196, 1,3,3}; 
Count(st1); 
Count(st1); 
} 
7 
7 
#include<iostream> 
#include<string> 
usingnamespace::std; 
structInner 
{intNum; intArr[3];}; 
structstudent 
{string Name;Inner info;}; 
voidCount(student *stu) 
{ 
inttotal = 0; 
for(inti = 0; i<3; i++) 
{ 
total += stu->info.Arr[i]; 
} 
cout << total << endl; 
stu->info.Arr[0]=3; 
} 
voidmain(void) 
{ 
student st = {"mee", 196, 1,3,3}; 
student *st1 = &st; 
Count(st1); 
Count(st1); 
} 
7 
9
Pointers and Structs 
#include<iostream> 
#include<string> 
usingnamespace::std; 
structInner 
{intNum; intArr[3];}; 
structstudent 
{string Name;Inner info;}; 
voidCount(student *&stu) 
{ 
inttotal = 0; 
for(inti = 0; i<3; i++) 
{ 
total += stu->info.Arr[i]; 
} 
cout << total << endl; 
stu->info.Arr[0]=3; 
} 
voidmain(void) 
{ 
student st = {"mee", 196, 1,3,3}; 
student *st1 = &st; 
Count(st1); 
Count(st1); 
} 
7 
9 
#include<iostream> 
#include<string> 
usingnamespace::std; 
structInner 
{intNum; intArr[3];}; 
structstudent 
{string Name;Inner info;}; 
voidCount(student &stu) 
{ 
inttotal = 0; 
for(inti = 0; i<3; i++) 
{ 
total += stu.info.Arr[i]; 
} 
cout << total << endl; 
stu.info.Arr[0]=3; 
} 
voidmain(void) 
{ 
student st1 = {"mee", 196, 1,3,3}; 
Count(st1); 
Count(st1); 
} 
7 
9
StructsMembers and Functions
StructsMembers and Functions 
#include<iostream> 
#include<string> 
usingnamespace::std; 
structstudent 
{ 
string Name; 
voidprint() 
{ 
cout << Name << endl; 
} 
}; 
voidmain(void) 
{ 
student st1 = {"mee"}; 
st1.print(); 
} 
mee 
#include<iostream> 
#include<string> 
usingnamespace::std; 
structstudent 
{ 
string Name; 
voidprint() 
{ 
cout << student.Name << endl; 
} 
}; 
voidmain(void) 
{ 
student st1 = {"mee"}; 
st1.print(); 
} 
Compiler error 
Function print is implicitely inline
StructsMembers and Functions 
#include<iostream> 
#include<string> 
usingnamespace::std; 
structstudent 
{ 
string Name; 
voidprint() 
{ 
cout << Name << endl; 
} 
voidChangeName(string str) 
{ 
Name = str; 
} 
}; 
voidmain(void) 
{ 
string NewName= "zee"; 
student st1 = {"mee"}; 
st1.print(); 
st1.ChangeName(NewName); 
st1.print(); 
} 
mee 
zee
StructsMembers and Functions 
#include<iostream> 
#include<string> 
usingnamespace::std; 
structstudent 
{ 
string Name; 
voidstudent::print(); 
voidChangeName(string str); 
}; 
voidstudent::print() 
{ 
cout << Name << endl; 
} 
voidstudent::ChangeName(string str) 
{ 
Name = str+Name; 
} 
voidmain(void) 
{ 
string NewName= "zee"; 
student st1 = {"mee"}; 
st1.print(); 
st1.ChangeName(“foo”); 
st1.print(); 
} 
mee 
foomee 
Now the functions are not implicitely inline 
#include<iostream> 
#include<string> 
usingnamespace::std; 
structstudent 
{ 
string Name; 
voidstudent::print(); 
voidChangeName(string str); 
}; 
voidstudent::print() 
{ 
cout << Name << endl; 
} 
voidstudent::ChangeName(string str) 
{ 
Name = str; 
} 
voidmain(void) 
{ 
string NewName= "zee"; 
student st1 = {"mee"}; 
st1.print(); 
st1.ChangeName(“foo”); 
st1.print(); 
} 
mee 
foo 
Now the functions are not implicitely inline
StructsMembers and Functions 
•privatedata members 
–Private access 
•Can be used by a few categories of functions 
•publicdata members 
–Public access 
•Can be used by all code
StructsMembers and Functions 
#include<iostream> 
#include<string> 
usingnamespace::std; 
structstudent 
{ 
private: 
intTempID; 
public: 
intID; 
string Name; 
voidChangeID(intn); 
voidprint(); 
voidChangeName(string str); 
}; 
voidstudent::print() 
{cout << Name << endl; } 
voidstudent::ChangeName(string str) 
{Name = str+Name;} 
voidstudent::ChangeID(intn) 
{TempID = 2; 
ID = n+TempID; 
cout << "Your ID = "<< ID << endl;} 
voidmain(void) 
{intn = 3; string NewName= "zee"; 
student st1;st1.print(); 
st1.ChangeName(NewName);st1.print(); TempID=5; 
st1.ChangeID(n); 
} 
Compiler error 
Un delcared identifier 
#include<iostream> 
#include<string> 
usingnamespace::std; 
structstudent 
{ 
private: 
intTempID; 
public: 
intID; 
string Name; 
voidChangeID(intn); 
voidprint(); 
voidChangeName(string str); 
}; 
voidstudent::print() 
{cout << Name << endl; } 
voidstudent::ChangeName(string str) 
{Name = str+Name;} 
voidstudent::ChangeID(intn) 
{TempID = 2; 
ID = n+TempID; 
cout << "Your ID = "<< ID << endl;} 
voidmain(void) 
{intn = 3; string NewName= "zee"; 
student st1;st1.print(); 
st1.ChangeName(NewName);st1.print(); 
st1.ChangeID(n); 
} 
zee 
Your ID = 5
StructsMembers and Functions 
#include<iostream> 
#include<string> 
usingnamespace::std; 
structstudent 
{ 
private: 
intTempID; 
public: 
intID; 
string Name; 
voidChangeID(intn); 
voidprint(); 
voidChangeName(string str); 
}; 
voidstudent::print() 
{cout << Name << endl; } 
voidstudent::ChangeName(string str) 
{Name = str+Name;} 
voidstudent::ChangeID(intn) 
{TempID = 2; 
ID = n+TempID; 
cout << "Your ID = "<< ID << endl;} 
voidmain(void) 
{intn = 3; string NewName= "zee"; 
student st1;st1.print(); 
st1.ChangeName(NewName);st1.print(); st1.TempID=5; st1.ChangeID(n);} 
Compiler error 
Can’t access private data memebers
Quiz
Quiz 1, 2, 3, 4 
#include <iostream> 
using namespace::std; 
void main(void) 
{ 
inti= 1; 
structMyFirstS 
{ 
inta, b, c; 
} TempStruct[2] = {{2,4} } 
cout<< TempStruct[i].a << endl; 
cout<< TempStruct[i].b << endl; 
cout<< TempStruct[i].c << endl; 
} 
Compiler error Missing; 
#include <iostream> 
using namespace::std; 
void main(void) 
{ 
inti= 0; 
struct 
{ 
inta, b, c; 
}; 
TempStruct[2] = {2,8,10}; 
cout<< TempStruct[i].a << endl; 
cout<< TempStruct[i].b << endl; 
cout<< TempStruct[i].c << endl; 
} 
Compiler error, TempStructhas no type! 
#include <iostream> 
using namespace::std; 
void main(void) 
{ 
string NewName= “wa3wa3" 
structstudent 
{ 
char Name[20]; 
intNum; 
intid; 
}; 
student st1 = {"mee", 196, 7}; 
st1 = {NewName, 111, 4}; 
} 
Compiler error 
structInner 
{intNum;intid;}; 
structstudent 
{string Name;Inner info;}; 
voidmain(void) 
{ 
student st1 = {"mee", 196, 7}; 
student *ptr = &st1; 
cout << ptr << endl; 
cout << ptr->Name << endl; 
cout << ptr->info->id << endl; 
} 
Compiler error, -> before info
Quiz 5, 6 
#include <iostream> 
#include <string> 
using namespace::std; 
structInner 
{intNum; intArr[3];}; 
structstudent 
{string Name;Inner info;}; 
void Count(student &stu) 
{ 
inttotal = 0; 
for (int i = 0; i<3; i++) 
{ 
total += 2*stu.info.Arr[i]; 
} 
cout<< total << endl; 
stu.info.Arr[1]=-3; 
} 
void main(void) 
{ 
student st1 = {"mee", 196, 1,3,3}; 
Count(st1); 
Count(st1); 
} 
14 
2 
#include <iostream> 
#include <string> 
using namespace::std; 
structstudent 
{public: 
intID; string Name; 
void ChangeID(intn); 
void print(); 
void ChangeName(string str); 
}; 
void student::print() 
{cout<< Name << endl; } 
void student::ChangeName(string str) 
{Name = str+Name;} 
void student::ChangeID(intn) 
{ 
static intTempID= 4; 
TempID+= 2; 
ID = n+TempID; 
cout<< "Your ID = " << ID << endl;} 
void main(void) 
{int n = 3; string NewName= "zee"; 
student st1;st1.print(); 
st1.ChangeName(NewName);st1.print(); 
st1.ChangeID(n); 
n = 5; 
st1.ChangeName(NewName);st1.print(); 
st1.ChangeID(n);} 
zee 
Your ID = 9 
zeezee 
Your ID = 13

More Related Content

PDF
C++ L03-Control Structure
PDF
C++ L04-Array+String
PDF
C++ L06-Pointers
PDF
C++ L02-Conversion+enum+Operators
PDF
C++ L05-Functions
PDF
C++ L01-Variables
PDF
C++ L08-Classes Part1
PDF
C++ L11-Polymorphism
C++ L03-Control Structure
C++ L04-Array+String
C++ L06-Pointers
C++ L02-Conversion+enum+Operators
C++ L05-Functions
C++ L01-Variables
C++ L08-Classes Part1
C++ L11-Polymorphism

What's hot (20)

PDF
C++ L09-Classes Part2
PDF
c programming
PDF
c programming
PPTX
Unit 3
PDF
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
PDF
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
PDF
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
PDF
Modern C++ Concurrency API
PDF
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
PDF
C++ L10-Inheritance
PPTX
C++ Lambda and concurrency
PDF
Programming with GUTs
PDF
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
PPTX
C++ Pointers
PDF
C++ Programming - 11th Study
PDF
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
DOC
C tech questions
PDF
Functional C++
PDF
What We Talk About When We Talk About Unit Testing
PDF
54602399 c-examples-51-to-108-programe-ee01083101
C++ L09-Classes Part2
c programming
c programming
Unit 3
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Modern C++ Concurrency API
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
C++ L10-Inheritance
C++ Lambda and concurrency
Programming with GUTs
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
C++ Pointers
C++ Programming - 11th Study
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
C tech questions
Functional C++
What We Talk About When We Talk About Unit Testing
54602399 c-examples-51-to-108-programe-ee01083101
Ad

Viewers also liked (7)

PDF
agile-planning
PDF
YEG-Agile-planning
PDF
Refactoring
PDF
Agile requirements
PDF
PRDCW-advanced-design-patterns
PDF
Agile planning
PDF
sdec11-Advanced-design-patterns
agile-planning
YEG-Agile-planning
Refactoring
Agile requirements
PRDCW-advanced-design-patterns
Agile planning
sdec11-Advanced-design-patterns
Ad

Similar to C++ L07-Struct (20)

PPTX
programming fundamentals
PPT
FP 201 - Unit4 Part 2
PPTX
Pf cs102 programming-10 [structs]
PDF
Chapter 4 - Structures - Student.pdf - slides
PPTX
Programming Fundamentals lecture-24-1.pptx
PPTX
CHAPTER -4-class and structure.pptx
PPTX
Ch7Structs.pptx
PDF
Codes on structures
PPTX
Cs1123 12 structures
PPTX
Chapter4.pptx
PPTX
12Structures.pptx
PPTX
Structure in C++ in the name of the lord
PPTX
Chapter 2 part II array and structure.pptx
PPT
CHAPTER-7 C++ PROGRAMMING ( STRUCTURE IN C++)
PPTX
Structures and Unions
PPT
Structure in programming in c or c++ or c# or java
PPT
Structure in c
PPTX
ECE2102-Week13 - 14-Strhhhhhhhjjjucts.pptx
PDF
Pointers and Structures
PPT
Struct
programming fundamentals
FP 201 - Unit4 Part 2
Pf cs102 programming-10 [structs]
Chapter 4 - Structures - Student.pdf - slides
Programming Fundamentals lecture-24-1.pptx
CHAPTER -4-class and structure.pptx
Ch7Structs.pptx
Codes on structures
Cs1123 12 structures
Chapter4.pptx
12Structures.pptx
Structure in C++ in the name of the lord
Chapter 2 part II array and structure.pptx
CHAPTER-7 C++ PROGRAMMING ( STRUCTURE IN C++)
Structures and Unions
Structure in programming in c or c++ or c# or java
Structure in c
ECE2102-Week13 - 14-Strhhhhhhhjjjucts.pptx
Pointers and Structures
Struct

More from Mohammad Shaker (20)

PDF
12 Rules You Should to Know as a Syrian Graduate
PDF
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
PDF
Interaction Design L06 - Tricks with Psychology
PDF
Short, Matters, Love - Passioneers Event 2015
PDF
Unity L01 - Game Development
PDF
Android L07 - Touch, Screen and Wearables
PDF
Interaction Design L03 - Color
PDF
Interaction Design L05 - Typography
PDF
Interaction Design L04 - Materialise and Coupling
PDF
Android L05 - Storage
PDF
Android L04 - Notifications and Threading
PDF
Android L09 - Windows Phone and iOS
PDF
Interaction Design L01 - Mobile Constraints
PDF
Interaction Design L02 - Pragnanz and Grids
PDF
Android L10 - Stores and Gaming
PDF
Android L06 - Cloud / Parse
PDF
Android L08 - Google Maps and Utilities
PDF
Android L03 - Styles and Themes
PDF
Android L02 - Activities and Adapters
PDF
Android L01 - Warm Up
12 Rules You Should to Know as a Syrian Graduate
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Interaction Design L06 - Tricks with Psychology
Short, Matters, Love - Passioneers Event 2015
Unity L01 - Game Development
Android L07 - Touch, Screen and Wearables
Interaction Design L03 - Color
Interaction Design L05 - Typography
Interaction Design L04 - Materialise and Coupling
Android L05 - Storage
Android L04 - Notifications and Threading
Android L09 - Windows Phone and iOS
Interaction Design L01 - Mobile Constraints
Interaction Design L02 - Pragnanz and Grids
Android L10 - Stores and Gaming
Android L06 - Cloud / Parse
Android L08 - Google Maps and Utilities
Android L03 - Styles and Themes
Android L02 - Activities and Adapters
Android L01 - Warm Up

Recently uploaded (20)

PDF
PPT on Performance Review to get promotions
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
Welding lecture in detail for understanding
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
composite construction of structures.pdf
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Construction Project Organization Group 2.pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Geodesy 1.pptx...............................................
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPT on Performance Review to get promotions
Lecture Notes Electrical Wiring System Components
Welding lecture in detail for understanding
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
composite construction of structures.pdf
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Embodied AI: Ushering in the Next Era of Intelligent Systems
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
CH1 Production IntroductoryConcepts.pptx
bas. eng. economics group 4 presentation 1.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Construction Project Organization Group 2.pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Geodesy 1.pptx...............................................
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Strings in CPP - Strings in C++ are sequences of characters used to store and...
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf

C++ L07-Struct

  • 1. Mohammad Shaker mohammadshaker.com @ZGTRShaker 2010, 11, 12, 13, 14 C++ Programming Language L07 -STRUCTS
  • 3. Float, double, long double C++ data types Structured Simple Address Pointer Reference enum Floating Array Struct Union Class Char, Short, int, long, bool Integral
  • 4. Structs •Differentiate: –Array: •Is a collection of variables of the same type –Struct“Structure” •Is a collection of variable of one or more type •Structis definition not a declaration –No memory allocation •“Memory is allocated just when we declare a variable” –Examples: •Student record, banks, players, addresses
  • 5. Structs #include<iostream> usingnamespace::std; voidmain(void) { structMyInner { intKey; intSalary; }; structEmployee { MyInner info; charName[20]; }; } #include<iostream> usingnamespace::std; voidmain(void) { inti = 0; structEmployee { inta, b, c; } TempStruct [2] = {{2,4,5}, {1,6,8}}; cout << TempStruct[i].a << endl; cout << TempStruct[i].b << endl; cout << TempStruct[i].c << endl; } 2 4 5 Press any key to continue
  • 6. Structs #include<iostream> usingnamespace::std; voidmain(void) { inti = 0; structEmployee { inta, b, c; } TempStruct [2] = {{2,4}, {1,6,8}}; cout << TempStruct[i].a << endl; cout << TempStruct[i].b << endl; cout << TempStruct[i].c << endl; } 2 4 0 Press any key to continue #include<iostream> usingnamespace::std; voidmain(void) { inti = 1; structEmployee { inta, b, c; } TempStruct [2] = {{2,4} }; cout << TempStruct[i].a << endl; cout << TempStruct[i].b << endl; cout << TempStruct[i].c << endl; } 0 0 0 Press any key to continue
  • 7. Structs #include<iostream> usingnamespace::std; voidmain(void) { inti = 1; structEmployee { inta, b, c; }; Employee TempStruct [2] = {{2,4} }; cout << TempStruct[i].a << endl; cout << TempStruct[i].b << endl; cout << TempStruct[i].c << endl; } 0 0 0 Press any key to continue #include<iostream> usingnamespace::std; voidmain(void) { inti = 1; structEmployee { inta, b, c; } TempStruct [2] = {{2,4} } cout << TempStruct[i].a << endl; cout << TempStruct[i].b << endl; cout << TempStruct[i].c << endl; } Compiler error. Missing semi colon be4 cout “;” (After TempStruct)
  • 8. Structs #include<iostream> usingnamespace::std; voidmain(void) { inti = 1; structEmployee { inta, b, c; } TempStruct [2] = {{2,4} } cout << TempStruct[i].a << endl; cout << TempStruct[i].b << endl; cout << TempStruct[i].c << endl; } Compiler error. Missing semi colon be4 cout “;” (After TempStruct) #include<iostream> usingnamespace::std; voidmain(void) { inti = 0; struct { inta, b, c; } TempStruct [2] = {{2,8} }; cout << TempStruct[i].a << endl; cout << TempStruct[i].b << endl; cout << TempStruct[i].c << endl; } 2 8 0 Press any key to continue
  • 9. Structs #include<iostream> usingnamespace::std; voidmain(void) { inti = -1; struct { inta, b, c; } TempStruct [2] = {{2,8} }; TempStruct[i].c = 67; cout << TempStruct[i].a << endl; cout << TempStruct[i].b << endl; cout << TempStruct[i].c << endl; } Compiler error. When not writing the name of the struct we should declare variable immediately #include<iostream> usingnamespace::std; voidmain(void) { inti = -1; struct { inta, b, c; } TempStruct [2] = {{2,8} }; cout << TempStruct[i].a << endl; cout << TempStruct[i].b << endl; cout << TempStruct[i].c << endl; } 2030155668 1 1698745032 Press any key to continue
  • 10. Structs #include<iostream> usingnamespace::std; voidmain(void) { inti = 0; struct { inta, b, c; }; TempStruct [2] = {{2,8} }; cout << TempStruct[i].a << endl; cout << TempStruct[i].b << endl; cout << TempStruct[i].c << endl; } 1231378292 1 1689373384 Press any key to continue #include<iostream> usingnamespace::std; voidmain(void) { inti = 1; struct { inta, b, c; } TempStruct [2] = {{2,8} }; TempStruct[i].c = 67; cout << TempStruct[i].a << endl; cout << TempStruct[i].b << endl; cout << TempStruct[i].c << endl; } 0 0 67 Press any key to continue
  • 11. Structs #include<iostream> usingnamespace::std; voidmain(void) { inti = 1; struct { inta, b, c; } TempStruct [2] = {{2,8} }; TempStruct.c = 67; cout << TempStruct[i].a << endl; cout << TempStruct[i].b << endl; cout << TempStruct[i].c << endl; } Compiler error #include<iostream> usingnamespace::std; voidmain(void) { inti = 1; struct { inta, b, c; } TempStruct [2] = {{2,8} }; TempStruct[i].c = 67; cout << TempStruct << endl; } 001BF048 Press any key to continue
  • 12. Structs #include<iostream> usingnamespace::std; voidmain(void) { structstudent { charName[20]; intNum; intid; }; student st1 = {"mee", 196, 7}; } Compile and run #include<iostream> usingnamespace::std; voidmain(void) { structstudent { charName[20]; intNum; intid; }; student st1 = {"mee", 196, 7}; st1 = {"zee", 111, 4}; } Compiler error
  • 13. Structs #include<iostream> usingnamespace::std; voidmain(void) { structstudent { charName[20]; intNum; intid; }; student st1 = {"mee", 196, 7}; student st2 = {"zee", 111, 4}; student Tempst1 = {"mee", 196, 7}; if(st1.Num == Tempst1.Num) { cout << "Yeah!"<< endl; } else { cout << "No!"<< endl; } } Yeah! because Structs are “value” type unlike classes which are “reference”type #include<iostream> usingnamespace::std; voidmain(void) { structstudent { charName[20]; intNum; intid; }; student st1 = {"mee", 196, 7}; student st2 = {"zee", 111, 4}; student Tempst1 = {"mee", 196, 7}; if (st1 == Tempst1) { cout << "Yeah!"<< endl; } else { cout << "No!"<< endl; } } Compiler error
  • 14. Structs #include<iostream> usingnamespace::std; voidmain(void) { structstudent { charName[20]; intNum; intid; }; student st1 = {"mee", 196, 7}; student st2 = {"zee", 111, 4}; student Tempst1 = {"mee", 196, 7}; if(st1.Num = st2.Num) { cout << "Yeah!"<< endl; } else { cout << "No!"<< endl; } cout << st1.Num << endl; cout << st2.Num << endl; } Yeah! 111 111
  • 16. Structs #include<iostream> usingnamespace::std; voidmain(void) {structInner { intNum; intid; }; structstudent { charName[20]; Inner info; }; student st1 = {"mee", { 11, 3 } }; student st2 = {"zee", 111, 4}; student &Tempst1 = st1; cout << st1. Name << endl; cout << st1.info. Num++ << endl; cout << st1.info.id << endl; cout << ++Tempst1.info. Num << endl; } mee 11 3 13 #include<iostream> usingnamespace::std; voidmain(void) {structInner { intNum; intid; }; structstudent { charName[20]; Inner info; }; student st1 = {"mee", { 11, 3 } }; student st2 = {"zee", 111, 4}; student &Tempst1 = st1; cout << st1. Name << endl; cout << st1. Num++ << endl; cout << st1.info.id << endl; cout << ++Tempst1.info.Num << endl; } Compiler error
  • 17. Structs #include<iostream> usingnamespace::std; voidmain(void) { structInner { intNum; intid; }; structstudent { charName[20]; Inner info; }; student st1 = {"mee", 11, 3 }; student st2 = {"zee", 111, 4}; student &Tempst1 = st1; cout << st1. Name << endl; cout << st2.info. Num++ << endl; cout << st2. info. id << endl; cout << ++Tempst1. info. Num << endl; } mee 111 4 12 #include<iostream> usingnamespace::std; voidmain(void) { structInner { intNum; intid; }; structstudent { string Name; Inner info; }; student st1 = {"mee", 11, 3 }; student st2 = {"zee", 111, 4}; student &Tempst1 = st1; st1.Name = "foo"; cout << st1.Name << endl; } Compiler error
  • 18. Structs #include<iostream> #include<string> usingnamespace::std; voidmain(void) { structInner { intNum; intid; }; structstudent { string Name; Inner info; }; student st1 = {"mee", 11, 3 }; student st2 = {"zee", 111, 4}; student &Tempst1 = st1; st1.Name = "foo"; cout << st1.Name << endl; } foo #include<iostream> #include<string> usingnamespace::std; voidmain(void) { structInner { intNum; intid; }; structstudent { string Name; Inner info; }; student st1 = {"mee", 11, 3 }; student st2 = {"zee", 111, 4}; student &Tempst1 = st1; st1.Name = "foo"; cout << st1 << endl; cout << st1.Name << endl; } Compiler error
  • 20. Structs #include<iostream> #include<string> usingnamespace::std; structInner{intNum;intid;}; structstudent{string Name;Inner info;}; student Print(student stu) { cout << stu.Name << endl; returnstu; } voidmain(void) { student st1 = {"mee", 11, 3 }; student st2 = {"zee", 111, 4}; student &Tempst1 = st1; Print(st1); } mee #include<iostream> #include<string> usingnamespace::std; student Print(student stu) { cout << stu.Name << endl; returnstu; } voidmain(void) { structInner {intNum;intid;}; structstudent {string Name;Inner info;}; student st1 = {"mee", 11, 3 }; student st2 = {"zee", 111, 4}; student &Tempst1 = st1; Print(st1); } Compiler error Inner student are in main and not global to let the function see them!
  • 21. Structs #include<iostream> #include<string> usingnamespace::std; structInner { intNum; intid; }; structstudent { string Name; Inner info; }; student Print(student stu) { cout << stu.Name << endl; stu.Name = "GoGo"; returnstu; } voidmain(void) {student st1 = {"mee", 11, 3 }; student st2 = {"zee", 111, 4}; student &Tempst1 = st1; Print(st1); cout << st1.Name << endl; } mee mee #include<iostream> #include<string> usingnamespace::std; structInner { intNum; intid; }; structstudent { string Name; Inner info; }; student Print(student &stu) { cout << stu.Name << endl; stu.Name = "GoGo"; returnstu; } voidmain(void) {student st1 = {"mee", 11, 3 }; student st2 = {"zee", 111, 4}; student &Tempst1 = st1; Print(st1); cout << st1.Name << endl; } mee GoGo
  • 22. Structs #include<iostream> #include<string> usingnamespace::std; structInner {intNum;intid;}; structstudent {string Name;Inner info; }; student Go() { student stu; cout << stu.Name << endl; stu.Name = "GoGo"; returnstu; } voidmain(void) {student st1 = {"mee", 11, 3 }; student st2 = {"zee", 111, 4}; st1=st2; cout << st1.Name << endl; student &Tempst1 = st1; st1=Go(); cout << st1.Name << endl; } zee GoGo
  • 24. Pointers and Structs #include<iostream> #include<string> usingnamespace::std; structInner {intNum;intid;}; structstudent {string Name;Inner info;}; voidmain(void) { student st1 = {"mee", 196, 7}; student *ptr = &st1; cout << ptr << endl; } Compile and run #include<iostream> #include<string> usingnamespace::std; structInner {intNum;intid;}; structstudent {string Name;Inner info;}; voidmain(void) { student st1 = {"mee", 196, 7}; student *ptr = &st1; cout << ptr << endl; cout << *ptr << endl; } Compiler error structInner {intNum;intid;}; structstudent {string Name;Inner info;}; voidmain(void) { student st1 = {"mee", 196, 7}; student *ptr = &st1; cout << ptr << endl; cout << ptr->Name << endl; cout << ptr->info.id << endl; } 0025F27C mee 7 structInner {intNum;intid;}; structstudent {string Name;Inner info;}; voidmain(void) { student st1 = {"mee", 196, 7}; student *ptr = &st1; cout << ptr << endl; cout << ptr->Name << endl; cout << ptr->info->id << endl; } Compiler error, -> before info
  • 25. Pointers and Structs structInner {intNum;intid;}; structstudent {string Name;Inner info;}; voidmain(void) { student st1 = {"mee", 196, 7}; student *ptr = &st1; cout << ptr << endl; cout << *(ptr->Name) << endl; cout << *(ptr->info.id) << endl; } Compiler error structInner {intNum;intid;}; structstudent {string Name;Inner info;}; voidmain(void) { student st1 = {"mee", 196, 7}; student *ptr = &st1; cout << (*ptr).Name << endl; cout << (*ptr).info.id << endl; } mee 7 structInner {intNum;intid;}; structstudent {string Name;Inner info;}; voidmain(void) { student st1 = {"mee", 196, 7}; student *ptr = &st1; cout << (*ptr)->Name << endl; cout << (*ptr)->info.id << endl; } Compiler error
  • 26. Pointers and Structs #include<iostream> #include<string> usingnamespace::std; structInner {intNum; intArr[3];}; structstudent {string Name;Inner info;}; voidCount(student stu) { inttotal = 0; for(inti = 0; i<3; i++) { total += stu.info.Arr[i]; } cout << total << endl; stu.info.Arr[0]=3; } voidmain(void) { student st1 = {"mee", 196, 1,3,3}; Count(st1); Count(st1); } 7 7 #include<iostream> #include<string> usingnamespace::std; structInner {intNum; intArr[3];}; structstudent {string Name;Inner info;}; voidCount(student *stu) { inttotal = 0; for(inti = 0; i<3; i++) { total += stu->info.Arr[i]; } cout << total << endl; stu->info.Arr[0]=3; } voidmain(void) { student st = {"mee", 196, 1,3,3}; student *st1 = &st; Count(st1); Count(st1); } 7 9
  • 27. Pointers and Structs #include<iostream> #include<string> usingnamespace::std; structInner {intNum; intArr[3];}; structstudent {string Name;Inner info;}; voidCount(student *&stu) { inttotal = 0; for(inti = 0; i<3; i++) { total += stu->info.Arr[i]; } cout << total << endl; stu->info.Arr[0]=3; } voidmain(void) { student st = {"mee", 196, 1,3,3}; student *st1 = &st; Count(st1); Count(st1); } 7 9 #include<iostream> #include<string> usingnamespace::std; structInner {intNum; intArr[3];}; structstudent {string Name;Inner info;}; voidCount(student &stu) { inttotal = 0; for(inti = 0; i<3; i++) { total += stu.info.Arr[i]; } cout << total << endl; stu.info.Arr[0]=3; } voidmain(void) { student st1 = {"mee", 196, 1,3,3}; Count(st1); Count(st1); } 7 9
  • 29. StructsMembers and Functions #include<iostream> #include<string> usingnamespace::std; structstudent { string Name; voidprint() { cout << Name << endl; } }; voidmain(void) { student st1 = {"mee"}; st1.print(); } mee #include<iostream> #include<string> usingnamespace::std; structstudent { string Name; voidprint() { cout << student.Name << endl; } }; voidmain(void) { student st1 = {"mee"}; st1.print(); } Compiler error Function print is implicitely inline
  • 30. StructsMembers and Functions #include<iostream> #include<string> usingnamespace::std; structstudent { string Name; voidprint() { cout << Name << endl; } voidChangeName(string str) { Name = str; } }; voidmain(void) { string NewName= "zee"; student st1 = {"mee"}; st1.print(); st1.ChangeName(NewName); st1.print(); } mee zee
  • 31. StructsMembers and Functions #include<iostream> #include<string> usingnamespace::std; structstudent { string Name; voidstudent::print(); voidChangeName(string str); }; voidstudent::print() { cout << Name << endl; } voidstudent::ChangeName(string str) { Name = str+Name; } voidmain(void) { string NewName= "zee"; student st1 = {"mee"}; st1.print(); st1.ChangeName(“foo”); st1.print(); } mee foomee Now the functions are not implicitely inline #include<iostream> #include<string> usingnamespace::std; structstudent { string Name; voidstudent::print(); voidChangeName(string str); }; voidstudent::print() { cout << Name << endl; } voidstudent::ChangeName(string str) { Name = str; } voidmain(void) { string NewName= "zee"; student st1 = {"mee"}; st1.print(); st1.ChangeName(“foo”); st1.print(); } mee foo Now the functions are not implicitely inline
  • 32. StructsMembers and Functions •privatedata members –Private access •Can be used by a few categories of functions •publicdata members –Public access •Can be used by all code
  • 33. StructsMembers and Functions #include<iostream> #include<string> usingnamespace::std; structstudent { private: intTempID; public: intID; string Name; voidChangeID(intn); voidprint(); voidChangeName(string str); }; voidstudent::print() {cout << Name << endl; } voidstudent::ChangeName(string str) {Name = str+Name;} voidstudent::ChangeID(intn) {TempID = 2; ID = n+TempID; cout << "Your ID = "<< ID << endl;} voidmain(void) {intn = 3; string NewName= "zee"; student st1;st1.print(); st1.ChangeName(NewName);st1.print(); TempID=5; st1.ChangeID(n); } Compiler error Un delcared identifier #include<iostream> #include<string> usingnamespace::std; structstudent { private: intTempID; public: intID; string Name; voidChangeID(intn); voidprint(); voidChangeName(string str); }; voidstudent::print() {cout << Name << endl; } voidstudent::ChangeName(string str) {Name = str+Name;} voidstudent::ChangeID(intn) {TempID = 2; ID = n+TempID; cout << "Your ID = "<< ID << endl;} voidmain(void) {intn = 3; string NewName= "zee"; student st1;st1.print(); st1.ChangeName(NewName);st1.print(); st1.ChangeID(n); } zee Your ID = 5
  • 34. StructsMembers and Functions #include<iostream> #include<string> usingnamespace::std; structstudent { private: intTempID; public: intID; string Name; voidChangeID(intn); voidprint(); voidChangeName(string str); }; voidstudent::print() {cout << Name << endl; } voidstudent::ChangeName(string str) {Name = str+Name;} voidstudent::ChangeID(intn) {TempID = 2; ID = n+TempID; cout << "Your ID = "<< ID << endl;} voidmain(void) {intn = 3; string NewName= "zee"; student st1;st1.print(); st1.ChangeName(NewName);st1.print(); st1.TempID=5; st1.ChangeID(n);} Compiler error Can’t access private data memebers
  • 35. Quiz
  • 36. Quiz 1, 2, 3, 4 #include <iostream> using namespace::std; void main(void) { inti= 1; structMyFirstS { inta, b, c; } TempStruct[2] = {{2,4} } cout<< TempStruct[i].a << endl; cout<< TempStruct[i].b << endl; cout<< TempStruct[i].c << endl; } Compiler error Missing; #include <iostream> using namespace::std; void main(void) { inti= 0; struct { inta, b, c; }; TempStruct[2] = {2,8,10}; cout<< TempStruct[i].a << endl; cout<< TempStruct[i].b << endl; cout<< TempStruct[i].c << endl; } Compiler error, TempStructhas no type! #include <iostream> using namespace::std; void main(void) { string NewName= “wa3wa3" structstudent { char Name[20]; intNum; intid; }; student st1 = {"mee", 196, 7}; st1 = {NewName, 111, 4}; } Compiler error structInner {intNum;intid;}; structstudent {string Name;Inner info;}; voidmain(void) { student st1 = {"mee", 196, 7}; student *ptr = &st1; cout << ptr << endl; cout << ptr->Name << endl; cout << ptr->info->id << endl; } Compiler error, -> before info
  • 37. Quiz 5, 6 #include <iostream> #include <string> using namespace::std; structInner {intNum; intArr[3];}; structstudent {string Name;Inner info;}; void Count(student &stu) { inttotal = 0; for (int i = 0; i<3; i++) { total += 2*stu.info.Arr[i]; } cout<< total << endl; stu.info.Arr[1]=-3; } void main(void) { student st1 = {"mee", 196, 1,3,3}; Count(st1); Count(st1); } 14 2 #include <iostream> #include <string> using namespace::std; structstudent {public: intID; string Name; void ChangeID(intn); void print(); void ChangeName(string str); }; void student::print() {cout<< Name << endl; } void student::ChangeName(string str) {Name = str+Name;} void student::ChangeID(intn) { static intTempID= 4; TempID+= 2; ID = n+TempID; cout<< "Your ID = " << ID << endl;} void main(void) {int n = 3; string NewName= "zee"; student st1;st1.print(); st1.ChangeName(NewName);st1.print(); st1.ChangeID(n); n = 5; st1.ChangeName(NewName);st1.print(); st1.ChangeID(n);} zee Your ID = 9 zeezee Your ID = 13