1 / 8

Programming Paradigms

Programming Paradigms. Lecturer Hamza Azeem. Lecture 9. Constructors Objects as Function Arguments. Constructors. It is a member function which initializes a class A constructor has: The same name as the class itself No return type. Classes. class KIETStudent { private: int age;

arlais
Download Presentation

Programming Paradigms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Paradigms LecturerHamza Azeem

  2. Lecture 9 ConstructorsObjects as Function Arguments

  3. Constructors • It is a member function which initializes a class • A constructor has: • The same name as the class itself • No return type

  4. Classes class KIETStudent { private: int age; char *name; double cgpa; char *intake; public: KIETStudent() { age = 0 ; name = “ “ ; cgpa = 0.0 ; intake = “ “ ; } intget_age(); };

  5. Constructors • Constructors are used for initialization of variables • In previous example the variables were intialized • The preferred way for intializing variables is using Initializer List

  6. Constructor using Initializer List class KIETStudent { private: int age; char *name; double cgpa; char *intake; public: KIETStudent() : age(0), name(“ “), cgpa(0), intake(“ “) { // empty body } void set_age(intnewage); intget_age(); };

  7. Constructor Overloading • Constructor Overloading meand creating more than one constructor • There is always a default constructor plus any additional constructors • For e.g. you want to intialize the variable with your own set of values while creating instance • Something like this • KIETStudent S1(”Ali Khan”, 25, 3.01, ”FALL09”); • Instead of • KIETStudent S1;

  8. Constructor Overloading class KIETStudent { private: int age; char *name; double cgpa; char *intake; public: KIETStudent() : age(0), name(“ “), cgpa(0), intake(“ “) { // empty body } KIETStudent(char *new_name, intnew_age, double new_cgpa, char *new_intake) : name(new_name), intake(new_intake), age(new_age), cgpa(new_cgpa) { // empty body } intget_age(); };

More Related