Computer Notes - Class

0

No comments posted yet

Comments

Slide 7

class is a keyword and must always be in small caps Omitting the semicolon at the end is a syntax error 13:22

Slide 17

Accessing a private member from outside member function is a syntax error You can use access specifiers as many times as you like Each access specifier must be followed by a ‘:’

Slide 1

Class http://ecomputernotes.com

Slide 2

Class Class is a tool to reSmithze objects Class is a tool for defining a new type http://ecomputernotes.com

Slide 3

Example Lion is an object Student is an object Both has some attributes and some behaviors http://ecomputernotes.com

Slide 4

Uses The problem becomes easy to understand Interactions can be easily modeled http://ecomputernotes.com

Slide 5

Type in C++ Mechanism for user defined types are Structures Classes Built-in types are like int, float and double User defined type can be Student in student management system Circle in a drawing software http://ecomputernotes.com

Slide 6

Abstraction Only include details in the system that are required for making a functional system Student Name Address Sibling Father Business http://ecomputernotes.com

Slide 7

Defining a New User Defined Type class ClassName { … DataType MemberVariable; ReturnType MemberFunction(); … }; http://ecomputernotes.com

Slide 8

Example class Student { int rollNo; char *name; float CGPA; char *address; … void setName(char *newName); void setRollNo(int newRollNo); … }; http://ecomputernotes.com

Slide 9

Why Member Function They model the behaviors of an object Objects can make their data invisible Object remains in consistent state http://ecomputernotes.com

Slide 10

Example Student aStudent; aStudent.rollNo = 514; aStudent.rollNo = -514; //Error http://ecomputernotes.com

Slide 11

Object and Class Object is an instantiation of a user defined type or a class http://ecomputernotes.com

Slide 12

Declaring class variables Variables of classes (objects) are declared just like variables of structures and built-in data types TypeName VaraibaleName; int var; Student aStudent; http://ecomputernotes.com

Slide 13

Accessing members Members of an object can be accessed using dot operator (.) to access via the variable name arrow operator (->) to access via a pointer to an object Member variables and member functions are accessed in a similar fashion http://ecomputernotes.com

Slide 14

Example class Student{ int rollNo; void setRollNo(int aNo); }; Student aStudent; aStudent.rollNo; http://ecomputernotes.com

Slide 15

Access specifiers http://ecomputernotes.com

Slide 16

Access specifiers There are three access specifiers ‘public’ is used to tell that member can be accessed whenever you have access to the object ‘private’ is used to tell that member can only be accessed from a member function ‘protected’ to be discussed when we cover inheritance http://ecomputernotes.com

Slide 17

Example class Student{ private: char * name; int rollNo; public: void setName(char *); void setRollNo(int); ... }; http://ecomputernotes.com

Slide 18

Example class Student{ ... int rollNo; public: void setRollNo(int aNo); }; int main(){ Student aStudent; aStudent.SetRollNo(1); } http://ecomputernotes.com

Slide 19

Default access specifiers When no access specifier is mentioned then by default the member is considered private member http://ecomputernotes.com

Slide 20

Example class Student { char * name; int RollNo; }; class Student { private: char * name; int RollNo; }; http://ecomputernotes.com

Slide 21

Example class Student { char * name; int RollNo; void SetName(char *); }; Student aStudent; aStudent.SetName(Smith);

Slide 22

Example class Student { char * name; int RollNo; public: void setName(char *); }; Student aStudent; aStudent.SetName(“Smith”); http://ecomputernotes.com

Summary: http://ecomputernotes.com - Computer Notes - Class in Object oriented Programming what is Class OOPs Explain about it in detail .explain it with example

Tags: computer notes - class

URL:
More by this User
Most Viewed