The document discusses the differences between structures in C and classes in C++. In C, structures are used to group related data but have limitations like not allowing operator overloading. C++ extends structures and introduces classes, which allow data hiding, inheritance and both data and functions as members. Classes in C++ can be used interchangeably with structures but are generally used to define types that have both data and methods.
1. Prepared By: Asst. Prof. Sejal Jadav
Unit-2
Classes and Objects, Constructor and Destructor
B.C.A & B.Sc.(IT) – 3
CS-13 C++ and Object Oriented
Programming
2. Prepared By: Asst. Prof. Sejal Jadav
C structures revisited
• The most important feature in C++ is the ‘class’ which
is the extension to structures.
• C – Structure is a user defined template for handling a
group of logically related data items.
• Once the structure type has been defined, we can
create variables of that type.
3. Prepared By: Asst. Prof. Sejal Jadav
For example
struct college
{
int code;
char name[20];
};
4. Prepared By: Asst. Prof. Sejal Jadav
• Here, keyword ‘struct’ declares ‘college’ as new data
type that can hold two members or elements of
different data type.
• The identifier ‘student’ is referred as to structure
name or structure tag that can be used to create
variables of type student.
5. Prepared By: Asst. Prof. Sejal Jadav
• For example
struct college c1; //C notation
• Here, c1 is a variable of type college and it has two
member variables as defined by the college template.
6. Prepared By: Asst. Prof. Sejal Jadav
• Member variables can be accessed using dot or
period operator as following
c1.code = 12345;
strcpy(c1.name,”CCSIT”);
• Structure can have arrays, pointers or even structures
as members.
7. Prepared By: Asst. Prof. Sejal Jadav
Limitations
• The standard c does not allow the struct data type to be
treated like built-in types.
• For example
struct demo
{
int x; int y;
};
struct demo d1, d2, d3;
8. Prepared By: Asst. Prof. Sejal Jadav
• Here d1, d2 and d3 can easily be assigned value using
dot operator but we can’t add two demo type
numbers to one another.
• For example
d3 = d1 + d2; //is illegal in C.
• Moreover, C – structures do not allow data hiding
because they are public.
9. Prepared By: Asst. Prof. Sejal Jadav
Extension
• C++ supports all features of C – structures but it has
expanded capabilities to suit with OOP concept.
• It also provides facilities like data hiding and
inheritance.
10. Prepared By: Asst. Prof. Sejal Jadav
• In C++, a structure can have both variables and
functions as members.
• Even member can also be declared as private.
• In C++, the structure names are stand-alone and can
be used just like any other data type.
11. Prepared By: Asst. Prof. Sejal Jadav
• It means ‘struct’ keyword is optional while declaring
structure type variables. Only structure name is
enough.
• For example
student s1; // C++ notation which is error in C
12. Prepared By: Asst. Prof. Sejal Jadav
• C++ incorporates all these extensions in another user
defined type known as ‘class’.
• There is very little syntactical(Syntax) difference
between structure and class and therefore they can
be used interchangeably.
• Most of programmers use structure for holding only
data and class for both data and functions.