Lecture 4.pptx - CS 122 DATA STRUCTURES AND ALGORITHMS STRUCTURES USER DEFINED DATA TYPES \u2022 Apart from the primitive built-in data types such as int

Lecture 4.pptx - CS 122 DATA STRUCTURES AND ALGORITHMS...

This preview shows page 1 - 6 out of 17 pages.

CS 122: DATA STRUCTURES AND ALGORITHMS STRUCTURES
Image of page 1
USER DEFINED DATA TYPES Apart from the primitive built-in data types such as int, float and char; C++ supports user defined data types. Two mechanisms Using struct construct to defined a structure (supported in C and C++) Using class approach (Object Oriented Programming) In both cases, the programmer can define an new data type by making use of the existing built-in types.
Image of page 2
USER DEFINED DATA TYPES Example of a struct data type to represent a student struct Student{ string surname; string first_name; string reg_number; char gender; int age; string deg_program; }; The programmer can then declare variables of type Student as Student student1; Student student2, student3;
Image of page 3
USER DEFINED DATA TYPES Example of a class declaring a student data type class Student{ private string surname; private string first_name; private string reg_number; private int age; private char gender; private string deg_program; //functions public void enroll(); public void registerCourse() }; Creating objects Student student1; Student student2 = new Student();
Image of page 4
USER DEFINED DATA TYPES The struct and class constructs have some differences All member fields of a struct are public whereas in class you can have public and private members In a struct, you can not incorporate functions (except macros) but in a class you can have both member fields and functions that operate on the fields A struct is only declared. A class is declared and defined (i.e implementing the functions in the class)
Image of page 5
Image of page 6

You've reached the end of your free preview.

Want to read all 17 pages?

  • Spring '19

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors