presentation on structure,functions and classes
DESCRIPTION
TRANSCRIPT
![Page 1: Presentation on structure,functions and classes](https://reader036.vdocuments.mx/reader036/viewer/2022062511/54be30234a7959552d8b456b/html5/thumbnails/1.jpg)
![Page 2: Presentation on structure,functions and classes](https://reader036.vdocuments.mx/reader036/viewer/2022062511/54be30234a7959552d8b456b/html5/thumbnails/2.jpg)
Arrays can be used to represent a group of data items that belong to the same type, such as int or float .
If we want to represent a collection of data items of different types using a single name then we cannot use an array.
The data type which we use is known as structure. Structure is user defined data type.
![Page 3: Presentation on structure,functions and classes](https://reader036.vdocuments.mx/reader036/viewer/2022062511/54be30234a7959552d8b456b/html5/thumbnails/3.jpg)
struct struct_name
{
data_type member1;
data_type member2;
… …
};
![Page 4: Presentation on structure,functions and classes](https://reader036.vdocuments.mx/reader036/viewer/2022062511/54be30234a7959552d8b456b/html5/thumbnails/4.jpg)
//header files
#include<stdio.h>
#include<conio.h>
//Structure declaration
struct student
{
// data types & members
int rollno;
char name[20];
};
![Page 5: Presentation on structure,functions and classes](https://reader036.vdocuments.mx/reader036/viewer/2022062511/54be30234a7959552d8b456b/html5/thumbnails/5.jpg)
void main ()
{
struct student S1; //Structure variable
printf(“Input the name of student “);
scanf(“%d”,&S1.name);
printf(“Input the Roll number of student “);
scanf(“%d”,&S1.rollno);
printf(“name =%d“S1.name);
printf(“Roll number=%d“S1.rollno);
getch();
}
![Page 6: Presentation on structure,functions and classes](https://reader036.vdocuments.mx/reader036/viewer/2022062511/54be30234a7959552d8b456b/html5/thumbnails/6.jpg)
Input the name of student abc
Input the Roll number of student 1
name = abc
Roll number = 1
![Page 7: Presentation on structure,functions and classes](https://reader036.vdocuments.mx/reader036/viewer/2022062511/54be30234a7959552d8b456b/html5/thumbnails/7.jpg)
10 11 12 13 14 15 16 17 18 19
0 1 2 3 4 5 6 7 8 9
20 21
rollno
name
![Page 8: Presentation on structure,functions and classes](https://reader036.vdocuments.mx/reader036/viewer/2022062511/54be30234a7959552d8b456b/html5/thumbnails/8.jpg)
Unions are a concept borrowed from structures and therefore follow the same syntax as structures .
There is a major difference between them in terms of storage .
A union may contain many members of different types ,it can handle only one member at a time.
![Page 9: Presentation on structure,functions and classes](https://reader036.vdocuments.mx/reader036/viewer/2022062511/54be30234a7959552d8b456b/html5/thumbnails/9.jpg)
union union_name
{
data_type member1;
data_type member2;
… …
};
![Page 10: Presentation on structure,functions and classes](https://reader036.vdocuments.mx/reader036/viewer/2022062511/54be30234a7959552d8b456b/html5/thumbnails/10.jpg)
//header files
#include<stdio.h>
#include<conio.h>
//Union declaration
union student
{
// data types & members
int rollno;
char name[20];
};
![Page 11: Presentation on structure,functions and classes](https://reader036.vdocuments.mx/reader036/viewer/2022062511/54be30234a7959552d8b456b/html5/thumbnails/11.jpg)
void main (){union student S1; //Union variable printf(“Input the name of student “);scanf(“%d”,&S1.name);printf(“Input the Roll number of student “);scanf(“%d”,&S1.rollno);printf(“name =%d“S1.name);printf(“Roll number=%d“S1.rollno);getch();}
![Page 12: Presentation on structure,functions and classes](https://reader036.vdocuments.mx/reader036/viewer/2022062511/54be30234a7959552d8b456b/html5/thumbnails/12.jpg)
Input the name of student abc
Input the Roll number of student 1
name = abc
Roll number = 1
![Page 13: Presentation on structure,functions and classes](https://reader036.vdocuments.mx/reader036/viewer/2022062511/54be30234a7959552d8b456b/html5/thumbnails/13.jpg)
10 11 12 13 14 15 16 17 18 19
0 1 2 3 4 5 6 7 8 9
name
rollno
![Page 14: Presentation on structure,functions and classes](https://reader036.vdocuments.mx/reader036/viewer/2022062511/54be30234a7959552d8b456b/html5/thumbnails/14.jpg)
Classes are same as structure but with a small difference ie. Structure are having data only(int or float) but classes are having data and functions together.
The class has a mechanism to prevent direct access to its members ,which is the central idea of object-oriented programming.
Classes are user defined data types.In short structure + functions = classes.
![Page 15: Presentation on structure,functions and classes](https://reader036.vdocuments.mx/reader036/viewer/2022062511/54be30234a7959552d8b456b/html5/thumbnails/15.jpg)
Data
Functions Class
A class definition begin with the keyword class.The body of the class is contained with in the
brace { };
![Page 16: Presentation on structure,functions and classes](https://reader036.vdocuments.mx/reader036/viewer/2022062511/54be30234a7959552d8b456b/html5/thumbnails/16.jpg)
class <name of class>{ private: declaration of variables ; declaration of functions ; public: declaration of variables ; declaration of functions;};
![Page 17: Presentation on structure,functions and classes](https://reader036.vdocuments.mx/reader036/viewer/2022062511/54be30234a7959552d8b456b/html5/thumbnails/17.jpg)
//Header files
#include<iostream.h>
#include<conio.h>
class student //class declaration
{
private:
int rollno; // data members
char name[20];
public:
void getdata(); //functions
![Page 18: Presentation on structure,functions and classes](https://reader036.vdocuments.mx/reader036/viewer/2022062511/54be30234a7959552d8b456b/html5/thumbnails/18.jpg)
//defining member function inside the class definition
void display()
{
cout<<“Name=“<<name<<endl;
cout<<“Roll number=“<<rollno<<endl;
}
};
![Page 19: Presentation on structure,functions and classes](https://reader036.vdocuments.mx/reader036/viewer/2022062511/54be30234a7959552d8b456b/html5/thumbnails/19.jpg)
// defining member functions outside the class definition
void student :: getdata()
{
cout<<“Input the name of student”;
cin>>name;
cout<<“Input the roll number”;
cin>>rollno;
}
![Page 20: Presentation on structure,functions and classes](https://reader036.vdocuments.mx/reader036/viewer/2022062511/54be30234a7959552d8b456b/html5/thumbnails/20.jpg)
main()
{
cout<<“We are in main function “;
student S1; //creat object S1
S1.getdata(); //call member function
S1.display(); //call member function
getch();
}
![Page 21: Presentation on structure,functions and classes](https://reader036.vdocuments.mx/reader036/viewer/2022062511/54be30234a7959552d8b456b/html5/thumbnails/21.jpg)
Input the name of student abc
Input the roll number 1
Name = abc
Roll number = 1
![Page 22: Presentation on structure,functions and classes](https://reader036.vdocuments.mx/reader036/viewer/2022062511/54be30234a7959552d8b456b/html5/thumbnails/22.jpg)
![Page 23: Presentation on structure,functions and classes](https://reader036.vdocuments.mx/reader036/viewer/2022062511/54be30234a7959552d8b456b/html5/thumbnails/23.jpg)