Graded Lab The Hidden This Pointer, Static Data Member
Graded Lab The Hidden This Pointer, Static Data Member
Objectives
The purpose of this activity is to introduce students with the concepts and use of this pointer, static member variable, its
use and const qualifier, use of key word static along with the creation of arrays of objects.
If you have a constructor (or member function) that has a parameter of the same name as a member variable, you can
disambiguate them by using “this”:
class Something
private:
int nData;
public:
Something(int nData)
this->nData = nData;
};
Sample Code 02:
class Calc
private:
int m_nValue;
public:
Calc() { m_nValue = 0; }
{ m_nValue *= nValue; }
};
int main()
Calc cCalc;
cCalc.Add(5);
cCalc.Sub(3);
cCalc.Mult(4);
cout<<cCalc.GetValue()<<endl; return 0;
}
Sample Code 03:
class Calc
private:
int m_nValue;
public:
Calc() { m_nValue = 0; }
};
int main()
Calc cCalc;
cCalc.Add(5).Sub(3).Mult(4);
cout<<cCalc.GetValue()<<endl;
return 0;
}
};
Const class objects and member Functions
class Date
private:
int m_nMonth;
int m_nDay;
int m_nYear;
Date() { }
public:
};
std::cout << cDate.GetMonth() << "/" << cDate.GetDay() << "/" << cDate.GetYear() << std::endl;
}
int main()
return 0;
private:
public:
};
int Something::s_nIDGenerator = 1;
int main()
Something cFirst;
Something cSecond;
Something cThird;
return 0;
}
Static Member Function
class IDGenerator
private:
public:
return s_nNextID++;
};
int IDGenerator::s_nNextID = 1;
int main()
cout << "The next ID is: " << IDGenerator::GetNextID() << endl;
return 0;
}
Array of Objects
Sample Code 01: Create an array of objects
#include <iostream>
class MyClass {
int x;
public:
void setX(int i) { x = i; }
int main()
{
MyClass bs[4];
int i;
obs[i].setX(i);
cout << "obs[" << i << "].getX(): " << obs[i].getX() << "\n";
return 0;
}
Sample Code 02: An array of objects: call its method
#include <iostream>
class MyClass
public:
MyClass() {
itsAge = 1;
itsWeight=5;
}
~MyClass() {}
return itsAge;
}
return itsWeight;
}
itsAge = age;
}
private:
int itsAge;
int itsWeight;
};
int main()
{
MyClass myObject[5];
int i;
for (i = 0; i < 5; i++)
myObject[i].SetAge(2*i +1);
return 0;
}
Sample Code 03: Initialize an array of objects without referencing the constructor directly
int main()
{
MyClass obs[4] = { -1, -2, -3, -4 }; int i;
return 0;
}
Sample Code 04: Initialize an array of objects by referencing the constructor directly
#include <iostream>
using namespace std;
class MyClass {
int x;
public:
MyClass(int i) { x = i; }
int getX() { return x; }
};
int main()
{
MyClass obs[4] = { MyClass(-1), MyClass (-2), MyClass (-3), MyClass (-4) };
int i;
return 0;
}
Lab Tasks
Task 1:
A book shop maintains the inventory of books that are being sold at the shop. The list includes details
such as author, title, price, publisher and stock position. Whenever a customer wants a book, the sales person
inputs the title and author and the system searches the list and displays weather it available or not. If it is not,
an appropriate message is displayed. If it is, then the system displays the book details and request for the
number of copies required. If the requested copies are available, the total cost of requested copies is displayed;
otherwise the message “Required copies not in the stock” is displayed. Design a system using a class called
books with suitable member functions and constructors. Use new operator in constructors to allocate memory
space if required. Provide these extra facilities in the program:
1. The price of the books should be updated as and when required. Use a private member function to
implement this.
2. The stock value of each book should be automatically updated as soon as a transaction is
completed.
3. The number of successful and unsuccessful transactions should be recorded for the purpose of
statistical analysis. Use static data members to keep count of transactions.
#include<iostream>
using namespace std;
class book
{
string author;
string title;
string publisher;
float price;
int stock;
static int succ_trans;
static int unsucc_trans;
public:
book(string author,string title,string publisher,float price,int stock)
{
this->author=author;
this->title=title;
this->publisher=publisher;
this->price=price;
this->stock=stock;
void display()
{
cout<<"author:"<<author<<endl;
cout<<"title :"<<title<<endl;
cout<<"publisher : "<<publisher<<endl;
cout<<"price of book "<<price<<endl;
cout<<"stock: "<<stock<<endl;
}
void update(float newprice)
{
price=newprice;
}
}
else
{
cout<<"out of stock";
unsucc_trans++;
}
}
static int getsucc_tans()
{
return succ_trans;
}
static int getunsucc_trans()
{
return unsucc_trans;
}
};
int main()
{
book b("fredy cruger","thousand splendid suns","san diego",35.5,30);
b.display();
int requestedcoy;
cout<<"enter your requested copies "<,endl;
cin>>requestedcopy;
b.transaction(requestedcopy);
b.getsucc_trans();
b.getunsucc_trans();
cout<<"no. of successful transactions"<<getsucc_trans<<endl;
cout<<"no. of unsuccessful transactions"<<getunsucc_trans<<endl;