ComputerProgrammingandBasicSoftwareEngineering5.ObjectsandClassesinC++WhatisaClass?A Class is a blueprint or prototype that defines thevariables and methods common to all objects of acertain kindEvery object of a Class is an instance of that classBenefit -ReusabilityThis arrangement saves effort in developing a number ofobjects of the same kind.口口区JavaWorkShop-Blink.javaUsually objects of a classFile Edit Build Debug Browse Project GUl Preferences windows Helpareused manytimes in anapplicationLine1 Col1Project BlinkObjects of class button
Objects of class button What is a Class? • A Class is a blueprint or prototype that defines the variables and methods common to all objects of a certain kind • Every object of a Class is an instance of that class • Benefit - Reusability This arrangement saves effort in developing a number of objects of the same kind. Usually objects of a class are used many times in an application Computer Programming and Basic Software Engineering 5. Objects and Classes in C++
ComputerProgrammingand BasicSoftwareEngineering5.Objects andClassesinC++TheButtonClass:VariablesColor(e.g.Grey)Methodsize(e.g.2cmx2cm)Press()shape (e.g.Rectangular)Instantiation(protruded)Instance ofButton ClassInstantiationInstance ofButtonClass
Variables Color (e.g. Grey) Size (e.g. 2cm x 2cm) Shape (e.g. Rectangular) (protruded) Method The Button Class: Instance of Button Class Instance of Button Class Instantiation Instantiation Press( ) Computer Programming and Basic Software Engineering 5. Objects and Classes in C++
ComputerProgrammingandBasicSoftwareEngineering5.Objects andClassesinC++5.2 ObjectsandClassesinC++
5.2 Objects and Classes in C++ Computer Programming and Basic Software Engineering 5. Objects and Classes in C++
ComputerProgrammingand BasicSoftwareEngineering5.ObjectsandClassesinC++DeclaringClassesin C++To declare a class,use the class keyword as followsclass CatP.121unsigned intitsAge;//Member variableitsWeight;// Member variableunsigned intvoid Meow;//Member function1 :Definition ofMeow)shouldfollow somewhereDeclaring this class does NOT allocate memoryfora CatOnlytell the compiler what a Cat is, howbig a Cat is (bymembervariables, e.g. itsAge, itsWeight), what a Catwoulddo(bymemberfunctions,e.g.Meow())Declaration of classes should beplaced intheheaderfileand included into your program.#include
• To declare a class, use the class keyword as follows • Declaring this class does NOT allocate memory for a Cat. • Only tell the compiler what a Cat is, how big a Cat is (by member variables, e.g. itsAge, itsWeight), what a Cat would do (by member functions, e.g. Meow() ). • Declaration of classes should be placed in the header file and included into your program. Declaring Classes in C++ class Cat { unsigned int itsAge; // Member variable unsigned int itsWeight;// Member variable void Meow(); // Member function }; Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ #include Definition of Meow() should follow somewhere P.12