






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Object Oriented Programming derives from the basic programming and structures studied till now. Objects and Classes are simple extension of those concepts, mainly structures , functions and variables. An example will illustrate further: #include
class smallobj //class definition { private : //All data and functions are private unless declared public int somedata; //class private data member public : void setdata (int d) // member function to set somedata, Also called Method { somedata = d;} void showdata ( ) // member function to print somedata { cout << “Data is” << somedata << endl; }
}; // Note: Semicolon “;” is required as in structures
int main ( ) { smallobj s1, s2; // Objects s1 and s2 of class smallobj
s1. setdata(1066); //calling member function setdata for object s s2. setdata(1776); //using Dot operator, Also called Class member // access operator
s1. showdata( ); //calling member function showdata for object s s2. showdata( ); // Also called Message passing return 0; } //-------------------------------------------------------------------------------------------------------- Main Keywords: Class Creates a new class Private Declares the data/functions private Public Declares the data/functions public Member Functions Functions in Class (Methods) Data Members Data Variables in Class Dot Operator Class Member Access Operator #include
class part
private: int model_number; //ID number of widget int part_number; //ID number of widget parts float cost; public: void setpart (int mn, int pn, float c) { model_number = mn; //assign values to private part_number = pn; //data members cost = c; // Note: no need of dot operator }
void showpart ( ) { cout << “Model ” << model_number; //display private data member cout << “, part ” << part_number; cout << “, costs Rs ” << cost << endl; // Again no dot operator } };
int main ( ) { part part1, part2; //defining objects of class part part1.setpart (6244, 373, 217.55F); //calling member function to set values part2.setpart (1000, 200, 100.11F); //using dot operator … i.e. the period “.” part1.showpart ( ); part2.showpart ( ); return 0; } //------------------------------------------------------------------------------------------------------
#include
class Counter { private: unsigned int count; //Data Member: to maintain count public: Counter( ) : count (10) // Constructor, Same name! initializing count { } // Empty Body. Note: The parameter list can have //variables, plus the body can have expressions. // Constructors have NO Return type! void inc_count ( ) //increment count member function { count++ ; }
int get_count ( ) //member function to return data member count { return count; } };
int main ( ) { Counter c1, c2; //create objects and initialize
cout << endl << “c1 = ” << c1.get_count( ); //display private data member of c1 object cout << endl << “c2 = ” << c2.get_count( );
c1.inc_count ( ); c2.inc_count ( ); c2.inc_count ( );
cout << endl << “c1 = ” << c1.get_count( ); //display again cout << endl << “c2 = ” << c2.get_count( ); cout << endl; return 0; } //------------------------------------------------------------------------------------------------------------
#include
class circle { private: int xCo, yCo, radius, fillcolor, fillstyle; //Data Members public: circle (int x, int y, int r, int color, int fs) : // Constructor, xCo(x), yCo(y), radius(r), fillcolor(fc), fillstyle(fs) { } };
The following example demonstrates constructor overloading, defining member functions outside the class, and objects as function argument.
#include
Distance (int ft, float in) : feet(ft), inches (in) // Constructor 2 , with 2 args { }
void setdist (int ft, float in) //Member Function: sets Distance to arguments { feet = ft; inches = in; }
void getdist ( ) //Get distance from User { cout << “\nEnter feet: ” ; cin >> feet; cout << “Enter inches: ” ; cin >> inches; } void showdist ( ) { cout << feet << “\’- ” << inches << “\” \n”; } //Display distance
void add_dist (Distance d1, Distance d2); //Declaration of function outside }; void Distance:: add_dist (Distance d1 , Distance d2 ) //add object lengths d1 and d { //uses scope resolution operator :: inches = d1.inches + d2.inches; // inches refers to the data member of // object for which add_dist is called! feet = 0; //Similarly feet refers to feet data member if (inches >= 12.0 //reduce inches below 12 { inches -= 12.0; feet++; } feet += d1.feet + d2.feet; //add feet of d1 & d2 lengths } int main ( ) { // Note: Constructors are called… Distance dist1, dist3; //define two objects of class Distance Distance dist2(11, 6.25); //define and initialize dist2 object dist1.getdist ( ); //get distance of d1 object dist3.add_dist (dist1, dist2); //dist3 = dist1 + dist2, Note: add_dist can // access data members of dist3, dist1, dist cout << “\ndist1 = ”; dist1.showdist ( ); cout << “\ndist2 = ”; dist2.showdist ( ); cout << “\ndist3 = ”; dist3.showdist ( ); return 0; }
using namespace std;
enum Suit {clubs, diamonds, hearts, spades}; const int jack = 11; //cards 2 to 10, then 11, 12, 13, 14 jack to ace const int queen = 12; const int king = 13; const int ace = 14;
class card { private: int number; //2 to 10, jack, queen, king, ace Suit suit; //clubs … spades public: card ( ) // Constructor 1, With no arguments, or initializations { } card (int n, Suit s) : number (n), suit (s) // Constructor 2, With two arguments { } void display ( ); //display card, Member Function Defined outside bool isEqual (card c2); //same as another card, Member Function Defined outside };
void card::display ( ) //displaying cards, using scope resolution operator ‘::’ { if (number >= 2 && number <= 10) cout << number << “ of ”; else switch (number) { case jack: cout << “jack of ”; break; case queen: cout << “queen of ”; break; case king: cout << “king of ”; break; case ace: cout << “ace of ”; break; } switch (suit) { case clubs: cout << “clubs ”; break; case diamonds: cout << “diamonds ”; break; case hearts: cout << “hearts ”; break; case spades: cout << “spades ”; break; } }
bool card::isEqual (card c2) //return true of card c2 equal to Member Function’s object { return ( number == c2.number && suit == c2.suit )? true : false ; // conditional operator }
int main ( ) { card temp, chosen, prize; //define cards, call Constructor with no arguments int position;
card card1 (7, clubs ); //initialize card1 to card3, calling Constructor with card card2 ( jack, hearts); //two arguments for each card. Note: constructor calls card card3 ( ace, spades); //are different from structure declaration = {ace, spade};
cout << “Card 1 is the ” ; card1.display( ); cout << “Card 2 is the ” ; card2.display( ); cout << “Card 3 is the ” ; card3.display( );
prize = card3; // ace of spades is the card the user has to find
cout << “Swapping card 1 and card 3 \n”; temp = card3; card3 = card1; card1 = temp; cout << “Swapping card 2 and card 3 \n”; temp = card3; card3 = card2; card2 = temp; cout << “Swapping card 1 and card 2 \n”; temp = card2; card2 = card1; card1 = temp;
cout << “Now, where is the ace of spades (Enter 1, 2, or 3)?”; cin >> position;
switch (position) { case 1: chosen = card1; break; case 2: chosen = card2; break; case 3: chosen = card3; break; }
if (chosen.isEqual (prize)) cout << “That’s right! You win!\n”; else cout << “Sorry, you lose! \n”;
cout << “ You chose the ”; chosen.display( ); //display the chosen card
return 0; }
#include