CODING QUESTIONS & ANSWERS – INTRODUCTION TO CLASSES (C++)
This PDF includes all coding questions and answers covering:
- Classes and objects
- Access specifiers
- Const member functions
- Member function definitions
- Pointers and dynamic allocation
- Header/implementation files
- Constructors (default, parameterized, overloaded)
- Destructor
- Private helper functions
- Arrays of objects
-------------------------------------------------------------
1. Basic Class Declaration
-------------------------------------------------------------
class Rectangle {
private:
double width;
double length;
public:
void setWidth(double w) { width = w; }
void setLength(double len) { length = len; }
double getWidth() const { return width; }
double getLength() const { return length; }
double getArea() const { return width * length; }
};
-------------------------------------------------------------
2. Using Objects
-------------------------------------------------------------
Rectangle r;
[Link](5.0);
[Link](4.0);
cout << [Link]();
-------------------------------------------------------------
3. Const Member Function
-------------------------------------------------------------
class Book {
private:
int pages;
public:
Book(int p){ pages = p; }
int getPages() const { return pages; }
};
-------------------------------------------------------------
4. Member Functions Outside Class
-------------------------------------------------------------
class Box {
private:
double height;
public:
void setHeight(double h);
double getHeight() const;
};
void Box::setHeight(double h){ height = h; }
double Box::getHeight() const { return height; }
-------------------------------------------------------------
5. Pointer To Object
-------------------------------------------------------------
Rectangle obj;
Rectangle *p = &obj;
p->setWidth(3.0);
p->setLength(7.0);
cout << p->getArea();
-------------------------------------------------------------
6. Dynamic Allocation
-------------------------------------------------------------
Rectangle *p = new Rectangle;
p->setWidth(10.0);
p->setLength(2.0);
cout << p->getArea();
delete p;
-------------------------------------------------------------
7. Separate Header and CPP
-------------------------------------------------------------
Rectangle.h:
class Rectangle {
private:
double width, length;
public:
void setWidth(double);
void setLength(double);
double getWidth() const;
double getLength() const;
double getArea() const;
};
[Link]:
void Rectangle::setWidth(double w){ width = w; }
void Rectangle::setLength(double l){ length = l; }
double Rectangle::getWidth() const { return width; }
double Rectangle::getLength() const { return length; }
double Rectangle::getArea() const { return width * length; }
-------------------------------------------------------------
8. Default Constructor
-------------------------------------------------------------
class Demo {
public:
Demo(){ cout << "Constructor"; }
};
-------------------------------------------------------------
9. Parameterized Constructor
-------------------------------------------------------------
class Rectangle {
private:
double w, l;
public:
Rectangle(double a, double b){ w=a; l=b; }
};
-------------------------------------------------------------
10. Constructor With Default Argument
-------------------------------------------------------------
class Sale {
private:
double cost, tax;
public:
Sale(double c, double t=0.05){
cost=c;
tax=t;
}
double getTotal() const { return cost + cost * tax; }
};
-------------------------------------------------------------
11. Overloaded Constructors
-------------------------------------------------------------
class Item {
private:
string name;
double price;
public:
Item(){ name=""; price=0; }
Item(string n){ name=n; price=0; }
Item(string n,double p){ name=n; price=p; }
};
-------------------------------------------------------------
12. Destructor Example
-------------------------------------------------------------
class Demo {
public:
Demo(){ cout << "Constructor"; }
~Demo(){ cout << "Destructor"; }
};
-------------------------------------------------------------
13. Destructor Freeing Memory
-------------------------------------------------------------
class Contact {
private:
char *name;
public:
Contact(const char *n){
name = new char[strlen(n)+1];
strcpy(name,n);
}
~Contact(){
delete [] name;
}
};
-------------------------------------------------------------
14. Private Helper Functions
-------------------------------------------------------------
class Contact {
private:
char *name;
void init(const char *n){
name = new char[strlen(n)+1];
strcpy(name,n);
}
public:
Contact(const char *n){ init(n); }
~Contact(){ delete [] name; }
};
-------------------------------------------------------------
15. Array of Objects
-------------------------------------------------------------
Item arr[] = {
Item("Hammer",6.95),
Item("Wrench",8.75),
Item("Pliers",3.50)
};
for(int i=0;i<3;i++){
cout << arr[i].getName() << " " << arr[i].getPrice();
}
-------------------------------------------------------------
16. Dynamic Array of Objects
-------------------------------------------------------------
Item *a = new Item[2]{
Item("Pen",1.5),
Item("Book",3.0)
};
delete [] a;
-------------------------------------------------------------
END OF EXAM CODING SET