I had explained Built-in and Derived Data types in previous blogs.
Now, Let's resume our topic from User Defined Data types.
Simple definition - User defined data types are the data types which the User define by himself/herself.
We need these data types because there are certain cases when the pre-defined data types won't help us, e.g. think of a list, which contain every information about every particular student learning in a school. Like Student name, Student IDs, Student class, Student section etc. You can think of using arrays, but arrays can only contain one field of data of built-in datatypes like int, char etc. Which is not sufficient.
Now, We will discuss the following topics -
- Class.
- Structures.
- Enumeration.
- Union.
Classes
1 2 3 4 5 6 7 8 9 | class Class_Name { // Class body starts here Access Specifiers: Data Members; Member functions(){;} }; // semi-colon is important here. |
Now, let's see it line by line.
- In Line 1, We have class Class_Name, here class is keyword ( keywords are words that are reserved by the c++ compiler, with some specific meaning), class tells us that, we are defining/ declaring a class. Class_Name is an identifier (it represents the new data types) and it is the name that the user wants to give to the class.
- In Line 3, We have Access Specifiers, Access specifiers specifies that who can access the members of our class like data in our class and function in our class etc. There are three access specifiers that we can use in our class -
- private.
- protected.
- public.
- Data Members - Data members is the data that our class contains, taking student list example - the data is equivalent to the Names, IDs etc.
- Member functions - For modifying and getting some useful results from the data, we need some function to act upon that data and those functions are called Member functions. (Functions present inside a class are called member functions, and other function are simply called function).
#include<iostream> using namespace std; class YourCppTutor{ public: void What_are_we_doing_here() { cout <<"We are learning C++" << endl; } }; int main() { YourCppTutor object1; YourCppTutor *object2; object1.What_are_we_doing_here(); object2->What_are_we_doing_here(); return 0; }
Structures
#include<iostream> #include<cstring> using namespace std; struct Students { char name[20]; // Unlike classes we can't initialize any data member within structure. int ID; char Class[30]; char Section; }; void Print(struct Students x) // Function for printing student information { cout << "Student name is : " << x.name << endl; cout << "Student Id is : " << x.ID << endl; cout << "Students studies in class " << x.Class << " and section is " << x.Section << endl; } int main() { struct Students first; strcpy(first.name, "JOHN"); first.ID = 1; strcpy(first.Class, "Computer Science"); first.Section = 'B'; Print(first); /* Another and simple way of initializing first is to pass RValues like an array struct Students first = {"JOHN", 1, "Computer Science", 'B'}; */ return 0; }
See output here.
Each Member of a structure has it's own memory space and all this memory spaces are included in the Students, For, clarity you can think of Students structure as a Universal Set, while the members can be considered as Subsets of universal Set and No set intersects with each other.
In the above example you must have noticed three things
- Every time we write our user-defined type i.e. Students, we write struct in front of it. Which is a necessity.
- In structures we can use any type of data as we want. and combine them to make a single block of structure.
- We accessed member objects with dot operator(particularly data members in this example), the reason being first is not a pointer.
Enumeration
#include<iostream> using namespace std; enum Menu{ // enum is keyword and Menu is an identifier Open, Close, New, Edit, Cut, Paste }; int main() { Menu task = Open; // Remember any object of Menu type can only take values defined in Menu cout << "Task has value : " << task << endl; // Defaul Value Start from 0; }
#include<iostream> using namespace std; enum Menu{ // enum is keyword and Menu is an identifier Open=1, Close=0, New = 3, Edit = 7, Cut, Paste }; int main() { cout << "Open is : " << Open << endl; cout << "Close is : " << Close << endl; cout << "New is : " << New << endl; cout << "Edit is : " << Edit << endl; cout << "Cut is : " << Cut << endl; cout << "Paste is : " << Paste << endl; return 0; }
Open is : 1 Close is : 0 New is : 3 Edit is : 7 Cut is : 8 Paste is : 9
Union
#include<iostream> #include<cstring> using namespace std; union Students { char name[20]; int ID; char Class[30]; char Section; }; void Print(union Students x) // Function for printing student information { cout << "Student name is : " << x.name << endl; cout << "Student Id is : " << x.ID << endl; cout << "Students studies in class " << x.Class << " and section is " << x.Section << endl; } int main() { union Students first; strcpy(first.name,"JOHN"); // Copying "JOHN" in name of first first.ID = 1; strcpy(first.Class, "Computer Science"); // Copying "Computer Science" in class first.Section = 'B'; Print(first); return 0; }
We will get output like this.
Student name is : Bomputer Science Student Id is : 1886220098 Students studies in class Bomputer Science and section is B
The output we got is weird, isn't it. Actually, the reason behind this random output is something like this-
Due to sharing of memory, when one member object is initialized, it affects the value of other member objects which were previously initialized.
I will discuss One more User-defined data type, Typedef in future slides.
Stay tuned for the upcoming content. For any query leave a comment below.
- Lippman, Stanley B. c++ primer 3rd edition(april 2, 1998).
Comments
Post a Comment