SlideShare une entreprise Scribd logo
1  sur  37
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...ssuserd6b1fd
 
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) 4 of 5 by...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) 4 of 5 by...ssuserd6b1fd
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...ssuserd6b1fd
 
Modern C++ Concurrency API
Modern C++ Concurrency APIModern C++ Concurrency API
Modern C++ Concurrency APISeok-joon Yun
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...ssuserd6b1fd
 
C++ Lambda and concurrency
C++ Lambda and concurrencyC++ Lambda and concurrency
C++ Lambda and concurrency명신 김
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTsKevlin Henney
 
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...ssuserd6b1fd
 
C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th StudyChris Ohk
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...ssuserd6b1fd
 
C tech questions
C tech questionsC tech questions
C tech questionsvijay00791
 
What We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit TestingWhat We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit TestingKevlin Henney
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101premrings
 

Tendances (20)

C++ L09-Classes Part2
C++ L09-Classes Part2C++ L09-Classes Part2
C++ L09-Classes Part2
 
c programming
c programmingc programming
c programming
 
c programming
c programmingc programming
c programming
 
Unit 3
Unit 3 Unit 3
Unit 3
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
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) 4 of 5 by...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) 4 of 5 by...
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 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
Modern C++ Concurrency APIModern C++ Concurrency API
Modern C++ Concurrency API
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
 
C++ L10-Inheritance
C++ L10-InheritanceC++ L10-Inheritance
C++ L10-Inheritance
 
C++ Lambda and concurrency
C++ Lambda and concurrencyC++ Lambda and concurrency
C++ Lambda and concurrency
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTs
 
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
 
C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th Study
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
 
C tech questions
C tech questionsC tech questions
C tech questions
 
Functional C++
Functional C++Functional C++
Functional C++
 
What We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit TestingWhat We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit Testing
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
 

En vedette

YEG-Agile-planning
YEG-Agile-planningYEG-Agile-planning
YEG-Agile-planningAmir Barylko
 
Agile requirements
Agile requirementsAgile requirements
Agile requirementsAmir Barylko
 
PRDCW-advanced-design-patterns
PRDCW-advanced-design-patternsPRDCW-advanced-design-patterns
PRDCW-advanced-design-patternsAmir Barylko
 
sdec11-Advanced-design-patterns
sdec11-Advanced-design-patternssdec11-Advanced-design-patterns
sdec11-Advanced-design-patternsAmir Barylko
 

En vedette (7)

agile-planning
agile-planningagile-planning
agile-planning
 
YEG-Agile-planning
YEG-Agile-planningYEG-Agile-planning
YEG-Agile-planning
 
Refactoring
RefactoringRefactoring
Refactoring
 
Agile requirements
Agile requirementsAgile requirements
Agile requirements
 
PRDCW-advanced-design-patterns
PRDCW-advanced-design-patternsPRDCW-advanced-design-patterns
PRDCW-advanced-design-patterns
 
Agile planning
Agile planningAgile planning
Agile planning
 
sdec11-Advanced-design-patterns
sdec11-Advanced-design-patternssdec11-Advanced-design-patterns
sdec11-Advanced-design-patterns
 

Similaire à C++ L07-Struct

Students Management System c++ project.pptx
Students Management System c++ project.pptxStudents Management System c++ project.pptx
Students Management System c++ project.pptxqaswarsarfraz
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfyamew16788
 
Tugas praktikukm pemrograman c++
Tugas praktikukm  pemrograman c++Tugas praktikukm  pemrograman c++
Tugas praktikukm pemrograman c++Dendi Riadi
 
Programa.eje
Programa.ejePrograma.eje
Programa.ejeguapi387
 
c++ #include -iostream- using namespace std- void InsertionSort(int nu.pdf
c++ #include -iostream- using namespace std- void InsertionSort(int nu.pdfc++ #include -iostream- using namespace std- void InsertionSort(int nu.pdf
c++ #include -iostream- using namespace std- void InsertionSort(int nu.pdfkuldeepkumarapgsi
 
Complete DB code following the instructions Implement the D.pdf
Complete DB code following the instructions Implement the D.pdfComplete DB code following the instructions Implement the D.pdf
Complete DB code following the instructions Implement the D.pdfaccess2future1
 
Add an interactive command line to your C++ application
Add an interactive command line to your C++ applicationAdd an interactive command line to your C++ application
Add an interactive command line to your C++ applicationDaniele Pallastrelli
 
ParallelProgrammingBasics_v2.pdf
ParallelProgrammingBasics_v2.pdfParallelProgrammingBasics_v2.pdf
ParallelProgrammingBasics_v2.pdfChen-Hung Hu
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxDeepasCSE
 
Introduction to cpp (c++)
Introduction to cpp (c++)Introduction to cpp (c++)
Introduction to cpp (c++)Arun Umrao
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloadingkinan keshkeh
 
Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++Yandex
 
ch5_additional.ppt
ch5_additional.pptch5_additional.ppt
ch5_additional.pptLokeshK66
 

Similaire à C++ L07-Struct (20)

C++ practical
C++ practicalC++ practical
C++ practical
 
CP 04.pptx
CP 04.pptxCP 04.pptx
CP 04.pptx
 
Students Management System c++ project.pptx
Students Management System c++ project.pptxStudents Management System c++ project.pptx
Students Management System c++ project.pptx
 
Codes on structures
Codes on structuresCodes on structures
Codes on structures
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
 
Tugas praktikukm pemrograman c++
Tugas praktikukm  pemrograman c++Tugas praktikukm  pemrograman c++
Tugas praktikukm pemrograman c++
 
Programa.eje
Programa.ejePrograma.eje
Programa.eje
 
c++ #include -iostream- using namespace std- void InsertionSort(int nu.pdf
c++ #include -iostream- using namespace std- void InsertionSort(int nu.pdfc++ #include -iostream- using namespace std- void InsertionSort(int nu.pdf
c++ #include -iostream- using namespace std- void InsertionSort(int nu.pdf
 
Computer Programming- Lecture 7
Computer Programming- Lecture 7Computer Programming- Lecture 7
Computer Programming- Lecture 7
 
Complete DB code following the instructions Implement the D.pdf
Complete DB code following the instructions Implement the D.pdfComplete DB code following the instructions Implement the D.pdf
Complete DB code following the instructions Implement the D.pdf
 
Add an interactive command line to your C++ application
Add an interactive command line to your C++ applicationAdd an interactive command line to your C++ application
Add an interactive command line to your C++ application
 
ParallelProgrammingBasics_v2.pdf
ParallelProgrammingBasics_v2.pdfParallelProgrammingBasics_v2.pdf
ParallelProgrammingBasics_v2.pdf
 
Lab 13
Lab 13Lab 13
Lab 13
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
 
Introduction to cpp (c++)
Introduction to cpp (c++)Introduction to cpp (c++)
Introduction to cpp (c++)
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
 
CSNB244 Lab5
CSNB244 Lab5CSNB244 Lab5
CSNB244 Lab5
 
Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++
 
ch5_additional.ppt
ch5_additional.pptch5_additional.ppt
ch5_additional.ppt
 

Plus de Mohammad Shaker

12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian GraduateMohammad Shaker
 
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]Mohammad Shaker
 
Interaction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with PsychologyInteraction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with PsychologyMohammad Shaker
 
Short, Matters, Love - Passioneers Event 2015
Short, Matters, Love -  Passioneers Event 2015Short, Matters, Love -  Passioneers Event 2015
Short, Matters, Love - Passioneers Event 2015Mohammad Shaker
 
Unity L01 - Game Development
Unity L01 - Game DevelopmentUnity L01 - Game Development
Unity L01 - Game DevelopmentMohammad Shaker
 
Android L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesAndroid L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesMohammad Shaker
 
Interaction Design L03 - Color
Interaction Design L03 - ColorInteraction Design L03 - Color
Interaction Design L03 - ColorMohammad Shaker
 
Interaction Design L05 - Typography
Interaction Design L05 - TypographyInteraction Design L05 - Typography
Interaction Design L05 - TypographyMohammad Shaker
 
Interaction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingInteraction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingMohammad Shaker
 
Android L04 - Notifications and Threading
Android L04 - Notifications and ThreadingAndroid L04 - Notifications and Threading
Android L04 - Notifications and ThreadingMohammad Shaker
 
Android L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSAndroid L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSMohammad Shaker
 
Interaction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsInteraction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsMohammad Shaker
 
Interaction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsInteraction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsMohammad Shaker
 
Android L10 - Stores and Gaming
Android L10 - Stores and GamingAndroid L10 - Stores and Gaming
Android L10 - Stores and GamingMohammad Shaker
 
Android L06 - Cloud / Parse
Android L06 - Cloud / ParseAndroid L06 - Cloud / Parse
Android L06 - Cloud / ParseMohammad Shaker
 
Android L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesAndroid L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesMohammad Shaker
 
Android L03 - Styles and Themes
Android L03 - Styles and Themes Android L03 - Styles and Themes
Android L03 - Styles and Themes Mohammad Shaker
 
Android L02 - Activities and Adapters
Android L02 - Activities and AdaptersAndroid L02 - Activities and Adapters
Android L02 - Activities and AdaptersMohammad Shaker
 

Plus de Mohammad Shaker (20)

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

Dernier

VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...ranjana rawat
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 

Dernier (20)

VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 

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