C++ 2

  • Uploaded by: sherwingodgift
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View C++ 2 as PDF for free.

More details

  • Words: 1,124
  • Pages: 24
Creating Classes and Object

Objectives In this lesson, you will learn to: • Creating Classes • Define member variables • Define member function • Define object • Initialize object

hp Invent

Creating Classes and Object

Problem Statement You are suppose to develop a module that accepts the student details. the details are given below. Student

student_name [25] mobile_no[11] date_of_birth[9] stud_adress[40]

get() print()

hp Invent

Creating Classes and Object

Class • A class defines a template for declaring run-time object. • All data within a class excepted to be private. • the public interface to a class consists of function know as methods. • Classes are the user defined datatypes. • The class keyword is used to declare a class.

hp

Invent

Creating Classes and Object Rules for Naming classes in C++ • A class name must not have embedded space or symbol like ? - + ! @ # % ^ & * ( ) [ ] { } . , ; : “ ‘ / and \. But we can use a underscore. • A class must begin with a letter hich may be followed by a sequence of letters or digits (0-9)but the first character of class name cannot be a digit. Class Naming Convention in C++ • A class name should be meaningful • A class should ideally be a noun. •First letter of every world should capital. Like:- EmployeeDetails

hp Invent

Creating Classes and Object

Access Specifiers • Access specifiers are used to determine whether any other class or function can access the members of class or not. • C++ class support three access specifiers. – public – private – protected

• public access specifiers will expose its members to other function and object.

hp

Invent

Creating Classes and Object

• private access specifiers will hide its members to other function and object. It’s the default access specifiers. • Protected access specifiers will work like private access specifiers and the difference we will study with Inheritance.

hp Invent

Creating Classes and Object

A simple class class StudentDetails { //class members will come here }; • Here class is a keyword • StudentDetails is a user defined class name

hp

Invent

Creating Classes and Object Now we can write our class class StudentDetails { private: char studName[ 25]; char mobileNo[11]; char dateOfBirth[9]; char studAdderss[40]; public:

hp Invent

Creating Classes and Object void get() { cout<<“Enter your name “; cin>>studName; cout<<“Enter your mobile no.”; cin>>mobileNo; cout<<“Enter your Date of birth”; cin>>dateOfBirth; cout<<“Enter your Address”; cin>>studAdderss; }

hp

Invent

Creating Classes and Object

void print() { cout<<“Name :”<<studName; cout<<“Mobile no :”<<mobileNo; cout<<“Date of birth :”<
hp Invent

Creating Classes and Object

• Encapsulation and classes – Object characteristics assembled at one place – Rigid enforcement of access specification.

• Data Abstraction and classes – Hides implementation details – A user-defined data type

Objects • Class cant work on its own so we have to make a working unit of it and the working unit of class is knows as object. • We can also call a object is the instance of class in memory.

hp

Invent

Creating Classes and Object More on objects …….. Polygon class

Polygon objects

Properties: Vertices Border color Abstract Fill color into

Methods: Draw Erase Move

hp Invent

Creating Classes and Object More on objects ……..

• An object in C++ is an item declared to be a class type Example: StudentDetails sobj; here sobj is the object of StudentDetails class.

• An object is an instance of class. • We can access the member of a object with the . (dot) operator. Now we can write the main for our class as given

hp

Invent

Creating Classes and Object

int main() { StudentDetails sobj; sobj.get(); //calling get function of object sobj.print();//calling print function } After compilation it will accept student it will print the student details.

hp

Invent

Creating Classes and Object

• In the above program if you will call only print function it will print some undesired values because the variables are not being given any initial value. • We can do it with the help of a function knows as constructor. • constructor is a special method automatically invoked when an object of a class is created. • A constructor is called to – Allocate space for the object – Initialize data members

hp Invent

Creating Classes and Object

More on Constructor….. • Each class can has one or more constructor • A constructor – Can be overloaded – Is a member function with the same name as class – Can have parameter but no return value – Implicitly returns a ‘this’ pointer to created object – It has many types (we will discus then with other topic).

hp

Invent

Creating Classes and Object More on Constructor….. • In our class we will make constructor as class StudentDetails { private: //declare variables as above public: StudentDetails () { // Initialize data members } //create other methods }//close class

hp Invent

Creating Classes and Object

• The constructor does not take any parameter is knows as default constructor • In case no constructor declared for the class the compiler creates a default constructor in such case ,instantiation of an object of this class invokes the default constructor. A Destructor • Is a member function • Is automatically called when an object is destroyed

hp

Invent

Creating Classes and Object

More on Destructor……. • Has the same name as the class plus a leading tilde (~). • Takes no arguments. • Has no return type • Cannot be overloaded • Usually frees up dynamic memory allocated by object • In our case it will be made like

hp

Invent

Creating Classes and Object More on Destructor……. • In our class we will make constructor as class StudentDetails { private: //declare variables as above public: ~ StudentDetails () { // Initialize data members } //create other methods and constructor }//close class

hp Invent

Creating Classes and Object We can also write member function out side class by using scope resolution operator(::). class StudentDetails { private: char studName[ 25]; char mobileNo[11]; char dateOfBirth[9]; char studAdderss[40]; public: void get(); void print(); }; //class closed

hp

Invent

Creating Classes and Object

void StudentDetails::get() { cout<<“Enter your name “; cin>>studName; cout<<“Enter your mobile no.”; cin>>mobileNo; cout<<“Enter your Date of birth”; cin>>dateOfBirth; cout<<“Enter your Address”; cin>>studAdderss; }

hp

Invent

Creating Classes and Object void StudentDetails::print() { cout<<“Name :”<<studName; cout<<“Mobile no :”<<mobileNo; cout<<“Date of birth :”<
hp Invent

Creating Classes and Object

• Write a program to accept the customer details. Class diagram is given below. Customer

custId[4] custName[20] mobileNo[11] dateOfSales[9] custAdress[40] accept() display()

hp Invent

Related Documents

C-2
October 2019 13
C 2
November 2019 10
C#-2
November 2019 16
C 2
October 2019 19
C++ 2
June 2020 8
C++ 2
May 2020 13

More Documents from "Mohan"

C++ 1
June 2020 13
C++ 2
June 2020 8