objects and classes in c++ object oriented programming

21
Objects and Classes

Upload: apcoms

Post on 16-Jan-2023

0 views

Category:

Documents


0 download

TRANSCRIPT

Objects and Classes

C++ objects as Data Typesclass Distance{private:

int feet; int inches;public: void setdist(int f, int i) { feet = f; inches = i; } void showdist() { cout << feet << "\'"<<inches <<

"\"" <<endl; }};

Object as Function Arguments class Distance { private:

int feet; float inches;

public: void getdist() {

cout << "Enter Feet: " ; cin >> feet; cout<< "Enter Inches: "; cin>> inches;

}

void setdist(int f, float i) { feet = f; inches =i; }

void showdist() { cout << feet << "\'" << inches << "\"" <<endl; }

void add_dist(Distance d1, Distance d2) { inches = d1.inches + d2.inches; feet = 0; if(inches >=12.0) {

inches -= 12.0; feet++; } feet += d1.feet + d2.feet; }

} ; // class ends

int main(){

Distance dist1, dist2, dist3; dist2.setdist(11,6.25); dist1.getdist(); dist3.add_dist(dist1,dist2); dist1.showdist(); dist2.showdist(); dist3.showdist(); return 0;}

Let’s Revise Constructor

Special Member Functions

Constructor:– Public function member– called when a new object is created (instantiated).

– Initialize data members.– Same name as class– No return type– Several constructors

• Function overloading

class Circle{ private:

double radius; public:

Circle() { radius = 0.0;}Circle(int r);void setRadius(double r){radius =

r;}double getDiameter(){ return radius

*2;}double getArea();double getCircumference();

};Circle::Circle(int r){ radius = r;}double Circle::getArea(){ return radius * radius * (22.0/7);}double Circle:: getCircumference(){ return 2 * radius * (22.0/7);}

Returning Objects from Functions class Distance { private: int feet; float inches;

public:Distance() // constructor {

feet = 0; inches = 0.0; }

Distance(int f, float i) // parameterized constructor

{ feet = f;

inches =i; }void getdist() // get length from user

{ cout << "Enter Feet: " ; cin >> feet; cout<< "Enter Inches: "; cin>> inches; }void showdist() // display distance { cout << feet << "\'" << inches << "\""

<<endl; }

Distance add_dist(Distance d1, Distance d2) { Distance temp; temp.inches = d1.inches + d2.inches; temp.feet = 0; if(temp.inches >=12.0) {

temp.inches -= 12.0; temp.feet++; } temp.feet += d1.feet + d2.feet;

return temp; }

} ; // class ends

int main(){

Distance dist1, dist3; Distance dist2(11,6.25); dist1.getdist(); // get dist1 from user dist3 = dist3.add_dist(dist1,dist2); // object is

returned to dist3 on L.H.S from add_dist function

//display all lengths dist1.showdist(); dist2.showdist(); dist3.showdist();

return 0;}

Another Example : Class Bookclass Book { int PageCount; int CurrentPage;

public:

Book( int NumPages) { PageCount = NumPages;

}

void SetPage( int PageNumber) { CurrentPage=PageNumber; }

int GetCurrentPage( void ) { return CurrentPage; }

};

int main() { Book ABook(128) ; ABook.SetPage( 56 ) ; cout << "Current Page " << ABook.GetCurrentPage() << endl;

return 0; }

Some practice……

Write a Student class having two data members, i.e. name, marks.

Writer 2 constructors for it, one non-parameterized and other having two parameters (name, marks)

Write a function that will show the values against a student object.

Some more examples

Distance Function, having one input parameterDistance add_dist(Distance d2) { Distance temp; temp.inches = inches + d2.inches;

if(temp.inches >=12.0) { temp.inches -= 12.0; temp.feet = 1; }

temp.feet += feet + d2.feet; return temp; }

int main(){

Distance dist1, dist3; Distance dist2(11,6.25); dist1.getdist(); // get dist1 from user dist3 = dist1.add_dist(dist1);//display all lengths dist1.showdist(); dist2.showdist(); dist3.showdist();

return 0;}

Thank you