
Object-Oriented Programming and Class Design with Examples
Learn about object-oriented programming, class design, and object creation using classes and objects in various programming languages such as C++ and pseudocode. Dive into examples like creating a Circle class with methods to calculate area and exploring constructors in a BMW_Z4 class. Get insights into different programming concepts through practical examples.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Classes & Classes & Objects Objects OBJECT OBJECT- -ORIENTED ORIENTED PROGRAMMING AND PROGRAMMING AND CLASS DESIGN CLASS DESIGN
Class Circle Pseudocode CLASS Circle BEGIN CREATE Radius = 1.0 CONSTRUCTOR Circle //called default constructor BEGIN END CONSTRUCTOR CONSTRUCTOR Circle (NewRadius) //called constructor BEGIN Radius = NewRadius END CONSTRUCTOR METHOD getArea () //compute and return circle area BEGIN RETURN (Radius * Radius * 3.14159) END METHOD END CLASS Ps
Class Circle class Circle { public: double radius = 1.0; Circle(){ } Circle(double newRadius) //another constructor { radius = newRadius; } double getArea() //calculates area { return (radius*radius*3.14159); } }; //default constructor
Driver in Pseudocode CLASS TestCircle BEGIN CREATE circle1, circle2 AS Circle circle1 = NEW circle() circle2 = NEW circle(25.0) PRINT ("Circle 1 area = " + circle1.getArea()) PRINT ("Circle 2 area = " + circle2.getArea()) END TestCircle // create two variables // create circle1 object // create circle2 object Outputs: Circle 1 area = 3.14159 Circle 2 area = 1963.4937499999999 Ps
Driver in C++ int main () { Circle c1; cout<< "The area of the c1 is " << c1.getArea()<<endl; Circle c2(25); cout<<"The area of c2 is " << c2.getArea(); }
Example CLASS BMW_Z4 BEGIN CONSTRUCTOR BMW_Z4() // constructor #1 BEGIN ModelYear = 2004 TopUp = false LicensePlate = "DEALER" END CONSTRUCTOR CONSTRUCTOR BMW_Z4(parameter: year) // constructor #2 BEGIN ModelYear = year TopUp = false LicensePlate = "DEALER" END CONSTRUCTOR END CLASS Ps
C++ Constructor Example class BMW_Z4 { int modelYear; String licensePlate; boolean topUp; BMW_Z4 () { modelYear = 2004; topUp = false; licensePlate = "DEALER"; } BMW_Z4 (int year) { modelYear = year; topUp = false; licensePlate = "DEALER"; } }
Properties Example Pseudocode CLASS BMW_Z4 BEGIN PRIVATE ModelYear PRIVATE LicensePlate PUBLIC METHOD SetModelYear (Year) BEGIN ModelYear = Year END PUBLIC METHOD GetModelYear () BEGIN RETURN ModelYear END PUBLIC METHOD SetLicensePlate (value) BEGIN LicensePlate = value END PUBLIC METHOD GetLicensePlate () BEGIN RETURN LicensePlate END END CLASS Ps
Class BMW_Z4 class BMW_Z4 { public: int modelYear; string licensePlate; bool topUp; BMW_Z4(); BMW_Z4(int year); void SetModelYear(int Year); int getModelYear(); void SetLicensePlate(string value); string getLicensePlate(); void SetTopUp(bool value); bool getTopUp(); private: string LicensePlate; bool TopUp; };
BMW_Z4::BMW_Z4 (void) { modelYear = 2004; topUp = false; licensePlate = "DEALER"; } BMW_Z4::BMW_Z4 (int year) { modelYear = year; topUp = false; licensePlate = "DEALER"; } void BMW_Z4 :: SetModelYear(int Year) { modelYear = Year; } int BMW_Z4:: getModelYear() { return modelYear; } void BMW_Z4 :: SetLicensePlate(string value) {licensePlate = value;} string BMW_Z4 ::getLicensePlate(){ return licensePlate; } void BMW_Z4::SetTopUp(bool value) { TopUp = value; } bool BMW_Z4::getTopUp() { return TopUp; }
Creating Object and using functions: int main(){ BMW_Z4 s1(1995); s1.SetModelYear(678); s1.SetLicensePlate("string value"); cout << s1.getModelYear(); cout << s1.getLicensePlate(); }