Document Details

SolicitousElf

Uploaded by SolicitousElf

CUNY John Jay College of Criminal Justice

Full Transcript

CSCI 373 LESSON 1 (GOODRICH 1.5, 2.1, 2.2) 1 Objectives Classes & Objects Encapsulation Friendship Inheritance Polymorphism Abstract Classes Interface 2 ...

CSCI 373 LESSON 1 (GOODRICH 1.5, 2.1, 2.2) 1 Objectives Classes & Objects Encapsulation Friendship Inheritance Polymorphism Abstract Classes Interface 2 Classes & Objects obj1 = car(“polo”, “green”) type color obj2 = car(“mini”, “aqua”) obj3 = car(“beetle”, “red”) In OOP, a class is a blueprint for creating objects, providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods). An object, is defined as an instance or an instantiation of a class. 3 Classes & Objects //Class class Car { public: Car(string, string);//Constructor ~Car(); //destructor string model; string color; void setColor(string); }; //Object Car obj1("polo", "green"); Car obj2("mini", "aqua"); Car obj3("beetle", "red"); 4 Constructors A constructor is a member function with – The same name as the class – Has no return type – A constructor is automatically called when an object is created. – Can have 0 or more input arguments Default constructor – A constructor with no arguments. – C++ always creates a default constructor with an empty body if the programmer does not explicitly create a constructor Overloading – More than one function with the same name and same return type but with a different type of parameters Copy constructor – A constructor that copies value from one instance of the object to 5 another Constructors //Car.h class Car { public: //default Car(); //Constructor with parameters Car(string, string); //copy constructor //constant reference as argument Car(const Car &other); string model; string color; }; 6 Constructors //Car.cpp //Default constructor Car::Car() { Car::Car() { } model = "null"; color = "null"; } //Constructor with parameters Car::Car(string m, string c) { model = m; color = c; } //Copy constructor Car::Car(const Car &other) { model = other.model; color = other.color; } 7 Constructor using initializer list //Car.h Car(string m, string c, Color objCol); //Car.cpp //normal Car::Car(string m, Color objC){ model = m; objColor = objC; }; //using initializer Car::Car(string m, Color objC):model(m), objColor(objC){ }; 8 How to invoke the constructors //default Car car1; //parametrized constructor Car car2("polo", "green"); //parametrized using initializer list Car car3("polo", Color(20,100,255, "Green")); //copied from car3 Car car4 = car3; //default constructor Car* pCar1 = new Car; //parametrized constructor Car* pCar2 = new Car("polo", "green"); //default constructor Car car; 9 Destructors A destructor is a member function which is called automatically when the object is destroyed, – when the program terminates or when memory is released A destructor of a class has the same name with the class name preceded by ~(tilde char) (class name Car, destructor is denoted ~Car) It does not return anything and does not take any arguments We need destructors to deallocate any memory used by data members If the object is created by using new or the constructor uses new to allocate memory, the destructor should use delete to free the memory: https://www.jjay-csci272.org/course- 10 materials/lesson-8 Destructors //Car.h class Car { public: string model; string color; Color objColor; Car();//default Car(string, string);//Constructor with parameters Car(string, Color);// Constructor using initializer ~Car();//destructor }; 11 Destructors //Car.cpp Car::Car { } Car::Car(string m, Color objC): model(m), objColor(objC){ }; Car::Car(string m, string c) { model = m; color = c; } Car::~Car() { cout

Use Quizgecko on...
Browser
Browser