SlideShare a Scribd company logo
programming fundamentals
The new Operator
• float* p;
• only allocate memory for a pointer
• *p=3.1459
• One way to avoid this is assign
memory using the key word new
The new Operator
• int main(){
• float*p;
• p=new float;//create unnamed
memory
• *p=3.14159;
• cout<<p<<" "<<*p;
• getch();
• }
The delete Operator
• The delete operator reverse the
operation of new operator
• It de-allocate the memory reserved
by the new operator
• float*p=new float;
• *p=3.1459;
• delete p;
• *p=3.44; //Wrong
Pointer to pointer(**)
• int main(){
• int n=44;
• int* pn=&n;
• int** ppn=&pn;
• cout<<"*pn="<<*pn<<"
ppn="<<**ppn;
• getch();
• }
44
pn
ppn
Character Pointers
• int main(){
• char *p="This is a text";
• // Exactly Same As
• char a[100]="This is a text";
• cout<<p;
• // Same As
• cout<<endl<<a;
• getch();
• return 0;
• }
String Manipulation
Functions
• char * strcpy (char *s1 , const char *s2 ) ;
Copies the string s2 into the character array s1.The
value of s1 returned.
• char * strncpy ( char *s1 , char *s2 , int n ) ;
copies atmost n characters
• char * strcat (char *s1 , char *s2 ) ;
Appends the string s2 to the string s1.the first
character of s2 overwrite the null character of s1.the
value of s1 is returned.
• char * strncat ( char *s1 , char *s2 , int n ) ;
Comparison Functions
• int strcmp (const char *s1 , const char *s2 )
Compares the string s1 with s2.The function return a
value zero,less than zero or greater than zero.
• int strncmp ( const char *s1 , const char *s2 ,
int n ) ;
• int strlen ( const char *s ) ;
• #include<iostream>
• #include<string>
• void main(){
• char x[100]={"Happy Birthday"};
• char y[100],z[100];
• strncpy(y,x,5);
• strcpy(z,y);
• strcat(x," to you");
• cout<<"nThe x string:"<<x;
•
• if(strcmp(x,y)==0)
• cout<<"n string x and y are equal";
• else
• cout<<"n string x and y are not equal";
•
• if(strcmp(z,y)==0)
• cout<<"n string z and y are equal";
• else
• cout<<"n string z and y are not equal";
• getch();
• }
programming fundamentals
• void main(){
• char *p;
• char a[100],b[15];
• cout<<"Plz Enter a string:";
• gets(a);
• cout<<"What u want find";
• gets(b);
• p=strtok(a," ");
• while(p!=NULL){
• if(strcmp(b,p)==0)
• cout<<"n The string" "<<p<< ""is
found:";
• p=strtok(NULL," ");
• }
• getch();
programming fundamentals
Structures
Structures
• User Defined Data type
• Used to define objects having same
properties
Structure definition
• struct Student {
• char name [ 60 ] ;
• char address [ 100 ] ;
• double gpa ;
• } ;
Keyword
Structure
name
• Student s1 , s2 , s3 ;
• Student ali, waqas;
Structure Name
Variable Name
Variables of Structure
Structure
• Structure can have pointer data
type as member
• Structure can have other structure
as member data type
• Structure can not have same
structure as data type
Structure
• struct address {
• char streetAddress [ 100 ] ;
• char *city ;
• char country [ 30 ] ;
• }
Structure
• struct Student {
• char name [ 60 ] ;
• address add ;
• double gpa ;
• } s1 , s2 , s3 ;
Structure Variables
Variables of type structure
Pointers to Structure
Array of Structure
Structures
• Student a,b,c;
• Student s [ 100 ] ;
• Student *sPtr ;
Example 2
• struct Student {
• char name [ 64 ] ;
• char course [ 128 ] ;
• int age ;
• int year ;
• } ;
Initializing Structures
Student s1 = { “Amir”,“cs201”,19, 2002 } ;
Name Course age Year
Initializing Structures
• s1.name ;
• s1.course ;
• s1.age ;
• s1.year ;
Initializing Structures
• s1.age = 20 ;
• s1.name = “Abbas Ali ” ; //Wrong
• strcpy ( s1.name , “Abbas Ali ” ) ;
• s1.year=2000;
Accessing structure
members
• cout << s1.name ;
• cout << s1.course ;
• cout << s1.age ;
• cout << s1.year ;
• #include<string>
• struct Student{
• char name [ 64 ];
• char course [ 128 ];
• int age;
• int year;
• };
• void main(){
• Student
s1={"Amir","Programming",19,2002},
s2;
• strcpy( s2.name,"Abbas Ali");
• s2.age=23;
• cin>>s2.year;
• cout<<s1.name<<" "<<s2.name<<endl;
• cout<<s1.course<<" "<<s2.course<<endl;
• cout<<s1.age<<" "<<s2.age<<endl;
• cout<<s1.year<<" "<<s2.year<<endl;
• getch();
• }
Structure
• Simple Variable of type
• Structure Pointer to Structure
• Arrays of Structures
• Function that returns a Structure
• Pass the Structure to functions
Structure (Arithmetic
Operation)
• Student stdnt1 , stdnt2 ;
• stdnt1 + stdnt2 ; //Wrong
Structure
• Student s1 , s2 ;
• s1 = s2 ;
#include<iostream>
#include<iomanip>
#include<cstring>
using namespace std;
struct Student{
char name [ 64 ];
char course [ 128 ];
int age;
int year;
};
int main(){
Student
s1={"Amir","Programming",19,2016},s2;
s2=s1;
cout<<s2.name<<endl<<s2.course<<endl;
cout<<s2.age<<endl<<s2.year<<endl;
}
Pointers to Structure
• Student *sPtr , s1 ;
• sPtr = &s1 ;
• *sPtr.name ; Wrong
• sPtr ->name ;
• Same as
• s1.name
#include<iostream>
#include<iomanip>
#include<cstring>
using namespace std;
struct Student{
char name [ 64 ];
char course [ 128 ];
int age;
int year;
};
int main(){
Student
s1={"Amir","Programming",19,2016},*s2;
s2=&s1;
cout<<s2->name<<endl;
cout<<s2->course<<endl;
cout<<s2->age<<endl;
cout<<s2->year<<endl;
using namespace std;
struct Student{
char name [ 64 ];
char course [ 128 ];
int age;
int year;
};
int main(){
Student *s1=new Student;
strcpy(s1->name,"Amir");
strcpy(s1->course,"Programming");
s1->age=19;
s1->year=2016;
cout<<s1->name<<endl;
cout<<s1->course<<endl;
cout<<s1->age<<endl;
cout<<s1->year<<endl;
}
Passing Structures to
Functions
Call by value
Call by Reference  X
• struct Student{
• char name[20];
• int rollno;
• };
• void init(Student&);
• void main(){
• Student s1;
• init(s1);
• cout<<s1.name<<" "<<s1.rollno;
• getch();
• }
• void init(Student &a){
• cout<<"Plz enter the name n";
• gets(a.name);
• cout<<"Plz enter the Roll No n";
• cin>>a.rollno;
• }
Arrays of Structure
• Student s [ 100 ] ;
• s [ 0 ].name ;
• s [ 1 ].name ;
• s [ 2 ].name ;
• . . .
• s [ 99 ].name ;
Example 3
• struct Student {
• char firstName [ 30 ] ;
• char lastName [ 30 ] ;
• char course [ 15 ] ;
• char rollNo [ 10 ] ;
• int age ;
• float gpa ;
• } ;

More Related Content

DOCX
Theme verdadeiro
PPTX
C++ examples &revisions
ODP
Ruby Basics by Rafiq
PPTX
Paperjs presentation
PDF
Myraytracer
PPTX
Bank management system project in c++ with graphics
DOCX
Superficies en maple
PDF
SPL 13 | Character Array(String) in C
Theme verdadeiro
C++ examples &revisions
Ruby Basics by Rafiq
Paperjs presentation
Myraytracer
Bank management system project in c++ with graphics
Superficies en maple
SPL 13 | Character Array(String) in C

What's hot (7)

PDF
An Introduction to Tinkerpop
PPT
C# Strings
PDF
HTML5 and CSS3 Refresher
PDF
[WELC] 21. I’m Changing the Same Code All Over the Place
PDF
Coscup2021-rust-toturial
PDF
Coscup2021 - useful abstractions at rust and it's practical usage
PPS
CS101- Introduction to Computing- Lecture 35
An Introduction to Tinkerpop
C# Strings
HTML5 and CSS3 Refresher
[WELC] 21. I’m Changing the Same Code All Over the Place
Coscup2021-rust-toturial
Coscup2021 - useful abstractions at rust and it's practical usage
CS101- Introduction to Computing- Lecture 35
Ad

Similar to programming fundamentals (20)

PDF
Chapter 4 - Structures - Student.pdf - slides
PPTX
Pf cs102 programming-10 [structs]
PDF
C programming day#3.
PPT
Lecture 7
PPT
Unit 1 introduction to data structure
PDF
0-C Reviewhgfhgfghfghfgdggfghfghfhgfhfgfhh.pdf
DOCX
COMPLEX AND USER DEFINED TYPESPlease note that the material on t.docx
PPT
Introduction to structures in c lang.ppt
PPT
structures.ppt
PPTX
PDF
C++ L07-Struct
PPTX
Cs1123 12 structures
PDF
Revision notes for exam 2011 computer science with C++
PPTX
Coding - L30-L31-Array of structures.pptx
PDF
Structures and pointers-2_240731_192519.pdf
PDF
Data Structure & Algorithm - Self Referential
PPT
Lecture number three Structures (1).ppt
PDF
C++ Course - Lesson 3
PPTX
Data Structure & aaplications_Module-1.pptx
PPTX
ECE2102-Week13 - 14-Strhhhhhhhjjjucts.pptx
Chapter 4 - Structures - Student.pdf - slides
Pf cs102 programming-10 [structs]
C programming day#3.
Lecture 7
Unit 1 introduction to data structure
0-C Reviewhgfhgfghfghfgdggfghfghfhgfhfgfhh.pdf
COMPLEX AND USER DEFINED TYPESPlease note that the material on t.docx
Introduction to structures in c lang.ppt
structures.ppt
C++ L07-Struct
Cs1123 12 structures
Revision notes for exam 2011 computer science with C++
Coding - L30-L31-Array of structures.pptx
Structures and pointers-2_240731_192519.pdf
Data Structure & Algorithm - Self Referential
Lecture number three Structures (1).ppt
C++ Course - Lesson 3
Data Structure & aaplications_Module-1.pptx
ECE2102-Week13 - 14-Strhhhhhhhjjjucts.pptx
Ad

More from Farhan ullah baig (20)

PPTX
Types of polymerization
PPTX
Nanomaterials
PPTX
Conducting polymers
PPTX
Gender based violence
PDF
PPTX
Evaluation of operation management in Textile Industry
PPTX
Medical mattress
PPTX
Waste in spinning
PPTX
Ceramic fibers
DOCX
Extraction of sugarcane fibers
PDF
PDF
Acetate rayon
PPTX
Kenaf fiber(Bast fiber)
PPT
Freud psycho-analytical theory
PPTX
PPTX
PDF
mechnaics of non-wovens
PPTX
Draw frame
PPTX
Industrial psychology
Types of polymerization
Nanomaterials
Conducting polymers
Gender based violence
Evaluation of operation management in Textile Industry
Medical mattress
Waste in spinning
Ceramic fibers
Extraction of sugarcane fibers
Acetate rayon
Kenaf fiber(Bast fiber)
Freud psycho-analytical theory
mechnaics of non-wovens
Draw frame
Industrial psychology

Recently uploaded (20)

PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
Welding lecture in detail for understanding
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
Geodesy 1.pptx...............................................
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Sustainable Sites - Green Building Construction
PDF
Digital Logic Computer Design lecture notes
PPT
Mechanical Engineering MATERIALS Selection
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
PPT on Performance Review to get promotions
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
bas. eng. economics group 4 presentation 1.pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Welding lecture in detail for understanding
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Arduino robotics embedded978-1-4302-3184-4.pdf
Geodesy 1.pptx...............................................
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
CH1 Production IntroductoryConcepts.pptx
Internet of Things (IOT) - A guide to understanding
Embodied AI: Ushering in the Next Era of Intelligent Systems
Sustainable Sites - Green Building Construction
Digital Logic Computer Design lecture notes
Mechanical Engineering MATERIALS Selection
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPT on Performance Review to get promotions
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026

programming fundamentals

  • 2. The new Operator • float* p; • only allocate memory for a pointer • *p=3.1459 • One way to avoid this is assign memory using the key word new
  • 3. The new Operator • int main(){ • float*p; • p=new float;//create unnamed memory • *p=3.14159; • cout<<p<<" "<<*p; • getch(); • }
  • 4. The delete Operator • The delete operator reverse the operation of new operator • It de-allocate the memory reserved by the new operator • float*p=new float; • *p=3.1459; • delete p; • *p=3.44; //Wrong
  • 5. Pointer to pointer(**) • int main(){ • int n=44; • int* pn=&n; • int** ppn=&pn; • cout<<"*pn="<<*pn<<" ppn="<<**ppn; • getch(); • } 44 pn ppn
  • 6. Character Pointers • int main(){ • char *p="This is a text"; • // Exactly Same As • char a[100]="This is a text"; • cout<<p; • // Same As • cout<<endl<<a; • getch(); • return 0; • }
  • 7. String Manipulation Functions • char * strcpy (char *s1 , const char *s2 ) ; Copies the string s2 into the character array s1.The value of s1 returned. • char * strncpy ( char *s1 , char *s2 , int n ) ; copies atmost n characters • char * strcat (char *s1 , char *s2 ) ; Appends the string s2 to the string s1.the first character of s2 overwrite the null character of s1.the value of s1 is returned. • char * strncat ( char *s1 , char *s2 , int n ) ;
  • 8. Comparison Functions • int strcmp (const char *s1 , const char *s2 ) Compares the string s1 with s2.The function return a value zero,less than zero or greater than zero. • int strncmp ( const char *s1 , const char *s2 , int n ) ; • int strlen ( const char *s ) ;
  • 9. • #include<iostream> • #include<string> • void main(){ • char x[100]={"Happy Birthday"}; • char y[100],z[100]; • strncpy(y,x,5); • strcpy(z,y); • strcat(x," to you"); • cout<<"nThe x string:"<<x; •
  • 10. • if(strcmp(x,y)==0) • cout<<"n string x and y are equal"; • else • cout<<"n string x and y are not equal"; • • if(strcmp(z,y)==0) • cout<<"n string z and y are equal"; • else • cout<<"n string z and y are not equal"; • getch(); • }
  • 12. • void main(){ • char *p; • char a[100],b[15]; • cout<<"Plz Enter a string:"; • gets(a); • cout<<"What u want find"; • gets(b); • p=strtok(a," "); • while(p!=NULL){ • if(strcmp(b,p)==0) • cout<<"n The string" "<<p<< ""is found:"; • p=strtok(NULL," "); • } • getch();
  • 15. Structures • User Defined Data type • Used to define objects having same properties
  • 16. Structure definition • struct Student { • char name [ 60 ] ; • char address [ 100 ] ; • double gpa ; • } ; Keyword Structure name
  • 17. • Student s1 , s2 , s3 ; • Student ali, waqas; Structure Name Variable Name Variables of Structure
  • 18. Structure • Structure can have pointer data type as member • Structure can have other structure as member data type • Structure can not have same structure as data type
  • 19. Structure • struct address { • char streetAddress [ 100 ] ; • char *city ; • char country [ 30 ] ; • }
  • 20. Structure • struct Student { • char name [ 60 ] ; • address add ; • double gpa ; • } s1 , s2 , s3 ;
  • 21. Structure Variables Variables of type structure Pointers to Structure Array of Structure
  • 22. Structures • Student a,b,c; • Student s [ 100 ] ; • Student *sPtr ;
  • 23. Example 2 • struct Student { • char name [ 64 ] ; • char course [ 128 ] ; • int age ; • int year ; • } ;
  • 24. Initializing Structures Student s1 = { “Amir”,“cs201”,19, 2002 } ; Name Course age Year
  • 25. Initializing Structures • s1.name ; • s1.course ; • s1.age ; • s1.year ;
  • 26. Initializing Structures • s1.age = 20 ; • s1.name = “Abbas Ali ” ; //Wrong • strcpy ( s1.name , “Abbas Ali ” ) ; • s1.year=2000;
  • 27. Accessing structure members • cout << s1.name ; • cout << s1.course ; • cout << s1.age ; • cout << s1.year ;
  • 28. • #include<string> • struct Student{ • char name [ 64 ]; • char course [ 128 ]; • int age; • int year; • }; • void main(){ • Student s1={"Amir","Programming",19,2002}, s2;
  • 29. • strcpy( s2.name,"Abbas Ali"); • s2.age=23; • cin>>s2.year; • cout<<s1.name<<" "<<s2.name<<endl; • cout<<s1.course<<" "<<s2.course<<endl; • cout<<s1.age<<" "<<s2.age<<endl; • cout<<s1.year<<" "<<s2.year<<endl; • getch(); • }
  • 30. Structure • Simple Variable of type • Structure Pointer to Structure • Arrays of Structures • Function that returns a Structure • Pass the Structure to functions
  • 31. Structure (Arithmetic Operation) • Student stdnt1 , stdnt2 ; • stdnt1 + stdnt2 ; //Wrong
  • 32. Structure • Student s1 , s2 ; • s1 = s2 ;
  • 33. #include<iostream> #include<iomanip> #include<cstring> using namespace std; struct Student{ char name [ 64 ]; char course [ 128 ]; int age; int year; }; int main(){ Student s1={"Amir","Programming",19,2016},s2; s2=s1; cout<<s2.name<<endl<<s2.course<<endl; cout<<s2.age<<endl<<s2.year<<endl; }
  • 34. Pointers to Structure • Student *sPtr , s1 ; • sPtr = &s1 ; • *sPtr.name ; Wrong • sPtr ->name ; • Same as • s1.name
  • 35. #include<iostream> #include<iomanip> #include<cstring> using namespace std; struct Student{ char name [ 64 ]; char course [ 128 ]; int age; int year; }; int main(){ Student s1={"Amir","Programming",19,2016},*s2; s2=&s1; cout<<s2->name<<endl; cout<<s2->course<<endl; cout<<s2->age<<endl; cout<<s2->year<<endl;
  • 36. using namespace std; struct Student{ char name [ 64 ]; char course [ 128 ]; int age; int year; }; int main(){ Student *s1=new Student; strcpy(s1->name,"Amir"); strcpy(s1->course,"Programming"); s1->age=19; s1->year=2016; cout<<s1->name<<endl; cout<<s1->course<<endl; cout<<s1->age<<endl; cout<<s1->year<<endl; }
  • 37. Passing Structures to Functions Call by value Call by Reference  X
  • 38. • struct Student{ • char name[20]; • int rollno; • }; • void init(Student&); • void main(){ • Student s1; • init(s1); • cout<<s1.name<<" "<<s1.rollno; • getch(); • } • void init(Student &a){ • cout<<"Plz enter the name n"; • gets(a.name); • cout<<"Plz enter the Roll No n"; • cin>>a.rollno; • }
  • 39. Arrays of Structure • Student s [ 100 ] ; • s [ 0 ].name ; • s [ 1 ].name ; • s [ 2 ].name ; • . . . • s [ 99 ].name ;
  • 40. Example 3 • struct Student { • char firstName [ 30 ] ; • char lastName [ 30 ] ; • char course [ 15 ] ; • char rollNo [ 10 ] ; • int age ; • float gpa ; • } ;