Answers

Question and Answer:

  Home  C++ Access Control

⟩ Explain classes and structure?

Class: User defined data type which contains data and methods to manipulate that data; is known as class. It is the fundamental packaging unit of OO technology. An object is a variable of a Class. Each object is associated with the data of type class with which it is created. Thus we can also say that class is a collection of objects of similar types. It is a user defined data type and behaves like built-in data type of the language. Since class has data and methods to manipulate the data, it supports one of the most important features of OO: Data Encapsulation.

Eg.

class Student

{

int rollno;

int marks1, marks2;

public:

void show(int r); // to print marks

void sum(int r); // to add the marks

};

We can create objects of class using:

class Student s1, s2;

Structure:

A structure is a collection of variables, referenced under one name, providing a convenient means of keeping related information together. Structure declaration forms a template which can be used to create structure objects. The variables inside a structure are called members. Generally all the members of a structure are logically related. Structure declaration precedes the keyword struct.

Consider following example:

struct address

{

char name[80];

char street[80];

char city[20];

char state[20];

};

Please note the semicolon at the end of structure declaration. This is done because a structure declaration is a statement. The type name of this structure is address. We can create structure variables using:

struct address ad1, ad2;

ad1 and ad2 are two variables of the type struct address..

 194 views

More Questions for you: