Types of Computer Languages Procedural BASIC FORTRAN PASCAL

Types of Computer Languages Procedural BASIC, FORTRAN, PASCAL Functional C, C++ Object Oriented C++, C#, JAVA 1

Object Oriented Programming • An object is an entity that store its own data and its own function. The data is termed as attributes or characteristic and behavior is termed as function. – Example: if we consider “monitor” as an object. Then its size, color, manufacturer are attributes and when you switch on it display some information is its behavior. • The fundamental idea behind OOP is to combine data and function(s) operating on that data into single unit. 2

Class vs struct • Class is a keyword like struct used to define a user-defined data type. • The data type is then further used to define a variable (object). So, a class defines the structure of an object A class is like a struct with following differences: • All members declared in struct are global or public by default while in class are private by default. 3

• Classes are stored in memory via heap whereas structs are stored in memory via stack. • Class can extend another class while a structures cannot extend anything. • Structure members cannot be declared as Protected while class members can be. • Constructors and Destructors can be defined in classes while in struct it cannot. 4

Access Level of Classes There are three types of access level of members of a class: • Private: The members are accessible only by the member functions or friend functions. • Protected: These members are accessible by the member functions of the class and the classes which are derived from this class. • Public: Accessible by any external member or function. 5

Syntax of defining a Class Syntax: class_name { access specifier: (private, protected, public) member(s); function(s) access specifier: member(s); function(s); } object_names; Example: class record { Private: float marks; Protected: char name[10]; int Roll. No; Public: Void dataentry(); Void datadisp(); } student; 6

Defining function Define and declare inside the class. Declare inside and define outside the class Class record class record { { char name[10]; public: void Entry(); { }; cout<<“Enter student Name”; cin>>name; } Scope operator Void record: : Entry() { cout<<“Enter student Name”; }; cin>>name; } 7

Defining an object and Accessing Members of a class with (. ) operator class record { char name[10]; public: main() { record student; student. Entry(); } void Entry(); }; Void record: : Entry() { cout<<“Enter student Name”; cin>>name; } 8

Defining an object and Accessing Members of a class with (->) operator class record { char name[10]; public: main() { record *student; student=new record; student->Entry(); } void Entry(); }; Void record: : Entry() { cout<<“Enter student Name”; cin>>name; } 9

Initializing data members of a class Data members of a class can be initialized by: • Functions • Constructors class record Main() { { int x; record obj; public: obj. init_x(10); void init_x(int a); obj. disp(); { x=a; } } void disp() { cout<<“The value of x=“<<x; } }; 10

Constructor • Constructors are special member functions of a class used to initialize member data of a class. • There can be any number of constructors inside a class, provided they have a different set of parameters. • There are some important qualities for a constructor to be noted. • Constructors have the same name as the class. • Constructors do not return any values • They are automatically executed when an object is created. 11

Destructor • Destructors in C++ also have the same name, except for the fact that they are preceded by a '~' operator. • The destructors are called to delete an object. It is not necessary to declare a constructor or a destructor inside a class. • If not declared, the compiler will automatically create a default one for each. • If the constructor/destructor is declared as private, then the class cannot be instantiated 12

Initializing data members of a class by constructor class record Main() { { int x; record obj(60); public: obj. disp(); record(int a); } { x=a; } void disp() { cout<<“The value of x=“<<x; } }; 13

class test { int x; public: test(int in) { x=in; cout<<" constructor is called and assign a value="<<x<<endl; } ~test() { main() { test a(20); a. disp(); test *b; b=new test(30); b->disp(); delete b; b->disp(); system("pause"); } cout<<" destructor is called"<<endl; } void disp() { cout<<"The value is ="<<x<<endl; } }; 14
- Slides: 14