MODULE 3
Classes and Data Abstraction
Structures are used to represent a record, suppose you want to keep track of your books in a library. You might want to track the following attributes about each book
Title
Author
Subject
Book ID
Defining a Structure
To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member, for your program. The format of the struct statement is this −
struct [structure tag] {
member definition;
member definition;
...
member definition;
} [one or more structure variables];
The structure tag is optional and each member definition is a normal variable definition, such as int i; or float f; or any other valid variable definition. At the end of the structure's definition, before the final semicolon, you can specify one or more structure variables but it is optional.
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
Accessing Structure Members
To access any member of a structure, we use the member access operator (.). The member access operator is coded as a period between the structure variable name and the structure member that we wish to access. You would use struct keyword to define variables of structure type. Following is the example to explain usage of structure −
#include <iostream>
#include <cstring>
using namespace std;
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main() {
struct Books Book1; // Declare Book1 of type Book
struct Books Book2; // Declare Book2 of type Book
// book 1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
// book 2 specification
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
// Print Book1 info
cout << "Book 1 title : " << Book1.title <<endl;
cout << "Book 1 author : " << Book1.author <<endl;
cout << "Book 1 subject : " << Book1.subject <<endl;
cout << "Book 1 id : " << Book1.book_id <<endl;
// Print Book2 info
cout << "Book 2 title : " << Book2.title <<endl;
cout << "Book 2 author : " << Book2.author <<endl;
cout << "Book 2 subject : " << Book2.subject <<endl;
cout << "Book 2 id : " << Book2.book_id <<endl;
return 0;
}
When the above code is compiled and executed, it produces the following result −
Book 1 title : Learn C++ Programming
Book 1 author : Chand Miyan
Book 1 subject : C++ Programming
Book 1 id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Yakit Singha
Book 2 subject : Telecom
Book 2 id : 6495700
2. What is a class?
Access Specifier
It specifies if class members are accessible to external functions (functions of other classes).
C++ provides three access specifiers :
Fig. C3.2 : Types of access specifiers
1. private
2. public
3. protected
class A
{
friend class B;
};
3. Explain the assignment operator ?
4. Explain class scope?
When the scope of a declaration extends to or past the end of a class definition, the regions defined by the member definitions of that class are included in the scope of the class. Members defined lexically outside of the class are also in this scope. In addition, the scope of the declaration includes any portion of the declarator following the identifier in the member definitions.
The name of a class member has class scope and can only be used in the following cases:
In a member function of that class
In a member function of a class derived from that class
After the . (dot) operator applied to an instance of that class
After the . (dot) operator applied to an instance of a class derived from that class, as long as the derived class does not hide the name
After the -> (arrow) operator applied to a pointer to an instance of that class
After the -> (arrow) operator applied to a pointer to an instance of a class derived from that class, as long as the derived class does not hide the name
After the :: (scope resolution) operator applied to the name of a class
After the :: (scope resolution) operator applied to a class derived from that class
5. Explain class objects?
// creates object b1 of class Book
Object
b1 |
Data Members: name pages price |
Member functions: Accept() Display() |
b1 |
| b2 |
Data Members : name pages price |
| Data Members : name pages price |
Member functions: Accept() Display() |
| Member functions : Accept() Display() |
Fig: Classes and objects
6. Write a program to accept and display details of the book?
#include<iostream> //Step 1 Header file
using namespace std ;
class Book //Step 2 Declares class Book
{
char name[10]; // Declares data members name,
// pages and price.
int pages; // These data members are private.
float price;
public: //Starts public section of class book
void accept() //Step 3 Defines function “accept”
{ //Entering the details of book
cout<<“Enter name of book”;
cin>>name;
cout<<“Enter pages of book”;
cin>>pages;
cout<<“Enter price of book”;
cin>>price;
} //End of accept function definition
void display() //Step 4 Defines function display
{ // Displays all the details of book
cout<<“Name of book is :”<<name;
cout<<“No. of Pages in book are :”<<pages;
cout<<“Price of book is:”<<price;
} // End of display function
}; //Step 5 End of class
int main() //Step 6 Start of main() function
{
Book b1,b2; //Step 7 Creates objects b1,b2
b1.accept(); //Step 8 Calls accept function for b1
b2.accept(); // Calls accept function for b2
b1.display(); //Step 9 Calls display function for b1
b2.display(); //Calls display function for b2
return 0;
} // End of main
7. Write a C++ program to find if a number is even or odd using scope resolution operator
#include<iostream> using namespace std ; class Example { int number; public: void accept(); void EvenOdd(); }; void Example::accept() { cout<<“Enter number”; cin>>number; } void Example:: EvenOdd() { if(number%2= =0) cout<<number<<“is even number”; else cout<<number<<“is odd number”; } void main() { Example O; O.accept(); O.EvenOdd(); } | //Header file
//Declares class Example //Declares data member number. It is private data member. //Starts public section of class Example //Declares functions “accept” and EvenOdd
//End of class definition //Defining accept function outside class using scope resolution operator.
//Accepting value for number from user //End of accept function definition //Defining EvenOdd function outside class using scope resolution operator.
//Checks if number is even or odd
//End of EvenOddfunction //Start of main() function //Creates object O //Calls accept function //Calls EvenOdd function //End of main |
8. Explain constructors?
class example
{
int x,y;
public:
example() //constructor, same name as that of class
{
}
void display()
{
:
}
};
class example
{
int x,y;
public:
example() //constructor initializing data members of class
{
x=10;
y=20;
}
void display()
{
:
}
};
9. Explain the type of constructors?
Types of constructors with syntax
Fig: Types of Constructor
Default /Non parameterized Constructor
class Volume
{
int x;
public:
Volume () // default constructor
{
x=10;
}
void display()
{
cout<<x*x*x;
}
};
void main()
{
Volume Obj; //invokes default constructor
Obj.display();
}
Parameterized Constructor
A constructor that receives parameters is parameterized constructor.
class Volume {
int x;
public:
Volume ( int a) // parameterized constructor
{
x=a;
}
void display()
{
cout<<x*x*x;
}
};
int main()
{
Volume obj(10); //invokes parameterized constructor
and pass argument 10 to it.
obj.display();
}
Copy Constructor
Copy constructor initializes an object using values of another object passed to it as a parameter.
class Volume
{
int x;
public:
Volume ( int & P) // copy constructor
{
x=P.x; //copies value
}
void display()
{
cout<<x*x*x;
}
};
int main()
{
Volume obj1; //invokes default constructor
Volume obj2(obj1); //calls copy constructor
obj2.display();
}
10. Explain constructor overloading?
Constructors can be overloaded just like member functions. Thus we can have multiple constructors in a class.
Write a C++ program to find volume of cube, cylinder.
Solution :
#include<iostream>
using namespace std ;
class Volume //Defines class Volume
{
int x,y,z;
public:
Volume ( int a) // Step 1Constructor with one argument
{
x=a;
}
Volume ( int a, int b) // Step 2 Constructor with two arguments
{
x=a;
y=b;
}
void displaycube();
void displaycylinder();
};
void Volume::displaycube()
// Step 3 Function to calculate volume of cube
{
cout<<x*x*x;
}
void Volume::displaycylinder()
//Step 4 Function to calculate volume of cylinder
{
cout<<3.14*x*x*y;
}
int main()
{
Volume obj1(10);
// Step 5Calls constructor with one argument.
Volume obj2(10,5);
// Step 6 Calls constructor with two arguments.
obj1. displaycube (); //Step 7
obj2. displaycylinder ();
return 0 ;
}
11. Explain default constructor?
Default /Non parameterized Constructor
class Volume
{
int x;
public:
Volume () // default constructor
{
x=10;
}
void display()
{
cout<<x*x*x;
}
};
void main()
{
Volume Obj; //invokes default constructor
Obj.display();
}
12. Explain destructor?
class example
{
public:
~example();//destructor
};