Documente Academic
Documente Profesional
Documente Cultură
Fall 2018
Dynamic Memory Allocation
Type Casting
Lecture 3
M. Tahir Awan
(mtahir@cust.edu.pk)
Capital University of Science & Technology (CUST),
Islamabad
Difference between C and C++ / 1
• Below are the differences :
C C++
C is a procedural programming C++ is an object oriented
language that is based upon functions programming language that is based
upon classes and objects
In C, there is no support for function In C++ has support for function and
overloading or operator overloading operator overloading
C does not support Inheritance, C++ supports Inheritance,
Polymorphism, Virtual Functions Polymorphism and Virtual Functions
C has not so strong data abstraction/ C++ has strong data abstraction and
data security features data security features
In C variables need to be defined at In C++ Variables can be defined
start of the program anywhere in the program
y yptr y
5 500000 600000 600000 5
yPtr
Address of y is
value of yptr
int num = 5;
int *x;
int **y;
x = #
y = &x;
cout<<*x; // Print on Screen ?
cout<<**y; // Print on Screen ?
9/27/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 13
2D Arrays : Pointer to Pointers
• 2D array elements are accessed like pointer to
pointers
suit[1] ’D’ ’i’ ’a’ ’m’ ’o’ ’n’ ’d’ ’s’ ’\0’
– suit array has a fixed size, but strings can be of any size
9/27/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 16
Void Pointer (Void *)
• Void pointer is a generic pointer, that can point to
objects of any data type.
• A void pointer is declared using the void keyword
and can store address of any data type
int x = 50;
float y = 5.7;
char name[30] = “Data Structures”;
void * ptr ;
ptr = &x;
ptr = &y;
ptr = name;
• To access data using void pointer, typecast it first
9/27/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 17
Const pointer
• A const pointer is a pointer whose value can not
be changed after initialization i.e. a const pointer
will always point to the same address
int value = 50;
int *const ptr = &value;
• y = (short) x; OR y = short ( x );
• z = float (x) / 33;
• z = static_cast <float> (x) / 33;
• z = reinterpret_cast <float> (x) / 33;
sum = 17;count = 5;
da = 3.3 ; db = 4.5 ; dc = 9.9;
ch = 'a';
void main() { }
int a=10;
float b=3.95;
char c = 'A';
void* ptr;
ptr = &a;
cout << *((int*) ptr) << endl;
ptr = &b;
cout << *((float*) ptr) << endl;
H e l l o \0
0 1 2 3 4 5 6 7 8 9
• Examples
struct complexNumber{
struct student{ float real;
char Name[20]; float complex;
float CGPA; };
};
struct book{
char title[30];
int price;
};
9/27/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 42
Declaration of Structure type Variables
• Structure variables are declared like other
variables
struct student{
struct student{
char Name[20]; char Name[20];
float CGPA; float CGPA;
}; }s1, s2;
struct student s1, s2; Alternate Declaration Method
struct student *ptrs;
struct Rectangle
{
float width; r1
float height;
0 width
float area;
0 height
};
r area
r1 = {4, 5, 0};
9/27/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 44
Structures : Example
• Structure definition and usage
#include <iostream>
using namespace std; if(s1.marks >= 50)
s1.grade = 'P';
struct student { else
char name[20]; s1.grade = 'F';
int marks;
char grade; cout<<"Student Name = “<<
}; s1.name<<endl;
cout<<"Student Marks = “<<
s1.marks<<endl;
void main()
cout<<"Student Grade = “<<
{ s1.grade<<endl;
struct student s1; }
struct distance{
distance d1; float feet;
distance *ptr = &d1 float inches;
};
ptr-> feet = 10;
ptr-> inches = 5;
(*ptr).inches = 5;
0-3 integer
struct calender {
int day;
4 - 12
char month[9]; 9 character
int year;
};
13 - 15 (hole)
……….......
0 1 2 3 19
9/27/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 49
Arrays of Structures : Example
• An array of structure is a type of array in which each
element contains a complete structure.
struct Book {
int ID;
int Pages;
float Price;
};
Book brec[100]; // declaration of array of structures
brec[100]
brec[0] brec[1] brec[99]
ID Pages Price ID Pages Price … ID Pages Price
Array of Structures : Example
• Array of Structures
#include <iostream> for(i=0;i<10;i++){
#include <string>
if(s1[i].marks >= 50)
using namespace std;
s1[i].grade = 'P';
else
struct student {
s1[i].grade = 'F';
string name;
}
int marks;
for(i=0;i<10;i++){
char grade;
cout<<"Student Name = “<<
}; s1[i].name<<endl;
void main() { cout<<"Student Marks = “<<
struct student *s1= new student s1[i].marks<<endl;
[10]; cout<<"Student Grade = “<<
int i,j; s1[i].grade<<endl;
for(i=0;i<10;i++){ }
cout<<" Student Name = "; delete [] s1;
cin>>s1[i].name; }
cout<<" Student Marks = ";
cin>>s1[i].marks;
}
9/27/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST 51
Using Structures With Functions
• Passing structures to functions
• Pass structures by Value
– Pass entire structure as function argument
– Changes made to struct members will not be
visible outside function
• Pass structures by Reference
– Pass a pointer to structure i.e. its address
– Changes made to structure members will
modify original structure
• Return Value as structure
– Function can return a structure
52
9/27/2018 CSEE2123: OOP and DS © M. Tahir Awan, CUST
Structure Call by Reference : Example
• Structure and Pointers
#include <iostream> findGrade (ptrS);
using namespace std;
cout<<"Student Name = “<<
struct student { s1.name<<endl;
char name[20]; cout<<"Student Marks = “<<
s1.marks<<endl;
int marks;
cout<<"Student Grade = “<<
char grade; s1.grade<<endl;
}; }
void findGrade ( struct student
*ptrS);
void findGrade ( struct student
void main() { *ptrS) {
struct student s1, *ptrS; if(ptrS->marks >= 50)
ptrS = &s1; ptrS->grade = 'P';
cout<<" Student Name = "; else
cin>>s1.name; ptrS->grade = 'F„;
cout<<" Student Marks = "; }
cin>>s1.marks;
}
};