Tuesday, 17 July 2012

VU CS304- Object Oriented Programming LATEST SOLVED SUBJECTIVES FROM MIDTERM PAPERS 2011 Part2


VU CS304- Object Oriented Programming LATEST SOLVED SUBJECTIVES FROM MIDTERM PAPERS 2011 Part2
MIDTERM EXAMINATION 2011
write c++ code for operator() ......5 marks
Answer:- (Page 161)
Function Operator
class String{
...
public:
char & operator()(int);
...
};
char & String::operator()
(int pos){
assert(pos>0 && pos<=size);
return bufferPtr[pos-1];
}
int main(){
String s1(“Ping”);
char g = s1(2); // g = „i‟
s1(2) = „o‟;
cout << g << “\n”;
cout << str.GetString();
return 0;
}
Write c++ code for operator [] using string class.......5 marks.....
Answer:- (Page 160)
Subscript operator must be overloaded as member function of the class with one
parameter of integer type,
class String{
...
public:
char & operator[](int);
...
};
char & String::operator[]( int pos){
assert(pos>0 && pos<=size);
return stringPtr[pos-1];
}
int main() {
String s1(“Ping”);
cout <<str.GetString()<< endl;
s1[2] = „o‟;
cout << str.GetString();
return 0;
}4
static data members.......2.mark
Answer:- (Page 108)
“A variable that is part of a class, yet is not part of any object of that class, is called static data member”

No comments:

Post a Comment